Skip to content

Commit 8288fd6

Browse files
authored
Merge pull request #357 from asgrim/use-php-config-if-possible
Fix PIE using default PHP version for configure when invoking from a different PHP version
2 parents 4485c6a + 3c4e20a commit 8288fd6

File tree

6 files changed

+64
-21
lines changed

6 files changed

+64
-21
lines changed

.github/workflows/continuous-integration.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@ jobs:
3131
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3232
- uses: actions/checkout@v5
3333
- uses: ramsey/composer-install@v3
34-
- name: GH token
34+
- name: GH token (non-Windows)
3535
if: matrix.operating-system != 'windows-latest'
3636
run: sudo composer config --global --auth github-oauth.github.com ${{ secrets.GITHUB_TOKEN }}
37+
- name: GH token (Windows)
38+
if: matrix.operating-system == 'windows-latest'
39+
run: composer config --global --auth github-oauth.github.com ${{ secrets.GITHUB_TOKEN }}
3740
- name: Run PHPUnit on Windows
3841
if: matrix.operating-system == 'windows-latest'
3942
run: vendor/bin/phpunit

src/Platform/TargetPhp/PhpBinaryPath.php

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use function is_executable;
3232
use function mkdir;
3333
use function preg_match;
34+
use function preg_replace;
3435
use function sprintf;
3536
use function strtolower;
3637
use function trim;
@@ -370,7 +371,50 @@ public static function fromCurrentProcess(): self
370371

371372
self::assertValidLookingPhpBinary($phpExecutable);
372373

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;
374418
}
375419

376420
private static function cleanWarningAndDeprecationsFromOutput(string $testOutput): string

test/behaviour/CliContext.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function theExtensionShouldHaveBeenBuilt(): void
8787
}
8888

8989
Assert::contains($this->output, 'phpize complete.');
90-
Assert::contains($this->output, 'Configure complete.');
90+
Assert::contains($this->output, 'Configure complete');
9191
Assert::contains($this->output, 'Build complete:');
9292
}
9393

test/integration/Command/BuildCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function testBuildCommandWillBuildTheExtension(): void
3838
}
3939

4040
self::assertStringContainsString('phpize complete.', $outputString);
41-
self::assertStringContainsString('Configure complete.', $outputString);
41+
self::assertStringContainsString('Configure complete', $outputString);
4242
self::assertStringContainsString('Build complete:', $outputString);
4343
}
4444
}

test/integration/Downloading/GithubPackageReleaseAssetsTest.php

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
namespace Php\PieIntegrationTest\Downloading;
66

7-
use Composer\Config;
8-
use Composer\IO\IOInterface;
7+
use Composer\Factory;
8+
use Composer\IO\NullIO;
99
use Composer\Package\CompletePackage;
1010
use Composer\Util\AuthHelper;
1111
use Composer\Util\HttpDownloader;
@@ -25,9 +25,6 @@
2525
use PHPUnit\Framework\Attributes\RequiresOperatingSystemFamily;
2626
use PHPUnit\Framework\TestCase;
2727

28-
use function getenv;
29-
use function is_string;
30-
3128
#[CoversClass(GithubPackageReleaseAssets::class)]
3229
final class GithubPackageReleaseAssetsTest extends TestCase
3330
{
@@ -58,17 +55,9 @@ public function testDeterminingReleaseAssetUrlForWindows(): void
5855
'https://api.github.com/repos/asgrim/example-pie-extension/zipball/f9ed13ea95dada34c6cc5a052da258dbda059d27',
5956
);
6057

61-
$io = $this->createMock(IOInterface::class);
62-
63-
$githubToken = getenv('GITHUB_TOKEN');
64-
if (is_string($githubToken) && $githubToken !== '') {
65-
$io->method('hasAuthentication')
66-
->willReturn(true);
67-
$io->method('getAuthentication')
68-
->willReturn(['username' => $githubToken, 'password' => 'x-oauth-basic']);
69-
}
70-
71-
$config = new Config();
58+
$io = new NullIO();
59+
$config = Factory::createConfig();
60+
$io->loadConfiguration($config);
7261

7362
self::assertSame(
7463
'https://github.com/asgrim/example-pie-extension/releases/download/2.0.2/php_example_pie_extension-2.0.2-8.3-ts-vs16-x86_64.zip',

test/unit/Platform/TargetPhp/PhpBinaryPathTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Php\Pie\Platform\TargetPhp\Exception\ExtensionIsNotLoaded;
1313
use Php\Pie\Platform\TargetPhp\Exception\InvalidPhpBinaryPath;
1414
use Php\Pie\Platform\TargetPhp\PhpBinaryPath;
15+
use Php\Pie\Util\Process;
1516
use PHPUnit\Framework\Attributes\CoversClass;
1617
use PHPUnit\Framework\Attributes\DataProvider;
1718
use PHPUnit\Framework\TestCase;
@@ -104,7 +105,13 @@ public function testVersionFromCurrentProcess(): void
104105
PHP_VERSION,
105106
$phpBinary->version(),
106107
);
107-
self::assertNull($phpBinary->phpConfigPath());
108+
109+
$phpConfig = $phpBinary->phpConfigPath();
110+
if ($phpConfig === null) {
111+
return;
112+
}
113+
114+
self::assertSame($phpBinary->phpBinaryPath, Process::run([$phpConfig, '--php-binary']));
108115
}
109116

110117
/** @return array<string, array{0: non-empty-string, 1: non-empty-string}> */

0 commit comments

Comments
 (0)