mirror of
https://github.com/crazywhalecc/static-php-cli.git
synced 2026-07-03 14:55:39 +08:00
refactor command as more easily
This commit is contained in:
@@ -7,32 +7,28 @@ namespace SPC\command;
|
||||
use CliHelper\Tools\ArgFixer;
|
||||
use CliHelper\Tools\DataProvider;
|
||||
use CliHelper\Tools\SeekableArrayIterator;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Helper\ProgressBar;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
/** @noinspection PhpUnused */
|
||||
#[AsCommand('deploy', 'Deploy static-php-cli self to an .phar application')]
|
||||
class DeployCommand extends BaseCommand
|
||||
{
|
||||
protected static $defaultName = 'deploy';
|
||||
|
||||
public function configure()
|
||||
{
|
||||
$this->setDescription('Deploy static-php-cli self to an .phar application');
|
||||
$this->addArgument('target', InputArgument::OPTIONAL, 'The file or directory to pack.');
|
||||
$this->addOption('auto-phar-fix', null, InputOption::VALUE_NONE, 'Automatically fix ini option.');
|
||||
$this->addOption('overwrite', 'W', InputOption::VALUE_NONE, 'Overwrite existing files.');
|
||||
}
|
||||
|
||||
public function execute(InputInterface $input, OutputInterface $output): int
|
||||
public function handle(): int
|
||||
{
|
||||
// 第一阶段流程:如果没有写path,将会提示输入要打包的path
|
||||
$prompt = new ArgFixer($input, $output);
|
||||
$prompt = new ArgFixer($this->input, $this->output);
|
||||
// 首先得确认是不是关闭了readonly模式
|
||||
if (ini_get('phar.readonly') == 1) {
|
||||
if ($input->getOption('auto-phar-fix')) {
|
||||
if ($this->getOption('auto-phar-fix')) {
|
||||
$ask = true;
|
||||
} else {
|
||||
$ask = $prompt->requireBool('<comment>pack command needs "phar.readonly" = "Off" !</comment>' . PHP_EOL . 'If you want to automatically set it and continue, just Enter', true);
|
||||
@@ -41,12 +37,12 @@ class DeployCommand extends BaseCommand
|
||||
global $argv;
|
||||
$args = array_merge(['-d', 'phar.readonly=0'], $_SERVER['argv']);
|
||||
if (function_exists('pcntl_exec')) {
|
||||
$output->writeln('<info>Changing to phar.readonly=0 mode ...</info>');
|
||||
$this->output->writeln('<info>Changing to phar.readonly=0 mode ...</info>');
|
||||
if (pcntl_exec(PHP_BINARY, $args) === false) {
|
||||
throw new \PharException('切换到读写模式失败,请检查环境。');
|
||||
}
|
||||
} else {
|
||||
$output->writeln('<info>Now running command in child process.</info>');
|
||||
$this->output->writeln('<info>Now running command in child process.</info>');
|
||||
passthru(PHP_BINARY . ' -d phar.readonly=0 ' . implode(' ', $argv), $retcode);
|
||||
exit($retcode);
|
||||
}
|
||||
@@ -61,9 +57,9 @@ class DeployCommand extends BaseCommand
|
||||
$phar_path = '/tmp/' . $phar_path;
|
||||
}
|
||||
if (file_exists($phar_path)) {
|
||||
$ask = $input->getOption('overwrite') ? true : $prompt->requireBool('<comment>The file "' . $phar_path . '" already exists, do you want to overwrite it?</comment>' . PHP_EOL . 'If you want to, just Enter');
|
||||
$ask = $this->getOption('overwrite') ? true : $prompt->requireBool('<comment>The file "' . $phar_path . '" already exists, do you want to overwrite it?</comment>' . PHP_EOL . 'If you want to, just Enter');
|
||||
if (!$ask) {
|
||||
$output->writeln('<comment>User canceled.</comment>');
|
||||
$this->output->writeln('<comment>User canceled.</comment>');
|
||||
return 1;
|
||||
}
|
||||
@unlink($phar_path);
|
||||
@@ -83,9 +79,9 @@ class DeployCommand extends BaseCommand
|
||||
$map[$v] = $path . '/' . $v;
|
||||
}
|
||||
|
||||
$output->writeln('<info>Start packing files...</info>');
|
||||
$this->output->writeln('<info>Start packing files...</info>');
|
||||
try {
|
||||
foreach ($this->progress($output)->iterate($map) as $file => $origin_file) {
|
||||
foreach ($this->progress()->iterate($map) as $file => $origin_file) {
|
||||
$phar->addFromString($file, php_strip_whitespace($origin_file));
|
||||
}
|
||||
// $phar->buildFromIterator(new SeekableArrayIterator($map, new ProgressBar($output)));
|
||||
@@ -100,30 +96,30 @@ class DeployCommand extends BaseCommand
|
||||
$stub = '.phar-entry.php';
|
||||
$phar->setStub($phar->createDefaultStub($stub));
|
||||
} catch (\Throwable $e) {
|
||||
$output->writeln($e);
|
||||
$this->output->writeln($e);
|
||||
return 1;
|
||||
}
|
||||
$phar->addFromString('.prod', 'true');
|
||||
$phar->stopBuffering();
|
||||
$output->writeln(PHP_EOL . 'Done! Phar file is generated at "' . $phar_path . '".');
|
||||
$this->output->writeln(PHP_EOL . 'Done! Phar file is generated at "' . $phar_path . '".');
|
||||
if (file_exists(SOURCE_PATH . '/php-src/sapi/micro/micro.sfx')) {
|
||||
$output->writeln('Detected you have already compiled micro binary, I will make executable now for you!');
|
||||
$this->output->writeln('Detected you have already compiled micro binary, I will make executable now for you!');
|
||||
file_put_contents(
|
||||
pathinfo($phar_path, PATHINFO_DIRNAME) . '/spc',
|
||||
file_get_contents(SOURCE_PATH . '/php-src/sapi/micro/micro.sfx') .
|
||||
file_get_contents($phar_path)
|
||||
);
|
||||
chmod(pathinfo($phar_path, PATHINFO_DIRNAME) . '/spc', 0755);
|
||||
$output->writeln('<info>Binary Executable: ' . pathinfo($phar_path, PATHINFO_DIRNAME) . '/spc</info>');
|
||||
$this->output->writeln('<info>Binary Executable: ' . pathinfo($phar_path, PATHINFO_DIRNAME) . '/spc</info>');
|
||||
}
|
||||
chmod($phar_path, 0755);
|
||||
$output->writeln('<info>Phar Executable: ' . $phar_path . '</info>');
|
||||
$this->output->writeln('<info>Phar Executable: ' . $phar_path . '</info>');
|
||||
return 0;
|
||||
}
|
||||
|
||||
private function progress(OutputInterface $output, int $max = 0): ProgressBar
|
||||
private function progress(int $max = 0): ProgressBar
|
||||
{
|
||||
$progress = new ProgressBar($output, $max);
|
||||
$progress = new ProgressBar($this->output, $max);
|
||||
$progress->setBarCharacter('<fg=green>⚬</>');
|
||||
$progress->setEmptyBarCharacter('<fg=red>⚬</>');
|
||||
$progress->setProgressCharacter('<fg=green>➤</>');
|
||||
|
||||
Reference in New Issue
Block a user