Skip to content

Commit 0619021

Browse files
committed
fix(parameters) : add compileParameters method to Task to convert parameter string to proper parameters while executing the command programmaticaly or from schedule
1 parent 0f9215d commit 0619021

File tree

7 files changed

+45
-16
lines changed

7 files changed

+45
-16
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@ protected $hidden = true;
8888

8989
From L5.5 onwards all commands are auto registered, so this wouldn't be a problem.
9090

91+
#### Command Parameters
92+
93+
If your command requires arguments or options please use the optional command parameters field. You can provide parameters to your command as a string in the following manner
94+
95+
```text
96+
name=john.doe --greetings='Welcome to the new world'
97+
```
98+
In the example above, name is an argument while greetings is an option
99+
91100
#### Console Command
92101

93102
In addition to the dashboard, Totem provides an artisan command to view a list of scheduled task.

public/js/app.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/assets/js/tasks/components/TaskOutput.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<div class="uk-modal-header">
88
<h3>Output</h3>
99
</div>
10-
<div class="uk-modal-body">
10+
<div class="uk-modal-body uk-overflow-auto uk-height-max-large">
1111
<div v-html="output"></div>
1212
</div>
1313
<div class="uk-modal-footer">

src/Console/Kernel.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public function all()
3131
*/
3232
protected function commands()
3333
{
34+
// TODO: remove this method while 5.5 branching
3435
if (method_exists($this, 'load')) {
3536
$this->load(base_path('app/Console/Commands'));
3637
}

src/Providers/ConsoleServiceProvider.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ class ConsoleServiceProvider extends ServiceProvider
1414
*
1515
* @return void
1616
*/
17-
public function register()
17+
public function boot()
1818
{
19-
$this->app->booted(function () {
19+
// TODO: refactor this to resolving callback while 5.5 branching
20+
$this->app->booted(function () {
2021
if ($this->app->runningInConsole()) {
2122
$this->schedule($this->app->make(Schedule::class));
2223
}
@@ -33,7 +34,8 @@ public function schedule(Schedule $schedule)
3334
$tasks = app('totem.tasks')->findAllActive();
3435

3536
$tasks->each(function ($task) use ($schedule) {
36-
$event = $schedule->command($task->command.' '.$task->parameters);
37+
38+
$event = $schedule->command($task->command, $task->compileParameters(true));
3739

3840
$event->cron($task->getCronExpression())
3941
->name($task->description)

src/Repositories/EloquentTaskRepository.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,17 +176,10 @@ public function deactivate($id)
176176
public function execute($id)
177177
{
178178
$task = $this->find($id);
179-
180179
$start = microtime(true);
181-
182180
try {
183-
$parameters = collect(explode(' ', $task->parameters))->mapWithKeys(function ($parameter) {
184-
$param = explode('=', $parameter);
185-
186-
return count($param) > 1 ? [$param[0] => $param[1]] : $param;
187-
})->toArray();
181+
Artisan::call($task->command, $task->compileParameters());
188182

189-
Artisan::call($task->command, $parameters);
190183
file_put_contents(storage_path($task->getMutexName()), Artisan::output());
191184
} catch (\Exception $e) {
192185
file_put_contents(storage_path($task->getMutexName()), $e->getMessage());

src/Task.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,30 @@ public function getUpcomingAttribute()
6060
return CronExpression::factory($this->getCronExpression())->getNextRunDate()->format('Y-m-d H:i:s');
6161
}
6262

63+
/**
64+
* Convert a string of command arguments and options to an array.
65+
*
66+
* @param bool $console if true will convert arguments to non associative array
67+
* @return array
68+
*/
69+
public function compileParameters($console = false)
70+
{
71+
if ($this->parameters) {
72+
$regex = '/(?=\S)[^\'"\s]*(?:\'[^\']*\'[^\'"\s]*|"[^"]*"[^\'"\s]*)*/';
73+
preg_match_all($regex, $this->parameters, $matches, PREG_SET_ORDER, 0);
74+
75+
$parameters = collect($matches)->mapWithKeys(function ($parameter) use ($console) {
76+
$param = explode('=', $parameter[0]);
77+
78+
return count($param) > 1 ? ($console ? ((starts_with($param[0], '--') ? [$param[0] => $param[1]] : [$param[1]])) : [$param[0] => $param[1]]) : $param;
79+
})->toArray();
80+
81+
return $parameters;
82+
}
83+
84+
return [];
85+
}
86+
6387
/**
6488
* Results Relation.
6589
*

0 commit comments

Comments
 (0)