Skip to content

Commit

Permalink
(WIP) Rework directories/files handling
Browse files Browse the repository at this point in the history
Directory scan is now handled at command level
Compile
- Add `Compile::addFile()`
- Add `Compile::addDirectory()`

PharBuilder
- Add `addFile()`
- Remove `addContentsFromDirectory()`

Phar
- Remove `addDirectory()`
  • Loading branch information
yannoff committed Mar 2, 2024
1 parent 77b9bbe commit 67bcf59
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 37 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
14 changes: 6 additions & 8 deletions src/PharBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,15 @@ public function setBanner(string $banner): self
}

/**
* Add all files in the directory tree, optionally filtered by extension
* Add a single file to the archive, optionally minified
*
* @param string $directory The directory to scan for contents
* @param ?string $extensions Filter on extension, may be "php" or "(php|phtml)"
*
* @return self
* @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 addContentsFromDirectory(string $directory, string $extensions = null): self
public function addFile(string $file, string $local = null, bool $minify = true)
{
$filter = ($extensions) ? sprintf('/\.%s$/', $extensions) : '';
$this->archive->addDirectory($directory, $filter);
$this->archive->addFileContents($file, $local, $minify);

return $this;
}
Expand Down

0 comments on commit 67bcf59

Please sign in to comment.