Skip to content

Commit 36e215d

Browse files
Fix console options parsing with scheduler
Co-Authored-By: Mohamed Said <themsaid@gmail.com>
1 parent cfc3ac9 commit 36e215d

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

src/Illuminate/Console/Scheduling/Schedule.php

+31-9
Original file line numberDiff line numberDiff line change
@@ -199,22 +199,44 @@ public function exec($command, array $parameters = [])
199199
protected function compileParameters(array $parameters)
200200
{
201201
return collect($parameters)->map(function ($value, $key) {
202-
if (is_array($value) && Str::startsWith($key, '--')) {
203-
return collect($value)->map(function ($value) use ($key) {
204-
return $key.'='.ProcessUtils::escapeArgument($value);
205-
})->implode(' ');
206-
} elseif (is_array($value)) {
207-
$value = collect($value)->map(function ($value) {
208-
return ProcessUtils::escapeArgument($value);
209-
})->implode(' ');
210-
} elseif (! is_numeric($value) && ! preg_match('/^(-.$|--.*)/i', $value)) {
202+
if (is_array($value)) {
203+
return $this->compileArrayInput($key, $value);
204+
}
205+
206+
if (! is_numeric($value) && ! preg_match('/^(-.$|--.*)/i', $value)) {
211207
$value = ProcessUtils::escapeArgument($value);
212208
}
213209

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

214+
/**
215+
* Compile array input for a command.
216+
*
217+
* @param string|int $key
218+
* @param array $value
219+
* @return string
220+
*/
221+
public function compileArrayInput($key, $value)
222+
{
223+
$value = collect($value)->map(function ($value) {
224+
return ProcessUtils::escapeArgument($value);
225+
});
226+
227+
if (Str::startsWith($key, '--')) {
228+
$value = $value->map(function ($value) use ($key) {
229+
return "{$key}={$value}";
230+
});
231+
} elseif (Str::startsWith($key, '-')) {
232+
$value = $value->map(function ($value) use ($key) {
233+
return "{$key} {$value}";
234+
});
235+
}
236+
237+
return $value->implode(' ');
238+
}
239+
218240
/**
219241
* Determine if the server is allowed to run this event.
220242
*

tests/Console/ConsoleEventSchedulerTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ public function testExecCreatesNewCommand()
5959
$schedule->exec('path/to/command', ['--title' => 'A "real" test']);
6060
$schedule->exec('path/to/command', [['one', 'two']]);
6161
$schedule->exec('path/to/command', ['-1 minute']);
62+
$schedule->exec('path/to/command', ['foo' => ['bar', 'baz']]);
63+
$schedule->exec('path/to/command', ['--foo' => ['bar', 'baz']]);
64+
$schedule->exec('path/to/command', ['-F' => ['bar', 'baz']]);
6265

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

7480
public function testExecCreatesNewCommandWithTimezone()

0 commit comments

Comments
 (0)