adachsoft / php-interpreter-resolver
Lists available PHP interpreters and resolves binary paths for logical PHP versions.
Package info
gitlab.com/a.adach/php-interpreter-resolver
pkg:composer/adachsoft/php-interpreter-resolver
Requires
- php: ^8.3
- adachsoft/collection: ^3.0
- adachsoft/command-executor-lib: ^2.0
Requires (Dev)
- adachsoft/php-code-style: ^0.7.4
- friendsofphp/php-cs-fixer: ^3.95
- phpstan/phpstan: ^2.3
- phpunit/phpunit: ^13
- rector/rector: ^2.9
- symplify/phpstan-rules: ^14.19
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
A PHP library for listing available PHP interpreters and resolving an interpreter binary path for a logical PHP version.
Features
- Lists PHP versions configured through
update-alternatives. - Resolves a logical version such as
8.3or8.3.1to its interpreter binary path. - Keeps interpreter paths out of the public version DTO and collection.
- Uses
adachsoft/command-executor-libas the process-execution boundary.
Requirements
- PHP 8.3 or higher.
- The
update-alternativescommand configured with PHP interpreter alternatives. adachsoft/command-executor-lib^2.0.adachsoft/collection^3.0.
Installation
composer require adachsoft/php-interpreter-resolver
Usage
Create the resolver through PhpInterpreterResolverFactory and provide a command executor and working directory:
<?php
declare(strict_types=1);
use AdachSoft\CommandExecutorLib\SimpleCommandExecutor;
use AdachSoft\PhpInterpreterResolver\PhpInterpreterResolverFactory;
$commandExecutor = new SimpleCommandExecutor();
$resolver = PhpInterpreterResolverFactory::create($commandExecutor, __DIR__);
foreach ($resolver->listAvailableVersions() as $phpVersion) {
printf(
'%s%s',
$phpVersion->version,
$phpVersion->isCurrent ? ' (current)' : '',
);
echo PHP_EOL;
}
$binaryPath = $resolver->resolveBinaryPath('8.3');
The returned binary path is intended for runner integrations. Do not expose it to end users when it is not part of the consuming application's interface.
See Basic usage for another example.
Exceptions
The resolver throws PhpInterpreterResolverException when a requested version has an invalid format, is unavailable, or the alternatives query fails. Its getCode() value can be compared with CODE_INVALID_VERSION, CODE_VERSION_NOT_AVAILABLE, or CODE_EXECUTION_FAILED.
<?php
use AdachSoft\PhpInterpreterResolver\Exception\PhpInterpreterResolverException;
try {
$binaryPath = $resolver->resolveBinaryPath('8.3');
} catch (PhpInterpreterResolverException $exception) {
// Handle the stable library error code without exposing internal command details.
error_log((string) $exception->getCode());
}