Skip to content

Commit

Permalink
Refactor: Rework directories/files handling
Browse files Browse the repository at this point in the history
Directory scan is now processed at command level

- Remove `PharBuilder::addContentsFromDirectory()` &  `Phar::addDirectory()` methods
- Add `PharBuilder::addFile()`, `Compile::addFile()` &  `Compile::addDirectory()` methods
  • Loading branch information
yannoff committed Mar 2, 2024
1 parent 77b9bbe commit 6b10898
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 45 deletions.
46 changes: 33 additions & 13 deletions src/Command/Compile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 <strong>$directory</strong> for <strong>$wildcard</strong> 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"
*
Expand All @@ -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 <strong>$dir</strong> for <strong>$wildcard</strong> 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
*
Expand Down
16 changes: 0 additions & 16 deletions src/Phar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
30 changes: 14 additions & 16 deletions src/PharBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 6b10898

Please sign in to comment.