From 02713fafb554e091a26e32d87038afc1d275c2cf Mon Sep 17 00:00:00 2001 From: crazywhalecc Date: Wed, 29 Apr 2026 15:09:21 +0800 Subject: [PATCH] Enhance package management in CraftCommand by merging craft-level packages and shared extensions into build options --- src/StaticPHP/Command/CraftCommand.php | 15 +++++++++++++++ src/StaticPHP/Package/PackageBuilder.php | 13 +++++++++++++ src/StaticPHP/Package/TargetPackage.php | 8 ++++++++ 3 files changed, 36 insertions(+) diff --git a/src/StaticPHP/Command/CraftCommand.php b/src/StaticPHP/Command/CraftCommand.php index 9b1126ac..f390e433 100644 --- a/src/StaticPHP/Command/CraftCommand.php +++ b/src/StaticPHP/Command/CraftCommand.php @@ -4,8 +4,10 @@ declare(strict_types=1); namespace StaticPHP\Command; +use StaticPHP\DI\ApplicationContext; use StaticPHP\Doctor\Doctor; use StaticPHP\Exception\ValidationException; +use StaticPHP\Package\PackageBuilder; use StaticPHP\Package\PackageInstaller; use StaticPHP\Util\FileSystem; use Symfony\Component\Console\Attribute\AsCommand; @@ -63,6 +65,18 @@ class CraftCommand extends BaseCommand $build_options["build-{$name}"] = true; } + // merge craft-level packages into with-packages option + if ($craft['packages'] !== []) { + $existing = parse_comma_list($build_options['with-packages'] ?? ''); + $build_options['with-packages'] = implode(',', array_unique(array_merge($existing, $craft['packages']))); + } + + // merge shared-extensions into build-shared option + if ($craft['shared-extensions'] !== []) { + $existing = parse_extension_list($build_options['build-shared'] ?? ''); + $build_options['build-shared'] = implode(',', array_unique(array_merge($existing, $craft['shared-extensions']))); + } + // clean build if ($craft['clean-build']) { FileSystem::resetDir(BUILD_ROOT_PATH); @@ -72,6 +86,7 @@ class CraftCommand extends BaseCommand $starttime = microtime(true); // run installer $installer = new PackageInstaller($build_options); + ApplicationContext::get(PackageBuilder::class)->setArgument('extensions', implode(',', $craft['extensions'])); $installer->addBuildPackage('php'); $installer->run(true); diff --git a/src/StaticPHP/Package/PackageBuilder.php b/src/StaticPHP/Package/PackageBuilder.php index 5138582f..582fa474 100644 --- a/src/StaticPHP/Package/PackageBuilder.php +++ b/src/StaticPHP/Package/PackageBuilder.php @@ -23,6 +23,9 @@ class PackageBuilder /** @var int make jobs count */ public readonly int $concurrency; + /** @var array Build arguments */ + protected array $arguments = []; + /** * @param array $options Builder options */ @@ -101,6 +104,16 @@ class PackageBuilder return $this->options[$key] ?? $default; } + public function setArgument(string $key, mixed $value): void + { + $this->arguments[$key] = $value; + } + + public function getArgument(string $key, mixed $default = null): mixed + { + return $this->arguments[$key] ?? $default; + } + /** * Deploy the binary file from src to dst. */ diff --git a/src/StaticPHP/Package/TargetPackage.php b/src/StaticPHP/Package/TargetPackage.php index ca80d83a..9ff5c7de 100644 --- a/src/StaticPHP/Package/TargetPackage.php +++ b/src/StaticPHP/Package/TargetPackage.php @@ -106,6 +106,14 @@ class TargetPackage extends LibraryPackage if ($input !== null && $input->hasArgument($key)) { return $input->getArgument($key); } + + // fallback to builder arguments (set programmatically, e.g. from CraftCommand) + $builder = ApplicationContext::has(PackageBuilder::class) + ? ApplicationContext::get(PackageBuilder::class) + : null; + if ($builder !== null && ($arg = $builder->getArgument($key)) !== null) { + return $arg; + } return null; }