diff --git a/redaxo/src/core/lib/util/path.php b/redaxo/src/core/lib/util/path.php index d63910ba77..a22a7345e6 100644 --- a/redaxo/src/core/lib/util/path.php +++ b/redaxo/src/core/lib/util/path.php @@ -404,4 +404,21 @@ public static function basename($path) /** @psalm-suppress ForbiddenCode */ return basename($path); } + + public static function findBinaryPath(string $commandName): ?string + { + if (!function_exists('exec')) { + return null; + } + + $out = []; + $cmd = sprintf('command -v %s || which %s', $commandName, $commandName); + exec($cmd, $out, $ret); + + if (0 === $ret) { + return (string) $out[0]; + } + + return null; + } } diff --git a/redaxo/src/core/tests/path_test.php b/redaxo/src/core/tests/path_test.php index cf9af88023..90d6fa27e0 100644 --- a/redaxo/src/core/tests/path_test.php +++ b/redaxo/src/core/tests/path_test.php @@ -45,6 +45,18 @@ public function testBasename() static::assertSame('config.yml', rex_path::basename('..\redaxo\data\core\config.yml')); } + public function testFindBinaryPath(): void + { + $path = rex_path::findBinaryPath('php'); + static::assertNotNull($path); + static::assertSame(PHP_BINARY, realpath($path)); + } + + public function testNotFoundBinaryPath(): void + { + static::assertNull(rex_path::findBinaryPath('noone-knows')); + } + private function path($path) { return str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $path);