Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion src/schedules/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,45 @@ 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);
}

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
*/
Expand Down