From 6b108984b8345d0e63a2721f10cd20746ab6dd6c Mon Sep 17 00:00:00 2001 From: Yannoff Date: Sat, 2 Mar 2024 11:30:28 +0100 Subject: [PATCH] Refactor: Rework directories/files handling Directory scan is now processed at command level - Remove `PharBuilder::addContentsFromDirectory()` & `Phar::addDirectory()` methods - Add `PharBuilder::addFile()`, `Compile::addFile()` & `Compile::addDirectory()` methods --- src/Command/Compile.php | 46 +++++++++++++++++++++++++++++------------ src/Phar.php | 16 -------------- src/PharBuilder.php | 30 +++++++++++++-------------- 3 files changed, 47 insertions(+), 45 deletions(-) diff --git a/src/Command/Compile.php b/src/Command/Compile.php index 0ec0774..aa3ba6f 100644 --- a/src/Command/Compile.php +++ b/src/Command/Compile.php @@ -17,6 +17,7 @@ use Yannoff\Component\Console\Command; use Yannoff\Component\Console\Definition\Option; +use Yannoff\PhpCodeCompiler\Directory; use Yannoff\PhpCodeCompiler\PharBuilder; class Compile extends Command @@ -76,7 +77,36 @@ protected function initBuilder(string $main): self } /** - * Add a list of directory specifications to the Phar builder + * Add files found in the directory to the builder, optionally filtered by extension + * + * @param string $directory The directory to scan for contents + * @param ?string $extensions Filter on extension, may be "php" or "(php|phtml)" + * + */ + protected function addDirectory(string $directory, string $extensions = null) + { + $wildcard = $extensions ? "*.$extensions" : 'all'; + $this->info("Scanning directory $directory for $wildcard files ..."); + + $filter = ($extensions) ? sprintf('/\.%s$/', $extensions) : ''; + $files = Directory::find($directory, $filter); + + array_walk($files, function ($file) { $this->addFile($file); }); + } + + /** + * Add a single file to the archive builder + * + * @param string $file + */ + protected function addFile(string $file) + { + $this->info('+ ' . $file, 'grey'); + $this->builder->addFile($file); + } + + /** + * Add a list of directory specifications to the archive builder * * @param array $dirs A list of directories in the form "$dir" or "$dir:$extension" * @@ -86,24 +116,14 @@ protected function addDirectories(array $dirs): self { foreach ($dirs as $spec) { list($dir, $ext) = explode(':', $spec); - $wildcard = $ext ? "*.$ext" : 'all'; - $this->info("Scanning directory $dir for $wildcard files ..."); - $this->builder->addContentsFromDirectory($dir, $ext); + $this->addDirectory($dir, $ext); } - $this->info( - implode("\n", array_map( - function ($f) { return sprintf('+ %s', $f); }, - $this->builder->list() - )), - 'grey' - ); - return $this; } /** - * Add banner file contents to the Phar builder + * Add banner file contents to the archive builder * * @param ?string $banner Path to the banner file * diff --git a/src/Phar.php b/src/Phar.php index d340f4a..eca7181 100644 --- a/src/Phar.php +++ b/src/Phar.php @@ -21,22 +21,6 @@ class Phar extends BuiltinPhar { public $files = []; - /** - * Add the contents of a directory in the archive - * - * @param string $directory The directory to be included - * @param string $filter Optional filter on file extensions - * - * @return self - */ - public function addDirectory(string $directory, string $filter = ''): self - { - $files = Directory::find($directory, $filter); - array_walk($files, function ($file) { $this->addFileContents($file); }); - - return $this; - } - /** * Similar to Phar::addFile(), with optional minifying * diff --git a/src/PharBuilder.php b/src/PharBuilder.php index a064c02..346fb59 100644 --- a/src/PharBuilder.php +++ b/src/PharBuilder.php @@ -110,22 +110,6 @@ public function setBanner(string $banner): self return $this; } - /** - * Add all files in the directory tree, optionally filtered by extension - * - * @param string $directory The directory to scan for contents - * @param ?string $extensions Filter on extension, may be "php" or "(php|phtml)" - * - * @return self - */ - public function addContentsFromDirectory(string $directory, string $extensions = null): self - { - $filter = ($extensions) ? sprintf('/\.%s$/', $extensions) : ''; - $this->archive->addDirectory($directory, $filter); - - return $this; - } - /** * Compress files, generate the stub and save PHAR to the output file * @@ -184,4 +168,18 @@ protected function stub(string $main, string $banner = null): string return implode(self::EOL, $lines); } + + /** + * Add a single file to the archive, optionally minified + * + * @param string $file Path to the file + * @param ?string $local Optional file alias + * @param bool $minify Whether comments/spaces should be removed from contents + */ + public function addFile(string $file, string $local = null, bool $minify = true) + { + $this->archive->addFileContents($file, $local, $minify); + + return $this; + } }