如何定位TYPO3二进制文件typo3 求解Composer模式下bin-dir获取方法
针对TYPO3 11+ Composer模式获取bin-dir路径的解决方案
方案1:轻量通用实现(推荐,适配99%场景)
TYPO3 11+ Composer模式下已经内置了TYPO3_PROJECT_ROOT常量,直接指向Composer项目根目录(即根composer.json所在目录)。Composer默认bin-dir为项目根目录下的vendor/bin,绝大多数场景下直接使用该规则即可,代码实现如下:
if (TYPO3_COMPOSER_MODE) { // 优先取默认Composer bin路径 $typo3BinaryPath = TYPO3_PROJECT_ROOT . '/vendor/bin/'; // 可选:配合扩展配置做兜底,允许用户自定义路径 $customBinPath = $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['你的扩展标识']['binPath'] ?? ''; if (!empty($customBinPath) && is_dir($customBinPath)) { $typo3BinaryPath = rtrim($customBinPath, '/') . '/'; } } else { $typo3BinaryPath = ExtensionManagementUtility::extPath('core') . 'bin/'; }
方案2:精准读取自定义bin-dir配置
如果需要严格匹配用户在根composer.json中自定义的bin-dir配置,不需要引入完整的composer/composer依赖包,直接解析根配置文件即可,更加轻量,实现如下:
if (TYPO3_COMPOSER_MODE) { $composerJsonPath = TYPO3_PROJECT_ROOT . '/composer.json'; $binDir = 'vendor/bin'; // 兜底默认值 if (file_exists($composerJsonPath)) { $composerConfig = json_decode(file_get_contents($composerJsonPath), true); if (isset($composerConfig['config']['bin-dir'])) { $binDir = trim($composerConfig['config']['bin-dir'], '/'); } } $typo3BinaryPath = TYPO3_PROJECT_ROOT . '/' . $binDir . '/'; } else { $typo3BinaryPath = ExtensionManagementUtility::extPath('core') . 'bin/'; }
补充说明
你提到的将路径归入扩展设置作为配置项的思路非常实用,建议作为兜底选项开放给用户,可以适配项目根目录外自定义bin-dir等极端自定义场景。
内容的提问来源于stack exchange,提问作者Tomasnorre
相关产品推荐
相关产品推荐

