Skip to content

Commit 6b10898

Browse files
committed
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
1 parent 77b9bbe commit 6b10898

File tree

3 files changed

+47
-45
lines changed

3 files changed

+47
-45
lines changed

src/Command/Compile.php

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
use Yannoff\Component\Console\Command;
1919
use Yannoff\Component\Console\Definition\Option;
20+
use Yannoff\PhpCodeCompiler\Directory;
2021
use Yannoff\PhpCodeCompiler\PharBuilder;
2122

2223
class Compile extends Command
@@ -76,7 +77,36 @@ protected function initBuilder(string $main): self
7677
}
7778

7879
/**
79-
* Add a list of directory specifications to the Phar builder
80+
* Add files found in the directory to the builder, optionally filtered by extension
81+
*
82+
* @param string $directory The directory to scan for contents
83+
* @param ?string $extensions Filter on extension, may be "php" or "(php|phtml)"
84+
*
85+
*/
86+
protected function addDirectory(string $directory, string $extensions = null)
87+
{
88+
$wildcard = $extensions ? "*.$extensions" : 'all';
89+
$this->info("Scanning directory <strong>$directory</strong> for <strong>$wildcard</strong> files ...");
90+
91+
$filter = ($extensions) ? sprintf('/\.%s$/', $extensions) : '';
92+
$files = Directory::find($directory, $filter);
93+
94+
array_walk($files, function ($file) { $this->addFile($file); });
95+
}
96+
97+
/**
98+
* Add a single file to the archive builder
99+
*
100+
* @param string $file
101+
*/
102+
protected function addFile(string $file)
103+
{
104+
$this->info('+ ' . $file, 'grey');
105+
$this->builder->addFile($file);
106+
}
107+
108+
/**
109+
* Add a list of directory specifications to the archive builder
80110
*
81111
* @param array $dirs A list of directories in the form "$dir" or "$dir:$extension"
82112
*
@@ -86,24 +116,14 @@ protected function addDirectories(array $dirs): self
86116
{
87117
foreach ($dirs as $spec) {
88118
list($dir, $ext) = explode(':', $spec);
89-
$wildcard = $ext ? "*.$ext" : 'all';
90-
$this->info("Scanning directory <strong>$dir</strong> for <strong>$wildcard</strong> files ...");
91-
$this->builder->addContentsFromDirectory($dir, $ext);
119+
$this->addDirectory($dir, $ext);
92120
}
93121

94-
$this->info(
95-
implode("\n", array_map(
96-
function ($f) { return sprintf('+ %s', $f); },
97-
$this->builder->list()
98-
)),
99-
'grey'
100-
);
101-
102122
return $this;
103123
}
104124

105125
/**
106-
* Add banner file contents to the Phar builder
126+
* Add banner file contents to the archive builder
107127
*
108128
* @param ?string $banner Path to the banner file
109129
*

src/Phar.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,6 @@ class Phar extends BuiltinPhar
2121
{
2222
public $files = [];
2323

24-
/**
25-
* Add the contents of a directory in the archive
26-
*
27-
* @param string $directory The directory to be included
28-
* @param string $filter Optional filter on file extensions
29-
*
30-
* @return self
31-
*/
32-
public function addDirectory(string $directory, string $filter = ''): self
33-
{
34-
$files = Directory::find($directory, $filter);
35-
array_walk($files, function ($file) { $this->addFileContents($file); });
36-
37-
return $this;
38-
}
39-
4024
/**
4125
* Similar to Phar::addFile(), with optional minifying
4226
*

src/PharBuilder.php

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -110,22 +110,6 @@ public function setBanner(string $banner): self
110110
return $this;
111111
}
112112

113-
/**
114-
* Add all files in the directory tree, optionally filtered by extension
115-
*
116-
* @param string $directory The directory to scan for contents
117-
* @param ?string $extensions Filter on extension, may be "php" or "(php|phtml)"
118-
*
119-
* @return self
120-
*/
121-
public function addContentsFromDirectory(string $directory, string $extensions = null): self
122-
{
123-
$filter = ($extensions) ? sprintf('/\.%s$/', $extensions) : '';
124-
$this->archive->addDirectory($directory, $filter);
125-
126-
return $this;
127-
}
128-
129113
/**
130114
* Compress files, generate the stub and save PHAR to the output file
131115
*
@@ -184,4 +168,18 @@ protected function stub(string $main, string $banner = null): string
184168

185169
return implode(self::EOL, $lines);
186170
}
171+
172+
/**
173+
* Add a single file to the archive, optionally minified
174+
*
175+
* @param string $file Path to the file
176+
* @param ?string $local Optional file alias
177+
* @param bool $minify Whether comments/spaces should be removed from contents
178+
*/
179+
public function addFile(string $file, string $local = null, bool $minify = true)
180+
{
181+
$this->archive->addFileContents($file, $local, $minify);
182+
183+
return $this;
184+
}
187185
}

0 commit comments

Comments
 (0)