Skip to content

Commit

Permalink
use command string instead of array
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigopedra committed Sep 16, 2024
1 parent 79fbf4a commit a4bfafb
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions src/Illuminate/Concurrency/ProcessDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Process\Factory as ProcessFactory;
use Illuminate\Process\Pool;
use Illuminate\Support\Arr;
use Illuminate\Support\ProcessUtils;
use Laravel\SerializableClosure\SerializableClosure;
use Symfony\Component\Process\PhpExecutableFinder;

Expand All @@ -26,18 +27,13 @@ public function __construct(protected ProcessFactory $processFactory)
*/
public function run(Closure|array $tasks): array
{
$php = $this->phpBinary();
$artisan = $this->artisanBinary();
$command = $this->buildCommand();

$results = $this->processFactory->pool(function (Pool $pool) use ($tasks, $php, $artisan) {
$results = $this->processFactory->pool(function (Pool $pool) use ($tasks, $command) {
foreach (Arr::wrap($tasks) as $task) {
$pool->path(base_path())->env([
'LARAVEL_INVOKABLE_CLOSURE' => serialize(new SerializableClosure($task)),
])->command([
$php,
$artisan,
'invoke-serialized-closure',
]);
])->command($command);
}
})->start()->wait();

Expand All @@ -59,22 +55,40 @@ public function run(Closure|array $tasks): array
*/
public function defer(Closure|array $tasks): DeferredCallback
{
return defer(fn () => $this->run($tasks));
$command = $this->buildCommand();

return defer(function () use ($tasks, $command) {
foreach (Arr::wrap($tasks) as $task) {
$this->processFactory->path(base_path())->env([
'LARAVEL_INVOKABLE_CLOSURE' => serialize(new SerializableClosure($task)),
])->run($command.' 2>&1 &');
}
});
}

/**
* Get the PHP binary.
*/
protected function phpBinary(): string
{
return (new PhpExecutableFinder)->find(false) ?: 'php';
return ProcessUtils::escapeArgument((new PhpExecutableFinder)->find(false) ?: 'php');
}

/**
* Get the Artisan binary.
*/
protected function artisanBinary(): string
{
return defined('ARTISAN_BINARY') ? ARTISAN_BINARY : 'artisan';
return ProcessUtils::escapeArgument(defined('ARTISAN_BINARY') ? ARTISAN_BINARY : 'artisan');
}

/**
* Build the command string.
*
* @return string
*/
protected function buildCommand(): string
{
return sprintf('%s %s invoke-serialized-closure', $this->phpBinary(), $this->artisanBinary());
}
}

0 comments on commit a4bfafb

Please sign in to comment.