Skip to content

Commit

Permalink
Fix console options parsing with scheduler
Browse files Browse the repository at this point in the history
Co-Authored-By: Mohamed Said <themsaid@gmail.com>
  • Loading branch information
GrahamCampbell and themsaid committed May 2, 2020
1 parent cfc3ac9 commit 36e215d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
40 changes: 31 additions & 9 deletions src/Illuminate/Console/Scheduling/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,22 +199,44 @@ public function exec($command, array $parameters = [])
protected function compileParameters(array $parameters)
{
return collect($parameters)->map(function ($value, $key) {
if (is_array($value) && Str::startsWith($key, '--')) {
return collect($value)->map(function ($value) use ($key) {
return $key.'='.ProcessUtils::escapeArgument($value);
})->implode(' ');
} elseif (is_array($value)) {
$value = collect($value)->map(function ($value) {
return ProcessUtils::escapeArgument($value);
})->implode(' ');
} elseif (! is_numeric($value) && ! preg_match('/^(-.$|--.*)/i', $value)) {
if (is_array($value)) {
return $this->compileArrayInput($key, $value);
}

if (! is_numeric($value) && ! preg_match('/^(-.$|--.*)/i', $value)) {
$value = ProcessUtils::escapeArgument($value);
}

return is_numeric($key) ? $value : "{$key}={$value}";
})->implode(' ');
}

/**
* Compile array input for a command.
*
* @param string|int $key
* @param array $value
* @return string
*/
public function compileArrayInput($key, $value)
{
$value = collect($value)->map(function ($value) {
return ProcessUtils::escapeArgument($value);
});

if (Str::startsWith($key, '--')) {
$value = $value->map(function ($value) use ($key) {
return "{$key}={$value}";
});
} elseif (Str::startsWith($key, '-')) {
$value = $value->map(function ($value) use ($key) {
return "{$key} {$value}";
});
}

return $value->implode(' ');
}

/**
* Determine if the server is allowed to run this event.
*
Expand Down
6 changes: 6 additions & 0 deletions tests/Console/ConsoleEventSchedulerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public function testExecCreatesNewCommand()
$schedule->exec('path/to/command', ['--title' => 'A "real" test']);
$schedule->exec('path/to/command', [['one', 'two']]);
$schedule->exec('path/to/command', ['-1 minute']);
$schedule->exec('path/to/command', ['foo' => ['bar', 'baz']]);
$schedule->exec('path/to/command', ['--foo' => ['bar', 'baz']]);
$schedule->exec('path/to/command', ['-F' => ['bar', 'baz']]);

$events = $schedule->events();
$this->assertSame('path/to/command', $events[0]->command);
Expand All @@ -69,6 +72,9 @@ public function testExecCreatesNewCommand()
$this->assertSame("path/to/command --title={$escape}A {$escapeReal}real{$escapeReal} test{$escape}", $events[5]->command);
$this->assertSame("path/to/command {$escape}one{$escape} {$escape}two{$escape}", $events[6]->command);
$this->assertSame("path/to/command {$escape}-1 minute{$escape}", $events[7]->command);
$this->assertSame("path/to/command {$escape}bar{$escape} {$escape}baz{$escape}", $events[8]->command);
$this->assertSame("path/to/command --foo={$escape}bar{$escape} --foo={$escape}baz{$escape}", $events[9]->command);
$this->assertSame("path/to/command -F {$escape}bar{$escape} -F {$escape}baz{$escape}", $events[10]->command);
}

public function testExecCreatesNewCommandWithTimezone()
Expand Down

0 comments on commit 36e215d

Please sign in to comment.