From 10e79ac2681f6374048bd2d5c80b26ab0cebe09a Mon Sep 17 00:00:00 2001 From: rango Date: Fri, 8 May 2026 11:50:50 +0800 Subject: [PATCH] fix(Linux): use php-config to dynamically get include paths --- src/Php/Platform/Linux.php | 46 +++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/src/Php/Platform/Linux.php b/src/Php/Platform/Linux.php index d103ad3f..84e920bd 100644 --- a/src/Php/Platform/Linux.php +++ b/src/Php/Platform/Linux.php @@ -120,20 +120,56 @@ class Linux extends PlatformBase } /** - * 构建 PHP 包含路径 + * 构建 PHP 包含路径(使用 php-config 动态获取) */ public function buildPhpIncludePaths(string $phpDir): array { + // 优先使用 php-config 获取包含路径 + $phpConfigPath = $this->findPhpConfig($phpDir); + if ($phpConfigPath) { + $includes = shell_exec("{$phpConfigPath} --includes 2>/dev/null"); + if ($includes) { + // 解析 -I/path 格式的路径 + preg_match_all('/-I([^\s]+)/', $includes, $matches); + if (!empty($matches[1])) { + // 过滤不存在的路径并返回 + return array_filter($matches[1], 'is_dir'); + } + } + } + + // 回退到硬编码路径(兼容旧版本) $paths = [ - $phpDir . '/include', - $phpDir . '/include/main', - $phpDir . '/include/TSRM', - $phpDir . '/include/Zend', + $phpDir . '/include/php', + $phpDir . '/include/php/main', + $phpDir . '/include/php/TSRM', + $phpDir . '/include/php/Zend', + $phpDir . '/include/php/ext', ]; // 过滤不存在的路径 return array_filter($paths, 'is_dir'); } + + /** + * 查找 php-config 可执行文件 + */ + private function findPhpConfig(string $phpDir): ?string + { + $candidates = [ + $phpDir . '/bin/php-config', + '/usr/bin/php-config', + '/usr/local/bin/php-config', + ]; + + foreach ($candidates as $path) { + if (is_executable($path)) { + return $path; + } + } + + return null; + } /** * 构建 PHP 库路径