Skip to content

Commit a9a0977

Browse files
authored
Merge pull request #105 from usemarkup/fix/add-native-argument-support-for-recurring-jobs
fix: add native argument support for recurring jobs
2 parents 40eb3ca + 4560ae7 commit a9a0977

File tree

4 files changed

+61
-11
lines changed

4 files changed

+61
-11
lines changed

Command/AddRecurringConsoleJobToQueueCommand.php

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,28 @@ private function addRecurringJobs(OutputInterface $output)
108108
}
109109

110110
$command = $configuration->getCommand();
111-
$arguments = [];
112111

113-
// i.e. does the command already have options or arguments within the string
114112
if (stripos($configuration->getCommand(), ' ') !== false) {
115-
$command = trim(strstr($configuration->getCommand(), ' ', true));
116-
$arguments = explode(' ', trim(strstr($configuration->getCommand(), ' ', false)));
113+
throw new \LogicException(sprintf('%s Command cannot contain spaces', $configuration->getCommand()));
114+
}
115+
116+
foreach ($configuration->getArguments() as $argument) {
117+
if (!is_string($argument)) {
118+
throw new \Exception('Argument was expected to be a string');
119+
}
120+
121+
$this->validateNoQuotes($argument, $configuration);
122+
123+
if (substr($argument, 0, 2) === '--') {
124+
$optionValue = ltrim(strstr($argument, '='), '=');
125+
126+
$this->validateNoQuotes($optionValue, $configuration);
127+
}
117128
}
118129

119130
$this->jobManager->addConsoleCommandJob(
120131
$command,
121-
$arguments,
132+
$configuration->getArguments(),
122133
$configuration->getTopic(),
123134
$configuration->getTimeout(),
124135
$configuration->getTimeout()
@@ -141,4 +152,23 @@ private function maintainJobLogs()
141152
{
142153
$this->jobLogRepository->removeExpiredJobs();
143154
}
155+
156+
/**
157+
* @param string $argument
158+
* @param RecurringConsoleCommandConfiguration $configuration
159+
* @throws \Exception
160+
*/
161+
private function validateNoQuotes(string $argument, RecurringConsoleCommandConfiguration $configuration): void
162+
{
163+
$firstCharacter = substr($argument, 0, 1);
164+
$lastCharacter = substr($argument, strlen($argument)-1, 1);
165+
166+
if ($firstCharacter === '"' && $lastCharacter === '"') {
167+
throw new \Exception(sprintf('remove quotes as they will be included as literal values on %s', $configuration->getCommand()));
168+
}
169+
170+
if ($firstCharacter === "'" && $lastCharacter === "'") {
171+
throw new \Exception(sprintf('remove quotes as they will be included as literal values on %s', $configuration->getCommand()));
172+
}
173+
}
144174
}

Model/RecurringConsoleCommandConfiguration.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,23 @@ class RecurringConsoleCommandConfiguration
4141
private $envs;
4242

4343
/**
44-
* @param string $command
45-
* @param string $topic
46-
* @param string $schedule
47-
* @param string|null $description
44+
* @var array
45+
*/
46+
private $arguments;
47+
48+
/**
49+
* @param string $command
50+
* @param array $arguments
51+
* @param string $topic
52+
* @param string $schedule
53+
* @param string|null $description
4854
* @param integer|null $timeout
4955
* @param array|null $envs
5056
*/
51-
public function __construct($command, $topic, $schedule, $description = null, $timeout = 60, $envs = null)
57+
public function __construct($command, array $arguments, $topic, $schedule, $description = null, $timeout = 60, $envs = null)
5258
{
5359
$this->command = $command;
60+
$this->arguments = $arguments;
5461
$this->schedule = $schedule;
5562
$this->topic = str_replace('-', '_', $topic);
5663
$this->timeout = $timeout;
@@ -76,6 +83,14 @@ public function getCommand()
7683
return $this->command;
7784
}
7885

86+
/**
87+
* @return array
88+
*/
89+
public function getArguments(): array
90+
{
91+
return $this->arguments;
92+
}
93+
7994
/**
8095
* @return string
8196
*/

Service/RecurringConsoleCommandReader.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,13 @@ private function parseConfiguration(array $config)
129129
throw new InvalidConfigurationException('`envs` config key must be an array or null');
130130
}
131131

132+
if (isset($group['arguments']) && !is_array($group['arguments'])) {
133+
throw new InvalidConfigurationException(sprintf('`arguments` config key must be an array for %s', $group['command']));
134+
}
135+
132136
$recurringConsoleCommandConfiguration = new RecurringConsoleCommandConfiguration(
133137
$group['command'],
138+
$group['arguments'] ?? [],
134139
$group['topic'],
135140
$group['schedule'],
136141
isset($group['description']) ? $group['description'] : null,

Tests/Model/RecurringConsoleCommandConfigurationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class RecurringConsoleCommandConfigurationTest extends TestCase
99
{
1010
public function testCanBeConstructed()
1111
{
12-
$config = new RecurringConsoleCommandConfiguration('foo:bar', 'test', '30 1 * * *', 'a short description');
12+
$config = new RecurringConsoleCommandConfiguration('foo:bar', [], 'test', '30 1 * * *', 'a short description');
1313
$this->assertEquals($config->getCommand(), 'foo:bar');
1414
$this->assertEquals($config->getTopic(), 'test');
1515
$this->assertEquals($config->getSchedule(), '30 1 * * *');

0 commit comments

Comments
 (0)