|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace StaticPHP\Command; |
| 6 | + |
| 7 | +use StaticPHP\Doctor\Doctor; |
| 8 | +use StaticPHP\Exception\ValidationException; |
| 9 | +use StaticPHP\Package\PackageInstaller; |
| 10 | +use StaticPHP\Util\FileSystem; |
| 11 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 12 | +use Symfony\Component\Console\Output\OutputInterface; |
| 13 | +use Symfony\Component\Yaml\Exception\ParseException; |
| 14 | +use Symfony\Component\Yaml\Yaml; |
| 15 | + |
| 16 | +#[AsCommand('craft', 'Build static-php from craft.yml')] |
| 17 | +class CraftCommand extends BaseCommand |
| 18 | +{ |
| 19 | + public function configure(): void |
| 20 | + { |
| 21 | + $this->addArgument('craft', null, 'Path to craft.yml file', WORKING_DIR . '/craft.yml'); |
| 22 | + } |
| 23 | + |
| 24 | + public function handle(): int |
| 25 | + { |
| 26 | + $craft_file = $this->getArgument('craft'); |
| 27 | + if (!file_exists($craft_file)) { |
| 28 | + $this->output->writeln('<error>craft.yml not found, please create one!</error>'); |
| 29 | + return static::USER_ERROR; |
| 30 | + } |
| 31 | + |
| 32 | + $craft = $this->validateAndParseCraftFile($craft_file); |
| 33 | + |
| 34 | + // set verbosity |
| 35 | + $this->output->setVerbosity($craft['verbosity']); |
| 36 | + |
| 37 | + // apply env |
| 38 | + array_walk($craft['extra-env'], fn ($v, $k) => f_putenv("{$k}={$v}")); |
| 39 | + |
| 40 | + // run doctor |
| 41 | + if ($craft['craft-options']['doctor']) { |
| 42 | + $doctor = new Doctor($this->output, FIX_POLICY_AUTOFIX); |
| 43 | + if ($doctor->checkAll()) { |
| 44 | + Doctor::markPassed(); |
| 45 | + $this->output->writeln(''); |
| 46 | + } else { |
| 47 | + $this->output->writeln('<error>Doctor check failed, please fix the issues and try again.</error>'); |
| 48 | + return static::ENVIRONMENT_ERROR; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + // parse download-options to installer's dl options |
| 53 | + $build_options = $craft['build-options']; |
| 54 | + if (!$craft['craft-options']['download']) { |
| 55 | + $build_options['no-download'] = true; |
| 56 | + } |
| 57 | + foreach ($craft['download-options'] as $k => $v) { |
| 58 | + $build_options["dl-{$k}"] = $v; |
| 59 | + } |
| 60 | + |
| 61 | + // parse SAPI |
| 62 | + foreach ($craft['sapi'] as $name) { |
| 63 | + $build_options["build-{$name}"] = true; |
| 64 | + } |
| 65 | + |
| 66 | + // clean build |
| 67 | + if ($craft['clean-build']) { |
| 68 | + FileSystem::resetDir(BUILD_ROOT_PATH); |
| 69 | + FileSystem::resetDir(SOURCE_PATH); |
| 70 | + } |
| 71 | + |
| 72 | + $starttime = microtime(true); |
| 73 | + // run installer |
| 74 | + $installer = new PackageInstaller($build_options); |
| 75 | + $installer->addBuildPackage('php'); |
| 76 | + $installer->run(true); |
| 77 | + |
| 78 | + $usedtime = round(microtime(true) - $starttime, 1); |
| 79 | + $this->output->writeln("\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"); |
| 80 | + $this->output->writeln("<info>✔ BUILD SUCCESSFUL ({$usedtime} s)</info>"); |
| 81 | + $this->output->writeln("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n"); |
| 82 | + |
| 83 | + $installer->printBuildPackageOutputs(); |
| 84 | + |
| 85 | + return static::SUCCESS; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Validate and parse craft.yml file to array. |
| 90 | + * |
| 91 | + * @param string $craft_file craft.yml path |
| 92 | + * @return array{ |
| 93 | + * php-version: string, |
| 94 | + * extensions: array<string>, |
| 95 | + * shared-extensions: array<string>, |
| 96 | + * packages: array<string>, |
| 97 | + * sapi: array<string>, |
| 98 | + * verbosity: int, |
| 99 | + * debug: bool, |
| 100 | + * clean-build: bool, |
| 101 | + * build-options: array<string, mixed>, |
| 102 | + * download-options: array<string, mixed>, |
| 103 | + * extra-env: array<string, string>, |
| 104 | + * craft-options: array{ |
| 105 | + * doctor: bool, |
| 106 | + * download: bool, |
| 107 | + * build: bool |
| 108 | + * } |
| 109 | + * } Parsed craft content |
| 110 | + */ |
| 111 | + private function validateAndParseCraftFile(string $craft_file): array |
| 112 | + { |
| 113 | + $build_options = $this->getApplication()->find('build:php')->getDefinition()->getOptions(); |
| 114 | + $download_options = $this->getApplication()->find('download')->getDefinition()->getOptions(); |
| 115 | + try { |
| 116 | + $craft = Yaml::parseFile($craft_file); |
| 117 | + } catch (ParseException $e) { |
| 118 | + throw new ValidationException("Craft file '{$craft_file}' is broken: {$e->getMessage()}"); |
| 119 | + } |
| 120 | + if (!is_assoc_array($craft)) { |
| 121 | + throw new ValidationException("Craft file '{$craft_file}' must be an associative array."); |
| 122 | + } |
| 123 | + |
| 124 | + // check php-version |
| 125 | + if (isset($craft['php-version']) && !preg_match('/^(\d+)(\.\d+)?(\.\d+)?$/', strval($craft['php-version']))) { |
| 126 | + throw new ValidationException("Craft file '{$craft_file}' has invalid 'php-version' field, it should be in format of '8.0.0'."); |
| 127 | + } |
| 128 | + |
| 129 | + // check php extensions field |
| 130 | + if (!isset($craft['extensions'])) { |
| 131 | + throw new ValidationException("Craft file '{$craft_file}' must have 'extensions' field."); |
| 132 | + } |
| 133 | + // parse extension if not list |
| 134 | + if (is_string($craft['extensions'])) { |
| 135 | + $craft['extensions'] = parse_extension_list($craft['extensions']); |
| 136 | + } |
| 137 | + |
| 138 | + // check shared-extensions field |
| 139 | + if (!isset($craft['shared-extensions'])) { |
| 140 | + $craft['shared-extensions'] = []; |
| 141 | + } elseif (is_string($craft['shared-extensions'])) { |
| 142 | + $craft['shared-extensions'] = parse_extension_list($craft['shared-extensions']); |
| 143 | + } |
| 144 | + |
| 145 | + // check libs and additional packages |
| 146 | + $v2_libs = parse_comma_list($craft['libs'] ?? []); |
| 147 | + $v3_packages = parse_comma_list($craft['packages'] ?? []); |
| 148 | + $craft['packages'] = array_merge($v2_libs, $v3_packages); |
| 149 | + |
| 150 | + // check PHP SAPI |
| 151 | + if (!isset($craft['sapi'])) { |
| 152 | + throw new ValidationException('Craft file "sapi" is required.'); |
| 153 | + } |
| 154 | + if (is_string($craft['sapi'])) { |
| 155 | + $craft['sapi'] = parse_comma_list($craft['sapi']); |
| 156 | + } |
| 157 | + |
| 158 | + // verbosity |
| 159 | + $verbosity_level = $craft['verbosity'] ?? OutputInterface::VERBOSITY_NORMAL; |
| 160 | + $debug = $craft['debug'] ?? false; |
| 161 | + if ($debug) { |
| 162 | + $verbosity_level = OutputInterface::VERBOSITY_DEBUG; |
| 163 | + } |
| 164 | + $craft['verbosity'] = $verbosity_level; |
| 165 | + |
| 166 | + // clean-build (if true, reset before all builds) |
| 167 | + $craft['clean-build'] ??= false; |
| 168 | + |
| 169 | + // build-options |
| 170 | + if (isset($craft['build-options'])) { |
| 171 | + if (!is_assoc_array($craft['build-options'])) { |
| 172 | + throw new ValidationException('Craft file "build" options must be an associative array.'); |
| 173 | + } |
| 174 | + foreach ($craft['build-options'] as $key => $value) { |
| 175 | + if (!isset($build_options[$key])) { |
| 176 | + throw new ValidationException('Craft file "build" option "' . $key . '" is invalid.'); |
| 177 | + } |
| 178 | + if ($build_options[$key]->isArray() && !is_array($value)) { |
| 179 | + throw new ValidationException('Craft file "build" option "' . $key . '" must be an array.'); |
| 180 | + } |
| 181 | + } |
| 182 | + } else { |
| 183 | + $craft['build-options'] = []; |
| 184 | + } |
| 185 | + |
| 186 | + // download-options |
| 187 | + if (isset($craft['download-options'])) { |
| 188 | + if (!is_assoc_array($craft['download-options'])) { |
| 189 | + throw new ValidationException('Craft file "download" options must be an associative array.'); |
| 190 | + } |
| 191 | + foreach ($craft['download-options'] as $key => $value) { |
| 192 | + if (!isset($download_options[$key])) { |
| 193 | + throw new ValidationException('Craft file "download" option "' . $key . '" is invalid.'); |
| 194 | + } |
| 195 | + if ($download_options[$key]->isArray() && !is_array($value)) { |
| 196 | + throw new ValidationException('Craft file "download" option "' . $key . '" must be an array.'); |
| 197 | + } |
| 198 | + } |
| 199 | + } else { |
| 200 | + $craft['download-options'] = []; |
| 201 | + } |
| 202 | + |
| 203 | + // post-parse: parse php-version field to download options |
| 204 | + if (isset($craft['php-version'])) { |
| 205 | + $craft['download-options']['with-php'] = strval($craft['php-version']); |
| 206 | + $craft['download-options']['ignore-cache'] = (($craft['download-options']['ignore-cache'] ?? false) === true ? true : 'php-src'); |
| 207 | + } |
| 208 | + |
| 209 | + // extra-env |
| 210 | + if (isset($craft['extra-env'])) { |
| 211 | + if (!is_assoc_array($craft['extra-env'])) { |
| 212 | + throw new ValidationException('Craft file "extra-env" must be an associative array.'); |
| 213 | + } |
| 214 | + } else { |
| 215 | + $craft['extra-env'] = []; |
| 216 | + } |
| 217 | + |
| 218 | + // craft-options |
| 219 | + $craft['craft-options']['doctor'] ??= true; |
| 220 | + $craft['craft-options']['download'] ??= true; |
| 221 | + |
| 222 | + return $craft; |
| 223 | + } |
| 224 | +} |
0 commit comments