cfg = $config->getConfig('tasks'); } /** * @param Task $task * @return bool * @throws TasksException */ public function addTask(Task $task): bool { $taskStorage = $this->getTaskStorage(); return $taskStorage->addTask($task); } /** * @return SuperVisor * @throws TasksException */ public function getSuperVisor(): SuperVisor { if (isset($this->supervisor)) return $this->supervisor; // First get the configuration for SuperVisors $cfg = $this->cfg->get('SuperVisor'); // Select the SuperVisor type $type = $cfg['type']; // Load the class of the currently selected type $class = 'FuzeWorks\Async\Supervisors\\' . $type; // Fetch the parameters for the selected SuperVisor $parameters = isset($cfg[$type]['parameters']) && is_array($cfg[$type]['parameters']) ? $cfg[$type]['parameters'] : []; // Then add the TaskStorage and Executor to the parameters array_unshift($parameters, $this->getTaskStorage(), $this->getExecutor()); // If the type does not exist, throw an exception if (!class_exists($class, true)) throw new TasksException("Could not get SuperVisor. Type of '$class' not found."); // And load the SuperVisor and test if everything is in order $object = new $class(...$parameters); if (!$object instanceof SuperVisor) throw new TasksException("Could not get SuperVisor. Type of '$class' is not instanceof TaskStorage."); $this->supervisor = $object; return $object; } /** * @return ShellWorker * @throws TasksException */ public function getWorker(): ShellWorker { if (isset($this->shellWorker)) return $this->shellWorker; $this->shellWorker = new ShellWorker($this->getTaskStorage()); return $this->shellWorker; } /** * Fetch the TaskStorage based on the configured type * * @return TaskStorage * @throws TasksException */ public function getTaskStorage(): TaskStorage { if (isset($this->taskStorage)) return $this->taskStorage; // First get the configuration for TaskStorage $cfg = $this->cfg->get('TaskStorage'); // Select the TaskStorage type $type = $cfg['type']; // Load the class of the currently selected type $class = 'FuzeWorks\Async\TaskStorage\\' . $type; // Fetch the parameters for the selected type $parameters = isset($cfg[$type]['parameters']) && is_array($cfg[$type]['parameters']) ? $cfg[$type]['parameters'] : []; // If the type does not exist, throw an exception if (!class_exists($class, true)) throw new TasksException("Could not get TaskStorage. Type of '$class' not found."); // And load the TaskStorage and test if everything is in order $object = new $class($parameters); if (!$object instanceof TaskStorage) throw new TasksException("Could not get TaskStorage. Type '$class' is not instanceof TaskStorage."); $this->taskStorage = $object; return $this->taskStorage; } /** * Fetch the Executor based on the configured type * * @return Executor * @throws TasksException */ protected function getExecutor(): Executor { if (isset($this->executor)) return $this->executor; // First get the configuration for Executor $cfg = $this->cfg->get('Executor'); // Select the Executor type $type = $cfg['type']; // Load the class of the currently selected type $class = 'FuzeWorks\Async\Executors\\' . $type; // Fetch the parameters for the selected type $parameters = isset($cfg[$type]['parameters']) && is_array($cfg[$type]['parameters']) ? $cfg[$type]['parameters'] : []; // If the type does not exist, throw an exception if (!class_exists($class, true)) throw new TasksException("Could not get Executor. Type of '$class' not found."); // And load the Executor and test if everything is in order $object = new $class($parameters); if (!$object instanceof Executor) throw new TasksException("Could not get Executor. Type '$class' is not instanceof Executor."); $this->executor = $object; return $this->executor; } /** * @inheritDoc */ public function getClassesPrefix(): ?string { return null; } /** * @inheritDoc */ public function getSourceDirectory(): ?string { return null; } }