diff --git a/src/schedules/Console.php b/src/schedules/Console.php index 41505d7..f923fd7 100644 --- a/src/schedules/Console.php +++ b/src/schedules/Console.php @@ -76,13 +76,15 @@ public static function isRunnable(): bool */ public function buildCommand(): array { + $arguments = $this->parseArguments($this->arguments); $command = [ PHP_BINARY, self::CRAFT_CLI_SCRIPT, $this->command, - $this->arguments, ]; + $command = array_merge($command, $arguments); + if ($this->user) { $command = array_merge(['sudo -u', $this->user], $command); } @@ -90,6 +92,29 @@ public function buildCommand(): array return $command; } + /** + * Split a raw argument string into individual arguments, respecting + * single- and double-quoted spans so a quoted value containing spaces + * is passed through as one argument instead of being split apart on + * every space. + * + * @param string|null $arguments + * @return string[] + */ + private function parseArguments(?string $arguments): array + { + if ($arguments === null || $arguments === '') { + return []; + } + + preg_match_all('/(?:[^\s"\']+|"[^"]*"|\'[^\']*\')+/', $arguments, $matches); + + return array_map( + static fn (string $arg): string => preg_replace(['/"([^"]*)"/', '/\'([^\']*)\'/'], '$1', $arg), + $matches[0] + ); + } + /** * @inheritdoc */