Skip to content

Commit d4e3063

Browse files
authored
Rename PHP executable finder (#93)
- Rename the PHP executable finder from `detectPhpExecutable()` to `getPhpExecutable()` as the act of detecting it is an implementation detail, one could specify directly the desired executable. - Make `getPhpExecutable()` non static. A user could for example want to get a specific path from the container in which case having access to an instance state is necessary.
1 parent 9b6e5ff commit d4e3063

File tree

2 files changed

+54
-15
lines changed

2 files changed

+54
-15
lines changed

src/Parallelization.php

+7-15
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,21 @@
1515

1616
use function getcwd;
1717
use function realpath;
18-
use RuntimeException;
1918
use function sprintf;
2019
use Symfony\Component\Console\Command\Command;
2120
use Symfony\Component\Console\Input\InputInterface;
2221
use Symfony\Component\Console\Logger\ConsoleLogger;
2322
use Symfony\Component\Console\Output\OutputInterface;
2423
use Symfony\Component\Console\Terminal;
2524
use Symfony\Component\DependencyInjection\ContainerInterface;
26-
use Symfony\Component\Process\PhpExecutableFinder;
27-
use Symfony\Component\Process\Process;
2825
use Webmozart\Assert\Assert;
2926
use Webmozarts\Console\Parallelization\ErrorHandler\ItemProcessingErrorHandler;
3027
use Webmozarts\Console\Parallelization\ErrorHandler\ItemProcessingErrorHandlerLogger;
3128
use Webmozarts\Console\Parallelization\ErrorHandler\ResetContainerErrorHandler;
3229
use Webmozarts\Console\Parallelization\Logger\DebugProgressBarFactory;
3330
use Webmozarts\Console\Parallelization\Logger\Logger;
3431
use Webmozarts\Console\Parallelization\Logger\StandardLogger;
32+
use Webmozarts\Console\Parallelization\Process\PhpExecutableFinder;
3533

3634
/**
3735
* Adds parallelization capabilities to console commands.
@@ -229,9 +227,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
229227
fn (string $item, InputInterface $input, OutputInterface $output) => $this->runSingleCommand($item, $input, $output),
230228
fn (int $count) => $this->getItemName($count),
231229
$this->getConsolePath(),
232-
self::detectPhpExecutable(),
230+
$this->getPhpExecutable(),
233231
$this->getName(),
234-
self::getWorkingDirectory(),
232+
$this->getWorkingDirectory(),
235233
$this->getExtraEnvironmentVariables(),
236234
$this->getDefinition(),
237235
$this->createItemErrorHandler(),
@@ -325,23 +323,17 @@ private static function getProgressSymbol(): string
325323
}
326324

327325
/**
328-
* Detects the path of the PHP interpreter.
326+
* Returns the path of the PHP executable.
329327
*/
330-
private static function detectPhpExecutable(): string
328+
private function getPhpExecutable(): string
331329
{
332-
$php = (new PhpExecutableFinder())->find();
333-
334-
if (false === $php) {
335-
throw new RuntimeException('Cannot find php executable');
336-
}
337-
338-
return $php;
330+
return PhpExecutableFinder::find();
339331
}
340332

341333
/**
342334
* Returns the working directory for the child process.
343335
*/
344-
private static function getWorkingDirectory(): string
336+
private function getWorkingDirectory(): string
345337
{
346338
return getcwd();
347339
}

src/Process/PhpExecutableFinder.php

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Webmozarts Console Parallelization package.
5+
*
6+
* (c) Webmozarts GmbH <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Webmozarts\Console\Parallelization\Process;
15+
16+
use Symfony\Component\Process\PhpExecutableFinder as SymfonyPhpExecutableFinder;
17+
use Webmozart\Assert\Assert;
18+
19+
final class PhpExecutableFinder
20+
{
21+
private static SymfonyPhpExecutableFinder $finder;
22+
23+
private function __construct()
24+
{
25+
}
26+
27+
public static function find(): string
28+
{
29+
$phpExecutable = self::getFinder()->find();
30+
31+
Assert::notFalse(
32+
$phpExecutable,
33+
'Could not find the PHP executable.',
34+
);
35+
36+
return $phpExecutable;
37+
}
38+
39+
private static function getFinder(): SymfonyPhpExecutableFinder
40+
{
41+
if (!isset(self::$finder)) {
42+
self::$finder = new SymfonyPhpExecutableFinder();
43+
}
44+
45+
return self::$finder;
46+
}
47+
}

0 commit comments

Comments
 (0)