Skip to content

Commit f016ebd

Browse files
authored
Merge pull request #2 from blitz-php/devs
patch: amelioration de la commande `tasks:run`
2 parents b56a4d5 + 1f32cb9 commit f016ebd

4 files changed

Lines changed: 41 additions & 47 deletions

File tree

.scrutinizer.yml

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/Commands/Run.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace BlitzPHP\Tasks\Commands;
1515

16+
use BlitzPHP\Tasks\Task;
1617
use BlitzPHP\Tasks\TaskRunner;
1718

1819
/**
@@ -29,7 +30,7 @@ class Run extends TaskCommand
2930
* {@inheritDoc}
3031
*/
3132
protected $options = [
32-
'--task' => 'Run specific task by alias.',
33+
'--task' => 'Exécuter une tâche spécifique par son alias.',
3334
];
3435

3536
/**
@@ -50,18 +51,31 @@ public function execute(array $params)
5051
$this->newLine();
5152
}
5253

53-
$this->task('Exécution de tâches...');
54+
$this->task('Exécution de tâches...')->eol();
5455

55-
call_user_func(config('tasks.init'), service('scheduler'));
56+
call_user_func(config('tasks.init'), $scheduler = service('scheduler'));
5657

57-
$runner = new TaskRunner();
58+
$only = $this->option('task');
5859

59-
if ($task = $this->option('task')) {
60-
$runner->only([$task]);
60+
$tasks = collect($scheduler->getTasks())
61+
->filter(fn (Task $task) => $task->shouldRun())
62+
->filter(fn (Task $task) => $only === null ? true : $task->name === $only);
63+
64+
if ($tasks->isEmpty()) {
65+
$this->writer->error('Aucune tâche à exécuter.');
66+
67+
return EXIT_ERROR;
68+
}
69+
70+
$runner = new TaskRunner($scheduler);
71+
72+
if ($only) {
73+
$runner->only([$only]);
6174
}
6275

6376
$runner->run();
6477

78+
$this->eol()->border();
6579
$this->writer->ok('Tâches en cours d\'exécution terminées');
6680

6781
return EXIT_SUCCESS;

src/Task.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,11 @@ class Task
6868
protected array $environments = [];
6969

7070
/**
71-
* L'alias par lequel cette tâche peut être exécutée
71+
* Proprietés magiques emulées
72+
*
73+
* @var array<string,mixed>
7274
*/
73-
protected string $name;
75+
protected array $attributes = [];
7476

7577
/**
7678
* @param mixed $action
@@ -92,7 +94,7 @@ public function __construct(string $type, $action)
9294
*/
9395
public function named(string $name): self
9496
{
95-
$this->name = $name;
97+
$this->attributes['name'] = $name;
9698

9799
return $this;
98100
}
@@ -295,12 +297,22 @@ protected function buildName(): string
295297
*/
296298
public function __get(string $key)
297299
{
298-
if ($key === 'name' && empty($this->name)) {
300+
if ($key === 'name' && empty($this->attributes['name'])) {
299301
return $this->buildName();
300302
}
301303

302304
if (property_exists($this, $key)) {
303305
return $this->{$key};
304306
}
307+
308+
return $this->attributes[$key] ?? null;
309+
}
310+
311+
/**
312+
* Setter magique
313+
*/
314+
public function __set(string $name, mixed $value): void
315+
{
316+
$this->attributes[$name] = $value;
305317
}
306318
}

src/TaskRunner.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace BlitzPHP\Tasks;
1515

16+
use Ahc\Cli\Output\Color;
1617
use Ahc\Cli\Output\Writer;
1718
use BlitzPHP\Utilities\Date;
1819
use Throwable;
@@ -69,14 +70,14 @@ public function run(): void
6970
$start = Date::now();
7071
$output = null;
7172

72-
$this->cliWrite('Traitement: ' . ($task->name ?: 'Task'), 'green');
73+
$this->cliWrite('Traitement: ' . ($task->name ?: 'Task'), Color::GREEN);
7374

7475
try {
7576
$output = $task->run();
7677

77-
$this->cliWrite('Exécuté: ' . ($task->name ?: 'Task'), 'cyan');
78+
$this->cliWrite('Exécuté: ' . ($task->name ?: 'Task'), Color::CYAN);
7879
} catch (Throwable $e) {
79-
$this->cliWrite('Échoué: ' . ($task->name ?: 'Task'), 'red');
80+
$this->cliWrite('Échoué: ' . ($task->name ?: 'Task'), Color::RED);
8081

8182
logger()->error($e->getMessage(), $e->getTrace());
8283
$error = $e;
@@ -122,7 +123,7 @@ public function withTestTime(string $time): self
122123
/**
123124
* Ecrire une ligne dans l'interface de ligne de commande
124125
*/
125-
protected function cliWrite(string $text, ?string $foreground = null): void
126+
protected function cliWrite(string $text, ?int $foreground = null): void
126127
{
127128
// Sauter l'écriture pour cli dans les tests
128129
if (on_test()) {

0 commit comments

Comments
 (0)