Skip to content

Commit

Permalink
added rex_path::findBinaryPath() (#5158)
Browse files Browse the repository at this point in the history
Co-authored-by: Gregor Harlan <330436+gharlan@users.noreply.github.com>
  • Loading branch information
staabm and gharlan committed Jul 14, 2022
1 parent 924f9f5 commit caffa38
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
17 changes: 17 additions & 0 deletions redaxo/src/core/lib/util/path.php
Expand Up @@ -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;
}
}
12 changes: 12 additions & 0 deletions redaxo/src/core/tests/path_test.php
Expand Up @@ -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);
Expand Down

0 comments on commit caffa38

Please sign in to comment.