From 36e215dd39cd757a8ffc6b17794de60476b2289d Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Sat, 2 May 2020 10:36:34 +0100 Subject: [PATCH] Fix console options parsing with scheduler Co-Authored-By: Mohamed Said --- .../Console/Scheduling/Schedule.php | 40 ++++++++++++++----- tests/Console/ConsoleEventSchedulerTest.php | 6 +++ 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/Illuminate/Console/Scheduling/Schedule.php b/src/Illuminate/Console/Scheduling/Schedule.php index b124ace71cf1..4ced59e3d721 100644 --- a/src/Illuminate/Console/Scheduling/Schedule.php +++ b/src/Illuminate/Console/Scheduling/Schedule.php @@ -199,15 +199,11 @@ 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); } @@ -215,6 +211,32 @@ protected function compileParameters(array $parameters) })->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. * diff --git a/tests/Console/ConsoleEventSchedulerTest.php b/tests/Console/ConsoleEventSchedulerTest.php index d772a45fcc00..a45c4531206a 100644 --- a/tests/Console/ConsoleEventSchedulerTest.php +++ b/tests/Console/ConsoleEventSchedulerTest.php @@ -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); @@ -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()