Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Consolidate banner handling #19

Merged
merged 1 commit into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .banner
Original file line number Diff line number Diff line change
@@ -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
32 changes: 15 additions & 17 deletions src/Command/Compile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -196,10 +198,9 @@ protected function setNotice(string $banner = null): self
{
if (is_file($banner)) {
$this->info("Loading banner contents from <strong>$banner</strong> 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);
}

Expand Down Expand Up @@ -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
Expand Down
154 changes: 154 additions & 0 deletions src/Contents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?php

/**
* This file is part of the PHP Code Compiler project
*
* Copyright (c) Yannoff (https://github.com/yannoff)
*
* @project PHP Code Compiler (yannoff/phpcc)
* @homepage https://github.com/yannoff/phpcc
* @license https://github.com/yannoff/phpcc/blob/main/LICENSE
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace Yannoff\PhpCodeCompiler;

/**
* Multi-line text manipulation helper class
*
* Usage example:
*
* $comments = Contents::open('banner.txt')
* ->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 ?? [];
}
}
29 changes: 14 additions & 15 deletions src/PharBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class PharBuilder
/**
* Optional banner/legal notice
*
* @var string
* @var string[]
*/
protected $banner;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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[] = '<?php';
if ($banner) {
$lines[] = $banner;
}
$lines = $banner ?? [];
$lines[] = sprintf('// Compiled with PHP version %s', PHP_VERSION);
$lines[] = sprintf('Phar::mapPhar("%s");', $this->pharname);
// Add support for builtin phar flavoured require "vendor/autoload.php"
Expand All @@ -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, '<?php');

if ($shebang) {
array_unshift($lines, '#!/usr/bin/env php');
}

return implode(self::EOL, $lines);
}

Expand Down
26 changes: 26 additions & 0 deletions src/Syntax/PHPComment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/**
* This file is part of the PHP Code Compiler project
*
* Copyright (c) Yannoff (https://github.com/yannoff)
*
* @project PHP Code Compiler (yannoff/phpcc)
* @homepage https://github.com/yannoff/phpcc
* @license https://github.com/yannoff/phpcc/blob/main/LICENSE
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace Yannoff\PhpCodeCompiler\Syntax;

/**
* PHP Comment tokens string representations
*/
class PHPComment
{
const OPEN = '/**';
const CLOSE = ' */';
const STAR = ' * ';
}