protected function execute(InputInterface $input, OutputInterface $output)
{
$this->validateInput($input);
$refresh = $input->hasOption('refresh') && $input->getOption('refresh');
$environments = $this->api()->getEnvironments($this->getSelectedProject(), $refresh ? true : null);
if ($input->getOption('no-inactive')) {
$environments = array_filter($environments, function ($environment) {
return $environment->status !== 'inactive';
});
}
if ($input->getOption('sort')) {
$this->api()->sortResources($environments, $input->getOption('sort'));
}
if ($input->getOption('reverse')) {
$environments = array_reverse($environments, true);
}
if ($input->getOption('pipe')) {
$output->writeln(array_keys($environments));
return;
}
$project = $this->getSelectedProject();
$this->currentEnvironment = $this->getCurrentEnvironment($project);
if (($currentProject = $this->getCurrentProject()) && $currentProject == $project) {
$projectConfig = $this->localProject->getProjectConfig($this->getProjectRoot());
if (isset($projectConfig['mapping'])) {
$this->mapping = $projectConfig['mapping'];
}
}
$tree = $this->buildEnvironmentTree($environments);
// Add orphaned environments (those whose parents do not exist) and
// their children to the tree.
foreach ($environments as $id => $environment) {
if (!isset($tree[$id]) && !empty($environment->parent) && !isset($environments[$environment->parent])) {
$tree[$id] = $environment;
$this->children[$id] = $this->buildEnvironmentTree($environments, $id);
}
}
// To make the display nicer, we move all the children of master
// to the top level.
if (isset($this->children['master'])) {
$tree += $this->children['master'];
$this->children['master'] = [];
}
$headers = ['ID', 'Name', 'Status'];
$table = new Table($input, $output);
if ($table->formatIsMachineReadable()) {
$table->render($this->buildEnvironmentRows($tree, false, false), $headers);
return;
}
$this->stdErr->writeln("Your environments are: ");
$table->render($this->buildEnvironmentRows($tree), $headers);
if (!$this->currentEnvironment) {
return;
}
$this->stdErr->writeln("<info>*</info> - Indicates the current environment\n");
$currentEnvironment = $this->currentEnvironment;
$this->stdErr->writeln("Check out a different environment by running <info>" . self::$config->get('application.executable') . " checkout [id]</info>");
if ($currentEnvironment->operationAvailable('branch')) {
$this->stdErr->writeln("Branch a new environment by running <info>" . self::$config->get('application.executable') . " environment:branch [new-name]</info>");
}
if ($currentEnvironment->operationAvailable('activate')) {
$this->stdErr->writeln("Activate the current environment by running <info>" . self::$config->get('application.executable') . " environment:activate</info>");
}
if ($currentEnvironment->operationAvailable('delete')) {
$this->stdErr->writeln("Delete the current environment by running <info>" . self::$config->get('application.executable') . " environment:delete</info>");
}
if ($currentEnvironment->operationAvailable('backup')) {
$this->stdErr->writeln("Make a snapshot of the current environment by running <info>" . self::$config->get('application.executable') . " snapshot:create</info>");
}
if ($currentEnvironment->operationAvailable('merge')) {
$this->stdErr->writeln("Merge the current environment by running <info>" . self::$config->get('application.executable') . " environment:merge</info>");
}
if ($currentEnvironment->operationAvailable('synchronize')) {
$this->stdErr->writeln("Sync the current environment by running <info>" . self::$config->get('application.executable') . " environment:synchronize</info>");
}
}