Skip to content

Commit 52d6b65

Browse files
committedApr 1, 2024
Added helper to find the PHP binary
1 parent 3323693 commit 52d6b65

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed
 

‎composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"illuminate/container": "^10.47",
3535
"psr/http-message": "^2.0",
3636
"symfony/console": "^6.4",
37+
"symfony/process": "^6.4",
3738
"webmozart/assert": "^1.11"
3839
},
3940
"require-dev": {

‎composer.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/TargetPhp/PhpBinaryPath.php

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Php\Pie\TargetPhp;
6+
7+
use Symfony\Component\Process\PhpExecutableFinder;
8+
use Symfony\Component\Process\Process;
9+
use Webmozart\Assert\Assert;
10+
11+
/** @internal This is not public API for PIE, so should not be depended upon unless you accept the risk of BC breaks */
12+
final class PhpBinaryPath
13+
{
14+
/** @param non-empty-string $phpBinaryPath */
15+
private function __construct(readonly string $phpBinaryPath)
16+
{
17+
}
18+
19+
public function version(): string
20+
{
21+
$phpVersion = trim((new Process([$this->phpBinaryPath, '-r', 'echo phpversion();']))
22+
->mustRun()
23+
->getOutput());
24+
Assert::stringNotEmpty($phpVersion, 'Could not determine PHP version');
25+
return $phpVersion;
26+
}
27+
28+
public static function fromPhpConfigExecutable(string $phpConfig): self
29+
{
30+
// @todo filter input/sanitize output
31+
$phpExecutable = trim((new Process([$phpConfig, '--php-binary']))
32+
->mustRun()
33+
->getOutput());
34+
Assert::stringNotEmpty($phpExecutable, 'Could not find path to PHP executable.');
35+
return new self($phpExecutable);
36+
}
37+
38+
public static function fromCurrentProcess(): self
39+
{
40+
$phpExecutable = trim((new PhpExecutableFinder())->find());
41+
Assert::stringNotEmpty($phpExecutable, 'Could not find path to PHP executable.');
42+
return new self($phpExecutable);
43+
}
44+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Php\PieUnitTest\TargetPhp;
6+
7+
use Php\Pie\TargetPhp\PhpBinaryPath;
8+
use PHPUnit\Framework\Attributes\CoversClass;
9+
use PHPUnit\Framework\TestCase;
10+
11+
#[CoversClass(PhpBinaryPath::class)]
12+
final class PhpBinaryPathTest extends TestCase
13+
{
14+
public function testVersionFromCurrentProcess(): void
15+
{
16+
self::assertSame(PHP_VERSION, PhpBinaryPath::fromCurrentProcess()->version());
17+
}
18+
}

0 commit comments

Comments
 (0)
Please sign in to comment.