|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace StaticPHP\Command; |
| 6 | + |
| 7 | +use StaticPHP\Util\FileSystem; |
| 8 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 9 | +use Symfony\Component\Console\Input\InputArgument; |
| 10 | +use Symfony\Component\Console\Input\InputOption; |
| 11 | + |
| 12 | +#[AsCommand('micro:combine', 'Combine micro.sfx and php code together')] |
| 13 | +class MicroCombineCommand extends BaseCommand |
| 14 | +{ |
| 15 | + public function configure(): void |
| 16 | + { |
| 17 | + $this->addArgument('file', InputArgument::REQUIRED, 'The php or phar file to be combined'); |
| 18 | + $this->addOption('with-micro', 'M', InputOption::VALUE_REQUIRED, 'Customize your micro.sfx file'); |
| 19 | + $this->addOption('with-ini-set', 'I', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'ini to inject into micro.sfx when combining'); |
| 20 | + $this->addOption('with-ini-file', 'N', InputOption::VALUE_REQUIRED, 'ini file to inject into micro.sfx when combining'); |
| 21 | + $this->addOption('output', 'O', InputOption::VALUE_REQUIRED, 'Customize your output binary file name'); |
| 22 | + } |
| 23 | + |
| 24 | + public function handle(): int |
| 25 | + { |
| 26 | + // 0. Initialize path variables |
| 27 | + $internal = FileSystem::convertPath(BUILD_ROOT_PATH . '/bin/micro.sfx'); |
| 28 | + $micro_file = $this->getOption('with-micro'); |
| 29 | + $file = $this->getArgument('file'); |
| 30 | + $ini_set = $this->getOption('with-ini-set'); |
| 31 | + $ini_file = $this->getOption('with-ini-file'); |
| 32 | + $target_ini = []; |
| 33 | + $output = $this->getOption('output') ?? 'my-app'; |
| 34 | + $ini_part = ''; |
| 35 | + // 1. Make sure specified micro.sfx file exists |
| 36 | + if ($micro_file !== null && !file_exists($micro_file)) { |
| 37 | + $this->output->writeln('<error>The micro.sfx file you specified is incorrect or does not exist!</error>'); |
| 38 | + return static::FAILURE; |
| 39 | + } |
| 40 | + // 2. Make sure buildroot/bin/micro.sfx exists |
| 41 | + if ($micro_file === null && !file_exists($internal)) { |
| 42 | + $this->output->writeln('<error>You haven\'t compiled micro.sfx yet, please use "build" command and "--build-micro" to compile phpmicro first!</error>'); |
| 43 | + return static::FAILURE; |
| 44 | + } |
| 45 | + // 3. Use buildroot/bin/micro.sfx |
| 46 | + if ($micro_file === null) { |
| 47 | + $micro_file = $internal; |
| 48 | + } |
| 49 | + // 4. Make sure php or phar file exists |
| 50 | + if (!is_file(FileSystem::convertPath($file))) { |
| 51 | + $this->output->writeln('<error>The file to combine does not exist!</error>'); |
| 52 | + return static::FAILURE; |
| 53 | + } |
| 54 | + // 5. Confirm ini files (ini-set has higher priority) |
| 55 | + if ($ini_file !== null) { |
| 56 | + // Check file exist first |
| 57 | + if (!file_exists($ini_file)) { |
| 58 | + $this->output->writeln('<error>The ini file to combine does not exist! (' . $ini_file . ')</error>'); |
| 59 | + return static::FAILURE; |
| 60 | + } |
| 61 | + $arr = parse_ini_file($ini_file); |
| 62 | + if ($arr === false) { |
| 63 | + $this->output->writeln('<error>Cannot parse ini file</error>'); |
| 64 | + return static::FAILURE; |
| 65 | + } |
| 66 | + $target_ini = array_merge($target_ini, $arr); |
| 67 | + } |
| 68 | + // 6. Confirm ini sets |
| 69 | + if ($ini_set !== []) { |
| 70 | + foreach ($ini_set as $item) { |
| 71 | + $arr = parse_ini_string($item); |
| 72 | + if ($arr === false) { |
| 73 | + $this->output->writeln('<error>--with-ini-set parse failed</error>'); |
| 74 | + return static::FAILURE; |
| 75 | + } |
| 76 | + $target_ini = array_merge($target_ini, $arr); |
| 77 | + } |
| 78 | + } |
| 79 | + // 7. Generate ini injection parts |
| 80 | + if (!empty($target_ini)) { |
| 81 | + $ini_str = $this->encodeINI($target_ini); |
| 82 | + logger()->debug('Injecting ini parts: ' . PHP_EOL . $ini_str); |
| 83 | + $ini_part = "\xfd\xf6\x69\xe6"; |
| 84 | + $ini_part .= pack('N', strlen($ini_str)); |
| 85 | + $ini_part .= $ini_str; |
| 86 | + } |
| 87 | + // 8. Combine ! |
| 88 | + $output = FileSystem::isRelativePath($output) ? (WORKING_DIR . '/' . $output) : $output; |
| 89 | + $file_target = file_get_contents($micro_file) . $ini_part . file_get_contents($file); |
| 90 | + if (PHP_OS_FAMILY === 'Windows' && !str_ends_with(strtolower($output), '.exe')) { |
| 91 | + $output .= '.exe'; |
| 92 | + } |
| 93 | + $output = FileSystem::convertPath($output); |
| 94 | + $result = file_put_contents($output, $file_target); |
| 95 | + if ($result === false) { |
| 96 | + $this->output->writeln('<error>Combine failed.</error>'); |
| 97 | + return static::FAILURE; |
| 98 | + } |
| 99 | + // 9. chmod +x |
| 100 | + chmod($output, 0755); |
| 101 | + $this->output->writeln('<info>Combine success! Binary file: ' . $output . '</info>'); |
| 102 | + return static::SUCCESS; |
| 103 | + } |
| 104 | + |
| 105 | + private function encodeINI(array $array): string |
| 106 | + { |
| 107 | + $res = []; |
| 108 | + foreach ($array as $key => $val) { |
| 109 | + if (is_array($val)) { |
| 110 | + $res[] = "[{$key}]"; |
| 111 | + foreach ($val as $skey => $sval) { |
| 112 | + $res[] = "{$skey}=" . (is_numeric($sval) ? $sval : '"' . $sval . '"'); |
| 113 | + } |
| 114 | + } else { |
| 115 | + $res[] = "{$key}=" . (is_numeric($val) ? $val : '"' . $val . '"'); |
| 116 | + } |
| 117 | + } |
| 118 | + return implode("\n", $res); |
| 119 | + } |
| 120 | +} |
0 commit comments