Added composer extension checks to phar bootstrap

This commit is contained in:
Chris 2020-10-29 01:38:30 +01:00
parent dd0e6c6b58
commit 113ca68bd9

View File

@ -26,6 +26,8 @@ class Builder
protected $bins;
protected $dependencies = [];
private static $default_options = [
'install' => false,
];
@ -55,7 +57,19 @@ class Builder
$json = file_get_contents($this->path."/composer.json");
$composer = json_decode($json);
$composer = json_decode($json);
if (isset($composer->require)) {
foreach ($composer->require as $package=>$constraint) {
if ($package == "php") {
$this->dependencies[$package] = $constraint;
print_info(" ! require php: %s", $constraint);
} elseif (strpos($package, "ext-") === 0) {
$this->dependencies[$package] = $constraint;
print_info(" ! require ext: %s %s", $package, $constraint);
}
}
}
if (!isset($composer->bin) || !is_array($composer->bin)) {
print_warn("No executable defined, building a library phar...");
@ -159,10 +173,22 @@ class Builder
$bins = (array)$bins;
$indexFile = "<?php\n";
$depcheck = null;
foreach ($this->dependencies as $dep=>$constraint) {
if ($dep == "php") {
} elseif (strpos($dep, "ext-") === 0) {
$ext = substr($dep, 4);
$depcheck.= 'if (!extension_loaded("' . $ext . '")) { @fwrite(STDERR, "Fatal: Missing required extension ' . $ext . '.\n"); exit(2); }' . PHP_EOL;
}
}
if (!$index && count($bins)==0) {
$indexFile = '<?php require_once __DIR__."/vendor/autoload.php";';
$indexFile.= 'require_once __DIR__."/vendor/autoload.php";';
} elseif (count($bins)>1) {
$indexFile = '<?php $bin=basename($argv[0],".phar"); switch($bin) {';
$indexFile.= '$bin=basename($argv[0],".phar"); switch($bin) {';
for ($n = 1; $n<count($bins); $n++) {
$indexFile.= 'case '.var_export(basename($bins[$n]),true).': require_once __DIR__."/".'.var_export($bins[$n],true).'; break;';
}
@ -183,6 +209,8 @@ class Builder
$index = "bootstrap.php";
}
$indexFile = str_replace("<?php", "<?php ".$depcheck, $indexFile);
$stub = "#!/usr/bin/env php\n".$phar->createDefaultStub($index);
$phar->setStub($stub);