diff --git a/README.md b/README.md index fff7d99..ec6f0a8 100644 --- a/README.md +++ b/README.md @@ -35,9 +35,10 @@ phpcc --version phpcc \ -e
\ -o \ - [-d dir [-d dir ...]] \ - [-f file [-f file ...]] \ - [-b ] + [-d [-d ...]] \ + [-f [-f ...]] \ + [-b ] \ + [-m [-m ...]] ``` ### Options/Arguments @@ -51,6 +52,7 @@ Name / Shorthand | Type | Description `--banner`, `-b` | value | Specify the filepath to the legal notice banner
_Will be included in the human-readable part of the stub._ |n `--file`, `-f` | multi | Adds a single file to the archive |n `--dir`, `-d` | multi | Adds a sources directory to the archive
_Possible dir spec formats:
- `$dir` => include all files in directory
- `$dir:$extension` => filter files on a specific extension_ |n +`--meta`, `-m` | multi | Adds a metadata to the archive
_Metadata must be specified in the `$key:$value` format_ |n ### Examples diff --git a/build.sh b/build.sh index 9bc20d1..f3dad9e 100755 --- a/build.sh +++ b/build.sh @@ -25,4 +25,4 @@ fi sed -i "s/\$VERSION *=.*/\$VERSION = '$version';/" $main -php -dphar.readonly=0 bin/compile.php -d src:php -d vendor:php -e $main -o $phar -b .banner +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 diff --git a/doc/examples.md b/doc/examples.md index 67b8d23..61c8cf9 100644 --- a/doc/examples.md +++ b/doc/examples.md @@ -38,3 +38,13 @@ phpcc -e app.php -o foobar.phar -b LICENSE ```bash phpcc -e app.php -o foobar.phar -f foo.php -f bar.php ``` + +## Example 5: Adding metadata to the archive + +- Define `app.php` as the stub main entrypoint script +- Save compiled phar executable to `bin/acme` +- Add the `license` & `author` metadata to the archive + +```bash +phpcc -e app.php -o bin/acme -m license:MIT -m author:yannoff +``` diff --git a/src/Command/Compile.php b/src/Command/Compile.php index c6d27b8..781bb28 100644 --- a/src/Command/Compile.php +++ b/src/Command/Compile.php @@ -37,6 +37,7 @@ public function configure() ->addOption('main', 'e', Option::VALUE, 'Set the PHAR stub\'s main entrypoint script') ->addOption('dir', 'd', Option::MULTI, 'Add directory contents ("-d $dir") optionally filtered on a specific file extension ("$dir:$extension")') ->addOption('file', 'f', Option::MULTI, 'Add a single file to the archive') + ->addOption('meta', 'm', Option::MULTI, 'Add a metadata property (eg: "-m $key:$value")') ->addOption('output', 'o', Option::VALUE, 'Set the compiled archive output name') ->addOption('banner', 'b', Option::VALUE, 'Load legal notice from the given banner file') ; @@ -51,6 +52,7 @@ public function execute() $dirs = $this->getOption('dir') ?? []; $files = $this->getOption('file') ?? []; + $meta = $this->getOption('meta') ?? []; $output = $this->getOption('output'); $main = $this->getOption('main'); @@ -60,6 +62,7 @@ public function execute() ->addFiles($files) ->addDirectories($dirs) ->setNotice($banner) + ->addMetadata($meta) ->publish($output) ->info('Build complete.') ; @@ -149,6 +152,25 @@ protected function addFiles(array $files): self return $this; } + /** + * Add a list of metadata properties to the archive builder + * + * @param string[] $definitions A list of $key:$value pairs + * + * @return self + */ + protected function addMetadata(array $definitions): self + { + foreach ($definitions as $definition) { + list($name, $value) = explode(':', $definition); + $this->info("Adding $name metadata property"); + $this->info("-> $name: $value", 'grey'); + $this->builder->addMetadata($name, $value); + } + + return $this; + } + /** * Add banner file contents to the archive builder * diff --git a/src/PharBuilder.php b/src/PharBuilder.php index 4a29839..6266764 100644 --- a/src/PharBuilder.php +++ b/src/PharBuilder.php @@ -57,6 +57,13 @@ class PharBuilder */ protected $banner; + /** + * Store the archive metadata properties + * + * @var array + */ + protected $metadata = []; + /** * PharBuilder factory method * @@ -124,6 +131,8 @@ public function compile(string $output, string $compression = 'GZ') throw new LogicException("Main script {$this->main} contents must be added to the archive"); } + $this->archive->setMetadata($this->metadata); + $c = constant('Phar::' . $compression); $this->archive->compressFiles($c); @@ -181,11 +190,28 @@ protected function stub(string $main, string $banner = null): string * @param string $file Path to the file * @param ?string $local Optional file alias * @param bool $minify Whether comments/spaces should be removed from contents + * + * @return self */ - public function addFile(string $file, string $local = null, bool $minify = true) + public function addFile(string $file, string $local = null, bool $minify = true): self { $this->archive->addFileContents($file, $local, $minify); return $this; } + + /** + * Add or update an archive metadata entry + * + * @param string $name + * @param ?mixed $value + * + * @return self + */ + public function addMetadata(string $name, $value = null): self + { + $this->metadata[$name] = $value; + + return $this; + } }