Skip to content

Commit 5dfa49b

Browse files
committed
Add --file multiple option
1 parent 5e0118f commit 5dfa49b

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ chmod +x ${BINDIR}/phpcc
2929
```
3030
phpcc --help
3131
phpcc --version
32-
phpcc -e <entrypoint> -o <output> [-d dir [-d dir ...]] [-b <banner>] [-l <license>]
32+
phpcc -e <entrypoint> -o <output> [-d dir [-d dir ...]] [-f file [-f file ...]] [-b <banner>]
3333
```
3434

3535
### Options/Arguments
@@ -44,9 +44,15 @@ phpcc -e <entrypoint> -o <output> [-d dir [-d dir ...]] [-b <banner>] [-l <licen
4444

4545
**MANDATORY** The Phar archive output file.
4646

47+
#### `-f`, `--file`
48+
49+
Adds a single file to the archive.
50+
51+
_Multiple values allowed here_
52+
4753
#### `-d`, `--dir`
4854

49-
Adds a PHP sources directory to the archive.
55+
Adds a sources directory to the archive.
5056

5157
_Multiple values allowed here_
5258

doc/examples.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,13 @@ phpcc -d src:php -d src:phtml -e main.php -o bin/foobar
2828
```bash
2929
phpcc -e app.php -o foobar.phar -b LICENSE
3030
```
31+
32+
## Example 4: Add sparse single PHP files
33+
34+
- Define `app.php` as the stub main entrypoint script
35+
- Save compiled phar executable to `foobar.phar`
36+
- Add `foo.php` and `bar.php` files to the archive
37+
38+
```bash
39+
phpcc -e app.php -o foobar.phar -f foo.php -f bar.php
40+
```

src/Command/Compile.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function configure()
3636
->setHelp('PHP Code compiler - Phar executable compiling utility')
3737
->addOption('main', 'e', Option::VALUE, 'Set the PHAR stub\'s main entrypoint script')
3838
->addOption('dir', 'd', Option::MULTI, 'Add directory contents ("-d $dir") optionally filtered on a specific file extension ("$dir:$extension")')
39+
->addOption('file', 'f', Option::MULTI, 'Add a single file to the archive')
3940
->addOption('output', 'o', Option::VALUE, 'Set the compiled archive output name')
4041
->addOption('banner', 'b', Option::VALUE, 'Load legal notice from the given banner file')
4142
;
@@ -49,11 +50,14 @@ public function execute()
4950
$banner = $this->getOption('banner') ?? '';
5051

5152
$dirs = $this->getOption('dir') ?? [];
53+
$files = $this->getOption('file') ?? [];
54+
5255
$output = $this->getOption('output');
5356
$main = $this->getOption('main');
5457

5558
$this
5659
->initBuilder($main)
60+
->addFiles($files)
5761
->addDirectories($dirs)
5862
->setNotice($banner)
5963
->publish($output)
@@ -96,7 +100,7 @@ protected function addDirectory(string $directory, string $extensions = null)
96100
/**
97101
* Add a single file to the archive builder
98102
*
99-
* @param string $file
103+
* @param string $file A relative or absolute file path
100104
*
101105
*/
102106
protected function addFile(string $file)
@@ -110,7 +114,7 @@ protected function addFile(string $file)
110114
/**
111115
* Add a list of directory specifications to the archive builder
112116
*
113-
* @param array $dirs A list of directories in the form "$dir" or "$dir:$extension"
117+
* @param string[] $dirs A list of specs in the form "$dir" or "$dir:$extension"
114118
*
115119
* @return self
116120
*/
@@ -120,14 +124,31 @@ protected function addDirectories(array $dirs): self
120124
list($directory, $extensions) = explode(':', $spec);
121125

122126
$wildcard = $extensions ? "*.$extensions" : 'all';
123-
$this->info("Scanning directory <strong>$directory</strong> for <strong>$wildcard</strong> files ...");
127+
$this->info("Scanning directory <strong>$directory</strong> for <strong>$wildcard</strong> files...");
124128

125129
$this->addDirectory($directory, $extensions);
126130
}
127131

128132
return $this;
129133
}
130134

135+
/**
136+
* Add a list of single files to the archive builder
137+
*
138+
* @param string[] $files A list of relative or absolute file paths
139+
*
140+
* @return self
141+
*/
142+
protected function addFiles(array $files): self
143+
{
144+
foreach ($files as $file) {
145+
$this->info("Adding single file <strong>$file</strong>...");
146+
$this->addFile($file);
147+
}
148+
149+
return $this;
150+
}
151+
131152
/**
132153
* Add banner file contents to the archive builder
133154
*
@@ -138,7 +159,7 @@ protected function addDirectories(array $dirs): self
138159
protected function setNotice(string $banner = null): self
139160
{
140161
if (is_file($banner)) {
141-
$this->info("Loading banner contents from <strong>$banner</strong> file ...");
162+
$this->info("Loading banner contents from <strong>$banner</strong> file...");
142163
$contents = file_get_contents($banner);
143164
$header = $this->phpdocize($contents);
144165

@@ -159,7 +180,7 @@ protected function setNotice(string $banner = null): self
159180
*/
160181
protected function publish(string $output, string $compression = 'GZ'): self
161182
{
162-
$this->info("Writing Phar archive to <strong>$output</strong> ...");
183+
$this->info("Writing Phar archive to <strong>$output</strong>...");
163184
$this->builder->compile($output, $compression);
164185

165186
return $this;

0 commit comments

Comments
 (0)