Skip to content

Commit 1a22115

Browse files
committed
Refactor: Consolidate banner handling
1 parent 39e35aa commit 1a22115

File tree

5 files changed

+212
-35
lines changed

5 files changed

+212
-35
lines changed

.banner

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
PHP Code Compiler - Phar executable compiling utility
1+
PHP Code Compiler - PHAR executable compiling utility
22

33
Copyright (c) Yann Blacher (Yannoff) - MIT License
44

5-
For the full copyright and license information, please see
6-
{@link https://github.com/yannoff/phpcc/blob/main/LICENSE}
5+
For the full copyright and license information, please
6+
see https://github.com/yannoff/phpcc/blob/main/LICENSE

src/Command/Compile.php

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
use Yannoff\Component\Console\Command;
1919
use Yannoff\Component\Console\Definition\Option;
2020
use Yannoff\Component\Console\Exception\RuntimeException;
21+
use Yannoff\PhpCodeCompiler\Contents;
2122
use Yannoff\PhpCodeCompiler\Directory;
2223
use Yannoff\PhpCodeCompiler\PharBuilder;
24+
use Yannoff\PhpCodeCompiler\Syntax\PHPComment;
2325

2426
class Compile extends Command
2527
{
@@ -196,10 +198,9 @@ protected function setNotice(string $banner = null): self
196198
{
197199
if (is_file($banner)) {
198200
$this->info("Loading banner contents from <strong>$banner</strong> file...");
199-
$contents = file_get_contents($banner);
200-
$header = $this->phpdocize($contents);
201+
$header = $this->commentOut($banner);
201202

202-
$this->info($header, 'grey');
203+
$this->info(implode("\n", $header), 'grey');
203204
$this->builder->setBanner($header);
204205
}
205206

@@ -262,27 +263,24 @@ protected function require(string $option)
262263
}
263264

264265
/**
265-
* Return the contents wrapped in a comments block
266+
* Return the banner file contents wrapped in a comments block
266267
*
267-
* @param string $contents
268+
* @param string $banner Path to the banner file
268269
*
269-
* @return string
270+
* @return string[]
270271
*/
271-
protected function phpdocize(string $contents): string
272+
protected function commentOut(string $banner): array
272273
{
273-
$lines = array_map(
274-
function($line) { return sprintf(' * %s', $line); },
275-
explode("\n", $contents)
276-
);
277-
278-
array_unshift($lines, '/**');
279-
array_push($lines, ' */');
280-
281-
return implode("\n",$lines);
274+
return Contents::open($banner)
275+
->prefix(PHPComment::STAR)
276+
->prepend(PHPComment::OPEN)
277+
->append(PHPComment::CLOSE)
278+
->all()
279+
;
282280
}
283281

284282
/**
285-
* Print a message to STDERR, optionally encapsuled by styling tags
283+
* Print a message to STDERR, optionally encapsulated by styling tags
286284
*
287285
* @param string $message
288286
* @param string $tag

src/Contents.php

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the PHP Code Compiler project
5+
*
6+
* Copyright (c) Yannoff (https://github.com/yannoff)
7+
*
8+
* @project PHP Code Compiler (yannoff/phpcc)
9+
* @homepage https://github.com/yannoff/phpcc
10+
* @license https://github.com/yannoff/phpcc/blob/main/LICENSE
11+
*
12+
* For the full copyright and license information, please view
13+
* the LICENSE file that was distributed with this source code.
14+
*/
15+
16+
namespace Yannoff\PhpCodeCompiler;
17+
18+
/**
19+
* Multi-line text manipulation helper class
20+
*
21+
* Usage example:
22+
*
23+
* $comments = Contents::open('banner.txt')
24+
* ->prefix('* ')
25+
* // or
26+
* ->map(function($line){ return '* ' . $line; })
27+
*/
28+
class Contents
29+
{
30+
/**
31+
* @var string[]
32+
*/
33+
protected $lines = [];
34+
35+
/**
36+
* @param ?string[] $lines
37+
*/
38+
public function __construct(array $lines = null)
39+
{
40+
$this->lines = $lines ?? [];
41+
}
42+
43+
/**
44+
* Render a string representation of the contents
45+
*
46+
* @return string
47+
*/
48+
public function __toString(): string
49+
{
50+
return $this->join("\n");
51+
}
52+
53+
/**
54+
* Create a new instance from the given lines stack
55+
*
56+
* @param string[] $lines
57+
*
58+
* @return Contents
59+
*/
60+
public static function load(array $lines = null): Contents
61+
{
62+
return new static($lines);
63+
}
64+
65+
/**
66+
* Create a new instance from the given file contents
67+
*
68+
* @param string $file
69+
*
70+
* @return Contents
71+
*/
72+
public static function open(string $file): Contents
73+
{
74+
$lines = file($file, FILE_IGNORE_NEW_LINES);
75+
76+
return self::load($lines);
77+
}
78+
79+
/**
80+
* Apply the given method to each line of the contents
81+
*
82+
* @param callable $callback
83+
*
84+
* @return self
85+
*/
86+
public function map(callable $callback): self
87+
{
88+
$this->lines = array_map($callback, $this->lines);
89+
90+
return $this;
91+
}
92+
93+
/**
94+
* Add a new line at the end of the contents
95+
*
96+
* @param string $line
97+
*
98+
* @return self
99+
*/
100+
public function append(string $line): self
101+
{
102+
$this->lines[] = $line;
103+
104+
return $this;
105+
}
106+
107+
/**
108+
* Insert a new line at the beginning of the contents
109+
*
110+
* @param string $line
111+
*
112+
* @return self
113+
*/
114+
public function prepend(string $line): self
115+
{
116+
array_unshift($this->lines, $line);
117+
118+
return $this;
119+
}
120+
121+
/**
122+
* Render a concatenated representation of the contents
123+
*
124+
* @param string $glue
125+
*
126+
* @return string
127+
*/
128+
public function join(string $glue = null): string
129+
{
130+
return implode($glue, $this->all());
131+
}
132+
133+
/**
134+
* Prefix each line of the contents with the given string
135+
*
136+
* @param string $prefix
137+
*
138+
* @return self
139+
*/
140+
public function prefix(string $prefix): self
141+
{
142+
return $this->map(function($line) use ($prefix) { return $prefix . $line; });
143+
}
144+
145+
/**
146+
* Getter for the contents stack
147+
*
148+
* @return string[]
149+
*/
150+
public function all(): array
151+
{
152+
return $this->lines ?? [];
153+
}
154+
}

src/PharBuilder.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class PharBuilder
5353
/**
5454
* Optional banner/legal notice
5555
*
56-
* @var string
56+
* @var string[]
5757
*/
5858
protected $banner;
5959

@@ -107,11 +107,11 @@ public function init(): self
107107
/**
108108
* Setter for the banner contents
109109
*
110-
* @param string $banner
110+
* @param string[] $banner
111111
*
112112
* @return self
113113
*/
114-
public function setBanner(string $banner): self
114+
public function setBanner(array $banner): self
115115
{
116116
$this->banner = $banner;
117117

@@ -166,22 +166,15 @@ public function list(): array
166166
/**
167167
* Generate the PHAR stub definition contents
168168
*
169-
* @param string $main The main entrypoint script
170-
* @param bool $shebang Whether to include the shebang line
171-
* @param ?string $banner Optional legal notice text
169+
* @param string $main The main entrypoint script
170+
* @param bool $shebang Whether to include the shebang line
171+
* @param ?string[] $banner Optional legal notice text
172172
*
173173
* @return string
174174
*/
175-
protected function stub(string $main, bool $shebang = true, string $banner = null): string
175+
protected function stub(string $main, bool $shebang = true, array $banner = null): string
176176
{
177-
$lines = [];
178-
if ($shebang) {
179-
$lines[] = '#!/usr/bin/env php';
180-
}
181-
$lines[] = '<?php';
182-
if ($banner) {
183-
$lines[] = $banner;
184-
}
177+
$lines = $banner ?? [];
185178
$lines[] = sprintf('// Compiled with PHP version %s', PHP_VERSION);
186179
$lines[] = sprintf('Phar::mapPhar("%s");', $this->pharname);
187180
// Add support for builtin phar flavoured require "vendor/autoload.php"
@@ -190,6 +183,12 @@ protected function stub(string $main, bool $shebang = true, string $banner = nul
190183
$lines[] = sprintf('set_include_path("phar://%s/");', $this->pharname);
191184
$lines[] = sprintf('require "%s"; __HALT_COMPILER();', $main);
192185

186+
array_unshift($lines, '<?php');
187+
188+
if ($shebang) {
189+
array_unshift($lines, '#!/usr/bin/env php');
190+
}
191+
193192
return implode(self::EOL, $lines);
194193
}
195194

src/Syntax/PHPComment.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the PHP Code Compiler project
5+
*
6+
* Copyright (c) Yannoff (https://github.com/yannoff)
7+
*
8+
* @project PHP Code Compiler (yannoff/phpcc)
9+
* @homepage https://github.com/yannoff/phpcc
10+
* @license https://github.com/yannoff/phpcc/blob/main/LICENSE
11+
*
12+
* For the full copyright and license information, please view
13+
* the LICENSE file that was distributed with this source code.
14+
*/
15+
16+
namespace Yannoff\PhpCodeCompiler\Syntax;
17+
18+
/**
19+
* PHP Comment tokens string representations
20+
*/
21+
class PHPComment
22+
{
23+
const OPEN = '/**';
24+
const CLOSE = ' */';
25+
const STAR = ' * ';
26+
}

0 commit comments

Comments
 (0)