diff --git a/Command/AddRecurringConsoleJobToQueueCommand.php b/Command/AddRecurringConsoleJobToQueueCommand.php index 38e9bf9..ee17ad1 100644 --- a/Command/AddRecurringConsoleJobToQueueCommand.php +++ b/Command/AddRecurringConsoleJobToQueueCommand.php @@ -108,17 +108,28 @@ private function addRecurringJobs(OutputInterface $output) } $command = $configuration->getCommand(); - $arguments = []; - // i.e. does the command already have options or arguments within the string if (stripos($configuration->getCommand(), ' ') !== false) { - $command = trim(strstr($configuration->getCommand(), ' ', true)); - $arguments = explode(' ', trim(strstr($configuration->getCommand(), ' ', false))); + throw new \LogicException(sprintf('%s Command cannot contain spaces', $configuration->getCommand())); + } + + foreach ($configuration->getArguments() as $argument) { + if (!is_string($argument)) { + throw new \Exception('Argument was expected to be a string'); + } + + $this->validateNoQuotes($argument, $configuration); + + if (substr($argument, 0, 2) === '--') { + $optionValue = ltrim(strstr($argument, '='), '='); + + $this->validateNoQuotes($optionValue, $configuration); + } } $this->jobManager->addConsoleCommandJob( $command, - $arguments, + $configuration->getArguments(), $configuration->getTopic(), $configuration->getTimeout(), $configuration->getTimeout() @@ -141,4 +152,23 @@ private function maintainJobLogs() { $this->jobLogRepository->removeExpiredJobs(); } + + /** + * @param string $argument + * @param RecurringConsoleCommandConfiguration $configuration + * @throws \Exception + */ + private function validateNoQuotes(string $argument, RecurringConsoleCommandConfiguration $configuration): void + { + $firstCharacter = substr($argument, 0, 1); + $lastCharacter = substr($argument, strlen($argument)-1, 1); + + if ($firstCharacter === '"' && $lastCharacter === '"') { + throw new \Exception(sprintf('remove quotes as they will be included as literal values on %s', $configuration->getCommand())); + } + + if ($firstCharacter === "'" && $lastCharacter === "'") { + throw new \Exception(sprintf('remove quotes as they will be included as literal values on %s', $configuration->getCommand())); + } + } } diff --git a/Model/RecurringConsoleCommandConfiguration.php b/Model/RecurringConsoleCommandConfiguration.php index 2ef315e..da80a80 100644 --- a/Model/RecurringConsoleCommandConfiguration.php +++ b/Model/RecurringConsoleCommandConfiguration.php @@ -41,16 +41,23 @@ class RecurringConsoleCommandConfiguration private $envs; /** - * @param string $command - * @param string $topic - * @param string $schedule - * @param string|null $description + * @var array + */ + private $arguments; + + /** + * @param string $command + * @param array $arguments + * @param string $topic + * @param string $schedule + * @param string|null $description * @param integer|null $timeout * @param array|null $envs */ - public function __construct($command, $topic, $schedule, $description = null, $timeout = 60, $envs = null) + public function __construct($command, array $arguments, $topic, $schedule, $description = null, $timeout = 60, $envs = null) { $this->command = $command; + $this->arguments = $arguments; $this->schedule = $schedule; $this->topic = str_replace('-', '_', $topic); $this->timeout = $timeout; @@ -76,6 +83,14 @@ public function getCommand() return $this->command; } + /** + * @return array + */ + public function getArguments(): array + { + return $this->arguments; + } + /** * @return string */ diff --git a/Service/RecurringConsoleCommandReader.php b/Service/RecurringConsoleCommandReader.php index 97a45a3..3460e6b 100644 --- a/Service/RecurringConsoleCommandReader.php +++ b/Service/RecurringConsoleCommandReader.php @@ -129,8 +129,13 @@ private function parseConfiguration(array $config) throw new InvalidConfigurationException('`envs` config key must be an array or null'); } + if (isset($group['arguments']) && !is_array($group['arguments'])) { + throw new InvalidConfigurationException(sprintf('`arguments` config key must be an array for %s', $group['command'])); + } + $recurringConsoleCommandConfiguration = new RecurringConsoleCommandConfiguration( $group['command'], + $group['arguments'] ?? [], $group['topic'], $group['schedule'], isset($group['description']) ? $group['description'] : null, diff --git a/Tests/Model/RecurringConsoleCommandConfigurationTest.php b/Tests/Model/RecurringConsoleCommandConfigurationTest.php index f5b47a1..a918809 100644 --- a/Tests/Model/RecurringConsoleCommandConfigurationTest.php +++ b/Tests/Model/RecurringConsoleCommandConfigurationTest.php @@ -9,7 +9,7 @@ class RecurringConsoleCommandConfigurationTest extends TestCase { public function testCanBeConstructed() { - $config = new RecurringConsoleCommandConfiguration('foo:bar', 'test', '30 1 * * *', 'a short description'); + $config = new RecurringConsoleCommandConfiguration('foo:bar', [], 'test', '30 1 * * *', 'a short description'); $this->assertEquals($config->getCommand(), 'foo:bar'); $this->assertEquals($config->getTopic(), 'test'); $this->assertEquals($config->getSchedule(), '30 1 * * *');