Skip to content

Instantly share code, notes, and snippets.

@liuxd
Created September 7, 2017 01:09
Show Gist options
  • Select an option

  • Save liuxd/a16953dfeb25485bf0b574c009b7837d to your computer and use it in GitHub Desktop.

Select an option

Save liuxd/a16953dfeb25485bf0b574c009b7837d to your computer and use it in GitHub Desktop.
phar-packer.php
#!/usr/bin/env php
<?php
// php.ini : `phar.readonly = Off`
$longopts = array(
'name::',
'path::',
'init:'
);
$opts = getopt('', $longopts);
$name = (isset($opts['name'])) ? $opts['name'] . '.phar' : '';
$path = (isset($opts['path'])) ? $opts['path'] : '';
$init = (isset($opts['init'])) ? $opts['init'] : 'index.php';
if (empty($name) && empty($path)) {
echo <<<HELP
phar packer 1.0
eg : php phar-packer.php --name=test --path=/project/to/ --init=index.php
--name the name of the .phar file.
--path the path of the project.
--init the init file.
HELP;
echo PHP_EOL;
return;
}
if (empty($name) || empty($path)) {
echo 'Options invalid!' . PHP_EOL;
return;
}
$phar = new Phar($name, 0);
$phar->buildFromDirectory($path);
$stub = <<< EOF
<?php
Phar::mapPhar('{$name}');
require 'phar://{$name}/{$init}';
__HALT_COMPILER();
EOF;
$phar->setStub($stub);
$phar->compressFiles(Phar::GZ);
$phar->stopBuffering();
# end of this file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment