/** * Resolves the environment configuration and maps the given servers to the given tasks, * according to the defined environment. * * @param array $environmentOptions Environment options * @param Environment $environment Environment * @param Project $project Project * * @return Mapping */ public function resolve(array $environmentOptions, Environment $environment, Project $project) { if (!array_key_exists('servers', $environmentOptions) || empty($environmentOptions['servers'])) { throw new \DomainException(sprintf("No servers for environment '%s' given", $environment->getName())); } if (!array_key_exists('stages', $environmentOptions) || empty($environmentOptions['stages'])) { throw new \DomainException(sprintf("No stages for environment '%s' given", $environment->getName())); } $this->logger->debug(sprintf("Resolving mapping for environment '%s'.", $environment->getName())); $mapping = new Mapping(); foreach ($environmentOptions['stages'] as $stageName => $stageOptions) { foreach ($stageOptions as $taskName => $taskOptions) { $stage = $environment->getStage($stageName); $task = $stage->getTask($taskName); if ($task instanceof LocalTask) { $this->logger->debug(sprintf("Local task '%s' in stage '%s' given. No servers will be added.", $taskName, $stageName)); $mapping->add($stage, $task); continue; } if (array_key_exists('servers', $taskOptions)) { foreach ($taskOptions['servers'] as $serverName) { $this->logger->debug(sprintf("Adding server '%s' to task '%s' in stage '%s'.", $serverName, $taskName, $stageName)); $mapping->add($stage, $task, $project->getServer($serverName)); } } else { foreach ($environmentOptions['servers'] as $serverName) { $this->logger->debug(sprintf("Adding server '%s' to task '%s' in stage '%s'.", $serverName, $taskName, $stageName)); $mapping->add($stage, $task, $project->getServer($serverName)); } } } } return $mapping; }
/** * Builds an environment. * * @param string $name Name * @param array $environment Environment * @param Project $project Project instance * * @return Environment */ public function build($name, array $environment, Project $project) { Assertion::string($name); $this->logger->debug(sprintf("Building environment '%s'", $name)); if (empty($environment)) { throw new \DomainException(sprintf("No configuration for environment '%s' given", $name)); } if (!array_key_exists('servers', $environment) || empty($environment['servers'])) { throw new \DomainException(sprintf("No servers for environment '%s' given", $name)); } if (!array_key_exists('stages', $environment) || empty($environment['stages'])) { throw new \DomainException(sprintf("No stages for environment '%s' given", $name)); } $this->tasks = $project->getTasks(); $this->logger->debug("Creating environment instance."); $options = $environment; $environment = new Environment($name); if (array_key_exists('config', $options)) { $this->logger->debug("Config section given. Hydrating config instance."); $environment->getConfig()->merge(new Config($options['config'])); } else { $this->logger->debug("No config section given."); } foreach ($options['stages'] as $name => $stage) { if (!$stage) { throw new \DomainException(sprintf("No tasks for stage '%s' in environment '%s' given", $name, $environment->getName())); } $environment->addStage($this->buildStage($name, $stage)); } $this->logger->debug(sprintf("Resolving task/server mapping.")); $environment->setMapping($this->mappingResolver->resolve($options, $environment, $project)); return $environment; }
/** * Prints an configured environment. * * @param Environment $environment Environment * @param OutputInterface $output Output * * @return null */ private function printEnvironment(Environment $environment, OutputInterface $output) { $output->writeln('+-----'); $output->writeln(sprintf('| Environment: <info>%s</info>', $environment->getName())); $table = new Table($output); $table->addRow(array('Stage', 'Order', 'Name', 'Task', 'Server')); $mapping = $environment->getMapping(); foreach ($environment->getStages() as $stage) { $sequence = 1; $table->addRow(new TableSeparator()); foreach ($stage->getTasks() as $task) { if ($task instanceof RemoteTask) { $servers = $this->implodeServerNames($mapping->getServersByTask($stage, $task)); } else { $servers = '-'; } $table->addRow(array($stage->getName(), $sequence, $task->getName(), get_class($task), $servers)); $sequence++; } } $table->render(); $output->writeln(''); }