diff --git a/.banner b/.banner index 7bfdab6..c5ea204 100644 --- a/.banner +++ b/.banner @@ -1,6 +1,6 @@ -PHP Code Compiler - Phar executable compiling utility +PHP Code Compiler - PHAR executable compiling utility Copyright (c) Yann Blacher (Yannoff) - MIT License -For the full copyright and license information, please see -{@link https://github.com/yannoff/phpcc/blob/main/LICENSE} +For the full copyright and license information, please +see https://github.com/yannoff/phpcc/blob/main/LICENSE diff --git a/src/Command/Compile.php b/src/Command/Compile.php index dba0841..c1cc323 100644 --- a/src/Command/Compile.php +++ b/src/Command/Compile.php @@ -18,8 +18,10 @@ use Yannoff\Component\Console\Command; use Yannoff\Component\Console\Definition\Option; use Yannoff\Component\Console\Exception\RuntimeException; +use Yannoff\PhpCodeCompiler\Contents; use Yannoff\PhpCodeCompiler\Directory; use Yannoff\PhpCodeCompiler\PharBuilder; +use Yannoff\PhpCodeCompiler\Syntax\PHPComment; class Compile extends Command { @@ -196,10 +198,9 @@ protected function setNotice(string $banner = null): self { if (is_file($banner)) { $this->info("Loading banner contents from $banner file..."); - $contents = file_get_contents($banner); - $header = $this->phpdocize($contents); + $header = $this->commentOut($banner); - $this->info($header, 'grey'); + $this->info(implode("\n", $header), 'grey'); $this->builder->setBanner($header); } @@ -262,27 +263,24 @@ protected function require(string $option) } /** - * Return the contents wrapped in a comments block + * Return the banner file contents wrapped in a comments block * - * @param string $contents + * @param string $banner Path to the banner file * - * @return string + * @return string[] */ - protected function phpdocize(string $contents): string + protected function commentOut(string $banner): array { - $lines = array_map( - function($line) { return sprintf(' * %s', $line); }, - explode("\n", $contents) - ); - - array_unshift($lines, '/**'); - array_push($lines, ' */'); - - return implode("\n",$lines); + return Contents::open($banner) + ->prefix(PHPComment::STAR) + ->prepend(PHPComment::OPEN) + ->append(PHPComment::CLOSE) + ->all() + ; } /** - * Print a message to STDERR, optionally encapsuled by styling tags + * Print a message to STDERR, optionally encapsulated by styling tags * * @param string $message * @param string $tag diff --git a/src/Contents.php b/src/Contents.php new file mode 100644 index 0000000..29f6883 --- /dev/null +++ b/src/Contents.php @@ -0,0 +1,154 @@ +prefix('* ') + * // or + * ->map(function($line){ return '* ' . $line; }) + */ +class Contents +{ + /** + * @var string[] + */ + protected $lines = []; + + /** + * @param ?string[] $lines + */ + public function __construct(array $lines = null) + { + $this->lines = $lines ?? []; + } + + /** + * Render a string representation of the contents + * + * @return string + */ + public function __toString(): string + { + return $this->join("\n"); + } + + /** + * Create a new instance from the given lines stack + * + * @param string[] $lines + * + * @return Contents + */ + public static function load(array $lines = null): Contents + { + return new static($lines); + } + + /** + * Create a new instance from the given file contents + * + * @param string $file + * + * @return Contents + */ + public static function open(string $file): Contents + { + $lines = file($file, FILE_IGNORE_NEW_LINES); + + return self::load($lines); + } + + /** + * Apply the given method to each line of the contents + * + * @param callable $callback + * + * @return self + */ + public function map(callable $callback): self + { + $this->lines = array_map($callback, $this->lines); + + return $this; + } + + /** + * Add a new line at the end of the contents + * + * @param string $line + * + * @return self + */ + public function append(string $line): self + { + $this->lines[] = $line; + + return $this; + } + + /** + * Insert a new line at the beginning of the contents + * + * @param string $line + * + * @return self + */ + public function prepend(string $line): self + { + array_unshift($this->lines, $line); + + return $this; + } + + /** + * Render a concatenated representation of the contents + * + * @param string $glue + * + * @return string + */ + public function join(string $glue = null): string + { + return implode($glue, $this->all()); + } + + /** + * Prefix each line of the contents with the given string + * + * @param string $prefix + * + * @return self + */ + public function prefix(string $prefix): self + { + return $this->map(function($line) use ($prefix) { return $prefix . $line; }); + } + + /** + * Getter for the contents stack + * + * @return string[] + */ + public function all(): array + { + return $this->lines ?? []; + } +} diff --git a/src/PharBuilder.php b/src/PharBuilder.php index 81de3e0..df8dcc2 100644 --- a/src/PharBuilder.php +++ b/src/PharBuilder.php @@ -53,7 +53,7 @@ class PharBuilder /** * Optional banner/legal notice * - * @var string + * @var string[] */ protected $banner; @@ -107,11 +107,11 @@ public function init(): self /** * Setter for the banner contents * - * @param string $banner + * @param string[] $banner * * @return self */ - public function setBanner(string $banner): self + public function setBanner(array $banner): self { $this->banner = $banner; @@ -166,22 +166,15 @@ public function list(): array /** * Generate the PHAR stub definition contents * - * @param string $main The main entrypoint script - * @param bool $shebang Whether to include the shebang line - * @param ?string $banner Optional legal notice text + * @param string $main The main entrypoint script + * @param bool $shebang Whether to include the shebang line + * @param ?string[] $banner Optional legal notice text * * @return string */ - protected function stub(string $main, bool $shebang = true, string $banner = null): string + protected function stub(string $main, bool $shebang = true, array $banner = null): string { - $lines = []; - if ($shebang) { - $lines[] = '#!/usr/bin/env php'; - } - $lines[] = 'pharname); // 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 $lines[] = sprintf('set_include_path("phar://%s/");', $this->pharname); $lines[] = sprintf('require "%s"; __HALT_COMPILER();', $main); + array_unshift($lines, '