Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 0 additions & 33 deletions .scrutinizer.yml

This file was deleted.

26 changes: 20 additions & 6 deletions src/Commands/Run.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace BlitzPHP\Tasks\Commands;

use BlitzPHP\Tasks\Task;
use BlitzPHP\Tasks\TaskRunner;

/**
Expand All @@ -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.',
];

/**
Expand All @@ -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;
Expand Down
20 changes: 16 additions & 4 deletions src/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string,mixed>
*/
protected string $name;
protected array $attributes = [];

/**
* @param mixed $action
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
}
9 changes: 5 additions & 4 deletions src/TaskRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace BlitzPHP\Tasks;

use Ahc\Cli\Output\Color;
use Ahc\Cli\Output\Writer;
use BlitzPHP\Utilities\Date;
use Throwable;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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()) {
Expand Down
Loading