|
31 | 31 | use function is_executable; |
32 | 32 | use function mkdir; |
33 | 33 | use function preg_match; |
| 34 | +use function preg_replace; |
34 | 35 | use function sprintf; |
35 | 36 | use function strtolower; |
36 | 37 | use function trim; |
@@ -370,7 +371,50 @@ public static function fromCurrentProcess(): self |
370 | 371 |
|
371 | 372 | self::assertValidLookingPhpBinary($phpExecutable); |
372 | 373 |
|
373 | | - return new self($phpExecutable, null); |
| 374 | + return self::guessWithPhpConfig(new self($phpExecutable, null)); |
| 375 | + } |
| 376 | + |
| 377 | + private static function guessWithPhpConfig(self $phpBinaryPath): self |
| 378 | + { |
| 379 | + $phpConfigAttempts = []; |
| 380 | + |
| 381 | + // Try to add `phpize` from path |
| 382 | + $whichPhpize = new \Symfony\Component\Process\Process(['which', 'php-config']); |
| 383 | + if ($whichPhpize->run() === 0) { |
| 384 | + $phpConfigAttempts[] = trim($whichPhpize->getOutput()); |
| 385 | + } |
| 386 | + |
| 387 | + $phpConfigAttempts[] = preg_replace('((.*)php)', '$1php-config', $phpBinaryPath->phpBinaryPath); |
| 388 | + |
| 389 | + foreach ($phpConfigAttempts as $phpConfigAttempt) { |
| 390 | + assert($phpConfigAttempt !== null); |
| 391 | + assert($phpConfigAttempt !== ''); |
| 392 | + if (! file_exists($phpConfigAttempt) || ! is_executable($phpConfigAttempt)) { |
| 393 | + continue; |
| 394 | + } |
| 395 | + |
| 396 | + $phpizeProcess = new \Symfony\Component\Process\Process([$phpConfigAttempt, '--php-binary']); |
| 397 | + if ($phpizeProcess->run() !== 0) { |
| 398 | + continue; |
| 399 | + } |
| 400 | + |
| 401 | + if (trim($phpizeProcess->getOutput()) !== $phpBinaryPath->phpBinaryPath) { |
| 402 | + continue; |
| 403 | + } |
| 404 | + |
| 405 | + $phpConfigApiVersionProcess = new \Symfony\Component\Process\Process([$phpConfigAttempt, '--phpapi']); |
| 406 | + |
| 407 | + // older versions of php-config did not have `--phpapi`, so we can't perform this validation |
| 408 | + if ($phpConfigApiVersionProcess->run() !== 0) { |
| 409 | + return new self($phpBinaryPath->phpBinaryPath, $phpConfigAttempt); |
| 410 | + } |
| 411 | + |
| 412 | + if (trim($phpConfigApiVersionProcess->getOutput()) === $phpBinaryPath->phpApiVersion()) { |
| 413 | + return new self($phpBinaryPath->phpBinaryPath, $phpConfigAttempt); |
| 414 | + } |
| 415 | + } |
| 416 | + |
| 417 | + return $phpBinaryPath; |
374 | 418 | } |
375 | 419 |
|
376 | 420 | private static function cleanWarningAndDeprecationsFromOutput(string $testOutput): string |
|
0 commit comments