-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: Consolidate banner handling
- Loading branch information
Showing
5 changed files
with
212 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ?? []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = ' * '; | ||
} |