Skip to content

Commit aa9db24

Browse files
committed
Implement --meta option
1 parent 382c058 commit aa9db24

File tree

5 files changed

+65
-5
lines changed

5 files changed

+65
-5
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ phpcc --version
3535
phpcc \
3636
-e <main> \
3737
-o <output> \
38-
[-d dir [-d dir ...]] \
39-
[-f file [-f file ...]] \
40-
[-b <banner>]
38+
[-d <dir> [-d <dir> ...]] \
39+
[-f <file> [-f <file> ...]] \
40+
[-b <banner>] \
41+
[-m <metadata> [-m <metadata> ...]]
4142
```
4243

4344
### Options/Arguments
@@ -51,6 +52,7 @@ Name / Shorthand | Type | Description
5152
`--banner`, `-b` | value | Specify the filepath to the legal notice banner<br/>_Will be included in the human-readable part of the stub._ |n
5253
`--file`, `-f` | multi | Adds a single file to the archive |n
5354
`--dir`, `-d` | multi | Adds a sources directory to the archive<br/>_Possible dir spec formats:<br/>- `$dir` => include all files in directory<br/>- `$dir:$extension` => filter files on a specific extension_ |n
55+
`--meta`, `-m` | multi | Adds a metadata to the archive<br/>_Metadata must be specified in the `$key:$value` format_ |n
5456

5557

5658
### Examples

build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ fi
2525

2626
sed -i "s/\$VERSION *=.*/\$VERSION = '$version';/" $main
2727

28-
php -dphar.readonly=0 bin/compile.php -d src:php -d vendor:php -e $main -o $phar -b .banner
28+
php -dphar.readonly=0 bin/compile.php -d src:php -d vendor:php -e $main -o $phar -b .banner -m license:MIT -m author:yannoff -m copyright:yannoff

doc/examples.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,13 @@ phpcc -e app.php -o foobar.phar -b LICENSE
3838
```bash
3939
phpcc -e app.php -o foobar.phar -f foo.php -f bar.php
4040
```
41+
42+
## Example 5: Adding metadata to the archive
43+
44+
- Define `app.php` as the stub main entrypoint script
45+
- Save compiled phar executable to `bin/acme`
46+
- Add the `license` & `author` metadata to the archive
47+
48+
```bash
49+
phpcc -e app.php -o bin/acme -m license:MIT -m author:yannoff
50+
```

src/Command/Compile.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function configure()
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")')
3939
->addOption('file', 'f', Option::MULTI, 'Add a single file to the archive')
40+
->addOption('meta', 'm', Option::MULTI, 'Add a metadata property (eg: "-m $key:$value")')
4041
->addOption('output', 'o', Option::VALUE, 'Set the compiled archive output name')
4142
->addOption('banner', 'b', Option::VALUE, 'Load legal notice from the given banner file')
4243
;
@@ -51,6 +52,7 @@ public function execute()
5152

5253
$dirs = $this->getOption('dir') ?? [];
5354
$files = $this->getOption('file') ?? [];
55+
$meta = $this->getOption('meta') ?? [];
5456

5557
$output = $this->getOption('output');
5658
$main = $this->getOption('main');
@@ -60,6 +62,7 @@ public function execute()
6062
->addFiles($files)
6163
->addDirectories($dirs)
6264
->setNotice($banner)
65+
->addMetadata($meta)
6366
->publish($output)
6467
->info('Build complete.')
6568
;
@@ -149,6 +152,25 @@ protected function addFiles(array $files): self
149152
return $this;
150153
}
151154

155+
/**
156+
* Add a list of metadata properties to the archive builder
157+
*
158+
* @param string[] $definitions A list of $key:$value pairs
159+
*
160+
* @return self
161+
*/
162+
protected function addMetadata(array $definitions): self
163+
{
164+
foreach ($definitions as $definition) {
165+
list($name, $value) = explode(':', $definition);
166+
$this->info("Adding <strong>$name</strong> metadata property");
167+
$this->info("-> $name: $value", 'grey');
168+
$this->builder->addMetadata($name, $value);
169+
}
170+
171+
return $this;
172+
}
173+
152174
/**
153175
* Add banner file contents to the archive builder
154176
*

src/PharBuilder.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ class PharBuilder
5757
*/
5858
protected $banner;
5959

60+
/**
61+
* Store the archive metadata properties
62+
*
63+
* @var array
64+
*/
65+
protected $metadata = [];
66+
6067
/**
6168
* PharBuilder factory method
6269
*
@@ -124,6 +131,8 @@ public function compile(string $output, string $compression = 'GZ')
124131
throw new LogicException("Main script {$this->main} contents must be added to the archive");
125132
}
126133

134+
$this->archive->setMetadata($this->metadata);
135+
127136
$c = constant('Phar::' . $compression);
128137
$this->archive->compressFiles($c);
129138

@@ -181,11 +190,28 @@ protected function stub(string $main, string $banner = null): string
181190
* @param string $file Path to the file
182191
* @param ?string $local Optional file alias
183192
* @param bool $minify Whether comments/spaces should be removed from contents
193+
*
194+
* @return self
184195
*/
185-
public function addFile(string $file, string $local = null, bool $minify = true)
196+
public function addFile(string $file, string $local = null, bool $minify = true): self
186197
{
187198
$this->archive->addFileContents($file, $local, $minify);
188199

189200
return $this;
190201
}
202+
203+
/**
204+
* Add or update an archive metadata entry
205+
*
206+
* @param string $name
207+
* @param ?mixed $value
208+
*
209+
* @return self
210+
*/
211+
public function addMetadata(string $name, $value = null): self
212+
{
213+
$this->metadata[$name] = $value;
214+
215+
return $this;
216+
}
191217
}

0 commit comments

Comments
 (0)