2017-01-09 01:24:59 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MakePhar;
|
|
|
|
|
|
|
|
class PharBuilder
|
|
|
|
{
|
2017-01-10 12:50:11 +00:00
|
|
|
protected $tempFile;
|
2017-01-09 01:24:59 +00:00
|
|
|
|
|
|
|
protected $manifest;
|
|
|
|
|
|
|
|
protected $files = [];
|
|
|
|
|
|
|
|
protected $phar;
|
|
|
|
|
|
|
|
public function __construct(Manifest $manifest)
|
|
|
|
{
|
|
|
|
$this->manifest = $manifest;
|
|
|
|
}
|
|
|
|
|
2017-01-10 12:50:11 +00:00
|
|
|
/**
|
|
|
|
* Collect files and prepare to build the .phar
|
|
|
|
*
|
|
|
|
*/
|
2017-01-09 01:24:59 +00:00
|
|
|
public function prepare()
|
|
|
|
{
|
|
|
|
log_debug("Finding files to add...");
|
|
|
|
$this->files = $this->manifest->findSourceFiles();
|
|
|
|
log_debug("Found %d files", count($this->files));
|
2017-01-10 12:50:11 +00:00
|
|
|
|
|
|
|
$this->tempFile = "/tmp/".uniqid("phar").".phar";
|
2017-01-09 01:24:59 +00:00
|
|
|
}
|
|
|
|
|
2017-01-10 12:50:11 +00:00
|
|
|
/**
|
|
|
|
* Build the .phar archive
|
|
|
|
*
|
|
|
|
*/
|
2017-01-09 01:24:59 +00:00
|
|
|
public function build()
|
|
|
|
{
|
|
|
|
// Create the phar
|
2017-01-10 12:50:11 +00:00
|
|
|
$phar = new \Phar($this->tempFile);
|
2017-01-10 14:13:49 +00:00
|
|
|
|
|
|
|
// verbatim add
|
|
|
|
$verbatim = $this->manifest->getVerbatim();
|
|
|
|
if ($verbatim) {
|
|
|
|
log_debug("Note: Creating verbatim archive without minification");
|
|
|
|
}
|
2017-01-09 01:24:59 +00:00
|
|
|
|
|
|
|
// Add files
|
|
|
|
log_debug("Adding files to phar archive...");
|
2017-01-10 00:34:08 +00:00
|
|
|
$t = count($this->files); $i = 0; $lp = null;
|
|
|
|
$phar->startBuffering();
|
2017-01-10 12:50:11 +00:00
|
|
|
$size_tot = 0; $size_min = 0;
|
2017-01-09 01:24:59 +00:00
|
|
|
foreach ($this->files as $file) {
|
2017-01-10 00:34:08 +00:00
|
|
|
$i++;
|
|
|
|
$tp = dirname($file->src);
|
2017-01-10 14:13:49 +00:00
|
|
|
if (($tp!=$lp) && (IS_TTY)) {
|
2017-01-10 00:34:08 +00:00
|
|
|
$lp=$tp;
|
|
|
|
printf("\r\e[K%d/%d %s", $i, $t, $tp);
|
|
|
|
}
|
2017-01-10 14:13:49 +00:00
|
|
|
|
|
|
|
if (fnmatch("*.php",$file->src) && (!$verbatim)) {
|
2017-01-10 12:50:11 +00:00
|
|
|
$min = php_strip_whitespace($file->src);
|
|
|
|
$size_tot += filesize($file->src);
|
|
|
|
$size_min += strlen($min);
|
|
|
|
$phar->addFromString($file->dest?:$file->src, $min);
|
2017-01-10 00:34:08 +00:00
|
|
|
} else {
|
2017-01-10 14:13:49 +00:00
|
|
|
$size_tot += filesize($file->src);
|
|
|
|
$size_min += filesize($file->src);
|
2017-01-10 00:34:08 +00:00
|
|
|
$phar->addFile($file->dest?:$file->src, $file->src);
|
|
|
|
}
|
2017-01-09 01:24:59 +00:00
|
|
|
}
|
2017-01-10 14:13:49 +00:00
|
|
|
(IS_TTY) && printf("\r\e[K");
|
2017-01-10 00:34:08 +00:00
|
|
|
$phar->stopBuffering();
|
2017-01-09 01:24:59 +00:00
|
|
|
|
2017-01-10 12:53:54 +00:00
|
|
|
// Helper to format sizes
|
2017-01-10 12:50:11 +00:00
|
|
|
$formatSize = function ($s) {
|
|
|
|
$u = [ 'B', 'KB', 'MB', 'GB' ];
|
|
|
|
while (count($u)>1 && ($s > 1024)) { $s /= 1024; array_shift($u); }
|
|
|
|
return sprintf("%.2f%s", $s, array_shift($u));
|
|
|
|
};
|
2017-01-10 12:53:54 +00:00
|
|
|
// Format the status
|
2017-01-10 12:50:11 +00:00
|
|
|
$str_tot = $formatSize($size_tot);
|
2017-01-10 14:13:49 +00:00
|
|
|
if (!$verbatim) {
|
|
|
|
$str_min = $formatSize($size_min);
|
|
|
|
$str_rat = sprintf("%.2f%%", 100/$str_tot*$str_min);
|
|
|
|
log_debug("Size: %s, Minified: %s (%s)", $str_tot, $str_min, $str_rat);
|
|
|
|
} else {
|
|
|
|
log_debug("Size: %s", $str_tot);
|
|
|
|
}
|
2017-01-10 12:50:11 +00:00
|
|
|
|
2017-01-09 01:24:59 +00:00
|
|
|
// Create stub
|
|
|
|
if ($this->manifest->getIsLibrary()) {
|
|
|
|
log_debug("Creating library stub...");
|
|
|
|
// Create library stub
|
|
|
|
$stub = '<?php ';
|
|
|
|
$stub.= 'require_once __DIR__."/vendor/autoload.php";';
|
2017-01-10 14:13:49 +00:00
|
|
|
foreach ($this->manifest->getProps() as $k=>$v) {
|
|
|
|
$stub.= sprintf('define(%s,%s);', var_export($k,true), var_export($v,true));
|
|
|
|
}
|
2017-01-10 00:34:08 +00:00
|
|
|
if (($stubfile = $this->manifest->getStubFile())) {
|
|
|
|
$stub.= 'require_once __DIR__."/'.ltrim($stubfile,'/').'";';
|
|
|
|
}
|
2017-01-09 01:24:59 +00:00
|
|
|
$phar->addFromString("index.php", $stub);
|
|
|
|
} else {
|
|
|
|
log_debug("Creating application stub...");
|
|
|
|
// Write application stub
|
|
|
|
$stubfile = $this->manifest->getStubFile();
|
2017-01-10 14:13:49 +00:00
|
|
|
$stub = '<?php ';
|
|
|
|
foreach ($this->manifest->getProps() as $k=>$v) {
|
|
|
|
$stub.= sprintf('define(%s,%s);', var_export($k,true), var_export($v,true));
|
|
|
|
}
|
|
|
|
$stub.= 'require_once __DIR__."/'.ltrim($stubfile,'/').'";';
|
2017-01-09 01:24:59 +00:00
|
|
|
$phar->addFromString("index.php", $stub);
|
|
|
|
// Make the phar stub executable
|
|
|
|
$mainstub = $phar->createDefaultStub("index.php");
|
2017-01-10 00:34:08 +00:00
|
|
|
$tmp = "/tmp/".uniqid("stub"); file_put_contents($tmp, $mainstub);
|
|
|
|
$mainstub = php_strip_whitespace($tmp);
|
|
|
|
unlink($tmp);
|
2017-01-09 01:24:59 +00:00
|
|
|
$mainstub = "#!/usr/bin/env php\n{$mainstub}";
|
|
|
|
$phar->setStub($mainstub);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the phar
|
|
|
|
$phar = null;
|
|
|
|
}
|
|
|
|
|
2017-01-10 12:50:11 +00:00
|
|
|
/**
|
|
|
|
* Move the built .phar into its target location.
|
|
|
|
*
|
|
|
|
*/
|
2017-01-09 01:24:59 +00:00
|
|
|
public function finalize()
|
|
|
|
{
|
|
|
|
$output = $this->manifest->getOutput();
|
|
|
|
|
|
|
|
if (file_exists($output)) {
|
|
|
|
unlink($output);
|
|
|
|
}
|
2017-01-10 14:13:49 +00:00
|
|
|
log_info("Writing %s", $output);
|
2017-01-10 12:50:11 +00:00
|
|
|
rename($this->tempFile, $output);
|
2017-01-09 01:24:59 +00:00
|
|
|
|
|
|
|
if (!$this->manifest->getIsLibrary()) {
|
|
|
|
chmod($output, 0777);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|