Skip to content

Commit

Permalink
WIP Implement Contents class
Browse files Browse the repository at this point in the history
  • Loading branch information
yannoff committed Apr 13, 2024
1 parent 7c2ec6b commit cac3ea6
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 17 deletions.
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
26 changes: 12 additions & 14 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\PHPComment;

class Compile extends Command
{
Expand Down Expand Up @@ -196,8 +198,7 @@ protected function setNotice(string $banner = null): self
{
if (is_file($banner)) {
$this->info("Loading banner contents from <strong>$banner</strong> file...");
$contents = file($banner, FILE_IGNORE_NEW_LINES);
$header = $this->phpdocize($contents);
$header = $this->phpdocize($banner);

$this->info(implode("\n", $header), 'grey');
$this->builder->setBanner($header);
Expand Down Expand Up @@ -264,25 +265,22 @@ protected function require(string $option)
/**
* Return the contents wrapped in a comments block
*
* @param string[] $contents
* @param string $banner
*
* @return string[]
*/
protected function phpdocize(array $contents): array
protected function phpdocize(string $banner): array
{
$lines = array_map(
function($line) { return sprintf(' * %s', $line); },
$contents
);

array_unshift($lines, '/**');
array_push($lines, ' */');

return $lines;
return Contents::file($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
143 changes: 143 additions & 0 deletions src/Contents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?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;

/**
* Example:
*
* $comments = Contents::load($banner)
* ->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 ?? [];
}

/**
* 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 file(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->lines);
}

/**
* 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 ?? [];
}
}
10 changes: 10 additions & 0 deletions src/PHPComment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Yannoff\PhpCodeCompiler;

class PHPComment
{
const OPEN = '/**';
const CLOSE = ' */';
const STAR = ' * ';
}

0 comments on commit cac3ea6

Please sign in to comment.