From 93ff407d4f4c4ab26550dd8a7be33c2f3cd7c666 Mon Sep 17 00:00:00 2001 From: Jean-Christian Denis Date: Fri, 28 Apr 2023 01:04:10 +0200 Subject: [PATCH] add Tasks stack --- src/{Action.php => AbstractTask.php} | 0 src/Tasks.php | 77 ++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) rename src/{Action.php => AbstractTask.php} (100%) create mode 100644 src/Tasks.php diff --git a/src/Action.php b/src/AbstractTask.php similarity index 100% rename from src/Action.php rename to src/AbstractTask.php diff --git a/src/Tasks.php b/src/Tasks.php new file mode 100644 index 0000000..c78f9ad --- /dev/null +++ b/src/Tasks.php @@ -0,0 +1,77 @@ + $stack The tasks stack */ + private array $stack = []; + + /** + * Contructor load cleaners. + */ + public function __construct() + { + # --BEHAVIOR-- improveTaskAdd: Tasks + dcCore::app()->callBehavior('improveTaskAdd', $this); + + uasort($this->stack, fn ($a, $b) => $a->name() <=> $b->name()); + uasort($this->stack, fn ($a, $b) => $a->priority() <=> $b->priority()); + } + + /** + * Add an task. + * + * @param AbstractTask $task The task instance + * + * @return Tasks Self instance + */ + public function add(AbstractTask $task): Tasks + { + if (!isset($this->stack[$task->id()])) { + $this->stack[$task->id()] = $task; + } + + return $this; + } + + /** + * Get all tasks. + * + * @return array The tasks stack + */ + public function dump(): array + { + return $this->stack; + } + + /** + * Get a task. + * + * @param string $id The task id + * + * @return null|AbstractTask The task + */ + public function get(string $id): ?AbstractTask + { + return $this->stack[$id] ?? null; + } +}