diff --git a/src/ZM/Command/InitCommand.php b/src/ZM/Command/InitCommand.php index e1cff088..42a541a0 100644 --- a/src/ZM/Command/InitCommand.php +++ b/src/ZM/Command/InitCommand.php @@ -9,6 +9,7 @@ use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\ConsoleSectionOutput; use Symfony\Component\Console\Output\OutputInterface; use ZM\Exception\InitException; +use ZM\Utils\ZMUtil; #[AsCommand(name: 'init', description: '初始化框架运行的基础文件')] class InitCommand extends Command @@ -75,10 +76,7 @@ class InitCommand extends Command $section->write('执行 composer dump-autoload ... '); // 兼容内建 Composer - $env = getenv('COMPOSER_EXECUTABLE'); - if ($env === false) { - $env = 'composer'; - } + $env = ZMUtil::getComposerExecutable(); passthru(PHP_BINARY . ' ' . escapeshellcmd($env) . ' dump-autoload'); $section->writeln('完成'); diff --git a/src/ZM/Utils/CodeGenerator/PluginGenerator.php b/src/ZM/Utils/CodeGenerator/PluginGenerator.php index 0e16dd65..aa316af5 100644 --- a/src/ZM/Utils/CodeGenerator/PluginGenerator.php +++ b/src/ZM/Utils/CodeGenerator/PluginGenerator.php @@ -6,6 +6,7 @@ namespace ZM\Utils\CodeGenerator; use ZM\Exception\FileSystemException; use ZM\Store\FileSystem; +use ZM\Utils\ZMUtil; /** * Class PluginGenerator @@ -81,10 +82,7 @@ class PluginGenerator file_put_contents(zm_dir($plugin_base_dir . '/composer.json'), json_encode($composer, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); // TODO: 寻找 PHP 运行环境和 Composer 是否在当前目录的情况 chdir($plugin_base_dir); - $env = getenv('COMPOSER_EXECUTABLE'); - if ($env === false) { - $env = 'composer'; - } + $env = ZMUtil::getComposerExecutable(); passthru(PHP_BINARY . ' ' . escapeshellcmd($env) . ' dump-autoload'); chdir(WORKING_DIR); return $plugin_base_dir; diff --git a/src/ZM/Utils/ZMUtil.php b/src/ZM/Utils/ZMUtil.php index 2b7e45bb..abc6b20b 100644 --- a/src/ZM/Utils/ZMUtil.php +++ b/src/ZM/Utils/ZMUtil.php @@ -27,4 +27,16 @@ class ZMUtil { return file_put_contents(($path ?? SOURCE_ROOT_DIR) . '/composer.json', json_encode($content, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); } + + /** + * 获取当前 Composer 二进制执行命令的初始位置 + */ + public static function getComposerExecutable(): string + { + $env = getenv('COMPOSER_EXECUTABLE'); + if ($env === false) { + $env = 'composer'; + } + return $env; + } }