From 5b285d5f97defc2fd0e6e49b9fa056cfd6dffffb Mon Sep 17 00:00:00 2001 From: Dimitri Sitchet Tomkeu Date: Tue, 21 Jan 2025 18:21:18 +0100 Subject: [PATCH 1/2] patch: amelioration de la commande `tasks:run` --- src/Commands/Run.php | 26 ++++++++++++++++++++------ src/Task.php | 20 ++++++++++++++++---- src/TaskRunner.php | 9 +++++---- 3 files changed, 41 insertions(+), 14 deletions(-) diff --git a/src/Commands/Run.php b/src/Commands/Run.php index 60eeb1d..6add613 100644 --- a/src/Commands/Run.php +++ b/src/Commands/Run.php @@ -13,6 +13,7 @@ namespace BlitzPHP\Tasks\Commands; +use BlitzPHP\Tasks\Task; use BlitzPHP\Tasks\TaskRunner; /** @@ -29,7 +30,7 @@ class Run extends TaskCommand * {@inheritDoc} */ protected $options = [ - '--task' => 'Run specific task by alias.', + '--task' => 'Exécuter une tâche spécifique par son alias.', ]; /** @@ -50,18 +51,31 @@ public function execute(array $params) $this->newLine(); } - $this->task('Exécution de tâches...'); + $this->task('Exécution de tâches...')->eol(); - call_user_func(config('tasks.init'), service('scheduler')); + call_user_func(config('tasks.init'), $scheduler = service('scheduler')); - $runner = new TaskRunner(); + $only = $this->option('task'); - if ($task = $this->option('task')) { - $runner->only([$task]); + $tasks = collect($scheduler->getTasks()) + ->filter(fn (Task $task) => $task->shouldRun()) + ->filter(fn (Task $task) => $only === null ? true : $task->name === $only); + + if ($tasks->isEmpty()) { + $this->writer->error('Aucune tâche à exécuter.'); + + return EXIT_ERROR; + } + + $runner = new TaskRunner($scheduler); + + if ($only) { + $runner->only([$only]); } $runner->run(); + $this->eol()->border(); $this->writer->ok('Tâches en cours d\'exécution terminées'); return EXIT_SUCCESS; diff --git a/src/Task.php b/src/Task.php index 97ac93c..dc5cf5d 100644 --- a/src/Task.php +++ b/src/Task.php @@ -68,9 +68,11 @@ class Task protected array $environments = []; /** - * L'alias par lequel cette tâche peut être exécutée + * Proprietés magiques emulées + * + * @var array */ - protected string $name; + protected array $attributes = []; /** * @param mixed $action @@ -92,7 +94,7 @@ public function __construct(string $type, $action) */ public function named(string $name): self { - $this->name = $name; + $this->attributes['name'] = $name; return $this; } @@ -295,12 +297,22 @@ protected function buildName(): string */ public function __get(string $key) { - if ($key === 'name' && empty($this->name)) { + if ($key === 'name' && empty($this->attributes['name'])) { return $this->buildName(); } if (property_exists($this, $key)) { return $this->{$key}; } + + return $this->attributes[$key] ?? null; + } + + /** + * Setter magique + */ + public function __set(string $name, mixed $value): void + { + $this->attributes[$name] = $value; } } diff --git a/src/TaskRunner.php b/src/TaskRunner.php index fbe4591..b5f7e04 100644 --- a/src/TaskRunner.php +++ b/src/TaskRunner.php @@ -13,6 +13,7 @@ namespace BlitzPHP\Tasks; +use Ahc\Cli\Output\Color; use Ahc\Cli\Output\Writer; use BlitzPHP\Utilities\Date; use Throwable; @@ -69,14 +70,14 @@ public function run(): void $start = Date::now(); $output = null; - $this->cliWrite('Traitement: ' . ($task->name ?: 'Task'), 'green'); + $this->cliWrite('Traitement: ' . ($task->name ?: 'Task'), Color::GREEN); try { $output = $task->run(); - $this->cliWrite('Exécuté: ' . ($task->name ?: 'Task'), 'cyan'); + $this->cliWrite('Exécuté: ' . ($task->name ?: 'Task'), Color::CYAN); } catch (Throwable $e) { - $this->cliWrite('Échoué: ' . ($task->name ?: 'Task'), 'red'); + $this->cliWrite('Échoué: ' . ($task->name ?: 'Task'), Color::RED); logger()->error($e->getMessage(), $e->getTrace()); $error = $e; @@ -122,7 +123,7 @@ public function withTestTime(string $time): self /** * Ecrire une ligne dans l'interface de ligne de commande */ - protected function cliWrite(string $text, ?string $foreground = null): void + protected function cliWrite(string $text, ?int $foreground = null): void { // Sauter l'écriture pour cli dans les tests if (on_test()) { From 1f32cb91b8547cf3c45d24a59605dcdff89bc9d5 Mon Sep 17 00:00:00 2001 From: Dimitri Sitchet Tomkeu Date: Tue, 21 Jan 2025 18:22:53 +0100 Subject: [PATCH 2/2] tests: suppression de scrutinizer --- .scrutinizer.yml | 33 --------------------------------- 1 file changed, 33 deletions(-) delete mode 100644 .scrutinizer.yml diff --git a/.scrutinizer.yml b/.scrutinizer.yml deleted file mode 100644 index e90b220..0000000 --- a/.scrutinizer.yml +++ /dev/null @@ -1,33 +0,0 @@ -build: - nodes: - analysis: - project_setup: - override: true - tests: - override: - - php-scrutinizer-run - - - command: 'vendor/bin/kahlan --reporter=verbose --clover=clover.xml' - coverage: - file: 'clover.xml' - format: 'clover' - use_website_config: true - tests: true - -filter: - excluded_paths: [spec/*] - -checks: - php: true - -coding_style: - php: - spaces: - around_operators: - bitwise: false - concatenation: true - -tools: - external_code_coverage: - timeout: 600 - runs: 3