/** * @param array $task * @param string $prefix * @param integer $indent * @return bool */ public function executeTask(array $task, $prefix, $indent) { $this->log('<task> TASK </task> ' . $prefix); $className = isset($task['.task']) && !empty($task['.task']) ? is_array($task['.task']) ? $task['.task']['class'] : $task['.task'] : 'cookyii\\build\\tasks\\BlankTask'; $params = isset($task['.task']) && !empty($task['.task']) ? is_array($task['.task']) ? $task['.task'] : [] : []; unset($params['class']); $params['command'] = $this; if (!class_exists($className)) { throw new \RuntimeException(sprintf('Class "%s" not found.', $className)); } if ($this->output->isVerbose()) { $this->log('<comment>[class]</comment> ', $indent + 1, false); $this->log($className); } $Event = new TaskEvent($this, $prefix, $task, $indent + 1); if (!$this->dispatch(static::EVENT_BEFORE_CONFIGURE_TASK, $Event)) { return false; } /** @var \cookyii\build\tasks\AbstractTask $Task */ $Task = Component::createObject($className, $params); Component::addEventListeners($Task->events()); if (isset($task['.events']) && !empty($task['.events'])) { Component::addEventListeners($task['.events']); } $attributes = isset($task['.task']) && !empty($task['.task']) ? is_array($task['.task']) ? $task['.task'] : [] : []; $attributes['prefix'] = $prefix; $attributes['indent'] = $indent; if ($this->output->isDebug()) { $this->log('<comment>[attributes]</comment>', $indent + 1); $this->log(print_r($attributes, 1), $indent + 2); } $Task->configure($attributes); $EventTask = new TaskEvent($this, $prefix, $Task, $indent + 1); if (!$this->dispatch(AbstractTask::EVENT_AFTER_INITIALIZE, $EventTask)) { return false; } if (!$this->dispatch(static::EVENT_AFTER_CONFIGURE_TASK, $EventTask)) { return false; } if (isset($task['.depends']) && !empty($task['.depends'])) { if ($this->output->isVerbose()) { $this->log('<comment>[depends]</comment>', $indent + 1); $chunks = array_chunk($task['.depends'], 4); foreach ($chunks as $chunk) { $this->log(implode(', ', $chunk), $indent + 2); } } foreach ($task['.depends'] as $depend_name) { $depend = $this->findTask($depend_name); $result = $this->executeTask($depend, $prefix . '[' . $depend_name . ']', $indent + 1); if (false === $result) { $this->log('<task-error> TASK </task-error> <error>' . $prefix . '[' . $depend_name . '] failure.</error>'); return false; } } } if (!$this->dispatch(static::EVENT_BEFORE_RUN_TASK, $EventTask)) { return false; } if (!$this->dispatch(AbstractTask::EVENT_BEFORE_RUN, $EventTask)) { return false; } $result = $Task->run(); if (!$this->dispatch(AbstractTask::EVENT_AFTER_RUN, $EventTask)) { return false; } if (!$this->dispatch(static::EVENT_AFTER_RUN_TASK, $EventTask)) { return false; } if ($this->output->isVeryVerbose()) { $this->log(sprintf('<comment>[executed]</comment> %s', get_class($Task)), $indent + 1); } if (isset($task['.events']) && !empty($task['.events'])) { Component::removeEventListeners($task['.events']); } Component::removeEventListeners($Task->events()); return $result; }
/** * @param array $config * @return array */ private function expandCompositeTasks(array $config) { if (!empty($config)) { foreach ($config as $task_name => $conf) { if (is_array($conf) && isset($conf['.task']) && !empty($conf['.task'])) { $className = is_array($conf['.task']) ? $conf['.task']['class'] : $conf['.task']; $params = is_array($conf['.task']) ? $conf['.task'] : []; unset($params['class']); $params['command'] = $this->command; /** @var AbstractCompositeTask $Task */ $Task = Component::createObject($className, $params); if ($Task instanceof AbstractCompositeTask) { $tasks = $Task->tasks(); if (!empty($tasks)) { $config[$task_name] = array_merge($conf, $tasks); } } } } } return $config; }