ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
AchieveCommand.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
21 namespace ILIAS\Setup\CLI;
22 
37 
41 class AchieveCommand extends Command
42 {
43  use HasAgent;
44  use HasConfigReader;
45  use ObjectiveHelper;
46 
47  protected static $defaultName = "achieve";
48 
49  protected array $preconditions = [];
50 
51  protected Refinery $refinery;
52 
56  public function __construct(
57  AgentFinder $agent_finder,
58  ConfigReader $config_reader,
59  array $preconditions,
60  Refinery $refinery
61  ) {
63  $this->agent_finder = $agent_finder;
64  $this->config_reader = $config_reader;
65  $this->preconditions = $preconditions;
66  $this->refinery = $refinery;
67  }
68 
69  protected function configure(): void
70  {
71  $this->setDescription("Achieve a named objective from an agent.");
72  $this->addArgument(
73  "objective",
74  InputArgument::OPTIONAL,
75  "Objective to be execute from an agent. Format: \$AGENT::\$OBJECTIVE"
76  );
77  $this->addArgument("config", InputArgument::OPTIONAL, "Configuration file for the installation");
78  $this->addOption(
79  "config",
80  null,
81  InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
82  "Define fields in the configuration file that should be overwritten, e.g. \"a.b.c=foo\"",
83  []
84  );
85  $this->addOption("yes", "y", InputOption::VALUE_NONE, "Confirm every message of the objective.");
86  $this->addOption("list", null, InputOption::VALUE_NONE, "Lists all achievable objectives");
87  }
88 
89  protected function execute(InputInterface $input, OutputInterface $output): int
90  {
91  $io = new IOWrapper($input, $output);
92  $io->printLicenseMessage();
93 
94  if ($this->shouldListNamedObjectives($input)) {
95  $this->executeListNamedObjectives($io, $output);
96  return 0;
97  }
98 
99  $this->executeAchieveObjective($io, $input);
100 
101  return 0;
102  }
103 
104  private function shouldListNamedObjectives(InputInterface $input): bool
105  {
106  return
107  (
108  $input->getOption("list") !== null
109  && is_bool($input->getOption("list"))
110  && $input->getOption("list")
111  )
112  ||
113  (
114  $input->getArgument("objective") === ""
115  || $input->getArgument("objective") === null
116  );
117  }
118 
119  private function executeListNamedObjectives(IOWrapper $io, OutputInterface $output): void
120  {
121  $io->title("Listing available objectives");
122 
123  $agentCollection = $this->agent_finder->getAgents();
124  foreach ($agentCollection->getNamedObjectives(null) as $cmd => $objectiveCollection) {
125  $output->write(str_pad($cmd, IOWrapper::LABEL_WIDTH));
126  $output->writeln($objectiveCollection->getDescription());
127  }
128  $output->writeln("");
129  }
130 
131  private function executeAchieveObjective(IOWrapper $io, InputInterface $input): void
132  {
133  $agent = $this->getRelevantAgent($input);
134  $objective_name = $input->getArgument('objective');
135 
136  $io->title("Achieve objective: $objective_name");
137 
138  $config = null;
139 
140  if ($input->getArgument("config")) {
141  $config = $this->readAgentConfig($agent, $input);
142  }
143 
144  $namedObjectives = $agent->getNamedObjectives($config);
145 
146  if (isset($namedObjectives[$objective_name])) {
147  $objective = $namedObjectives[$objective_name];
148  } else {
149  throw new InvalidArgumentException(
150  "There is no named objective '$objective_name'"
151  );
152  }
153 
154  if ($this->preconditions !== []) {
155  $objective = new ObjectiveWithPreconditions(
156  $objective->create(),
157  ...$this->preconditions
158  );
159  } else {
160  $objective = $objective->create();
161  }
162 
163  $environment = new ArrayEnvironment([
165  ]);
166  if ($config !== null) {
167  $environment = $this->addAgentConfigsToEnvironment($agent, $config, $environment);
168  }
169 
170  try {
171  $this->achieveObjective($objective, $environment, $io);
172  $io->success("Achieved objective '$objective_name'. Thanks and have fun!");
173  } catch (NoConfirmationException $e) {
174  $io->error("Aborted the attempt to achieve '$objective_name', a necessary confirmation is missing:\n\n" . $e->getRequestedConfirmation());
175  }
176  }
177 }
Read a json-formatted config from a file and overwrite some fields.
Wrapper around symfonies input and output facilities to provide just the functionality required for t...
Definition: IOWrapper.php:32
if(!array_key_exists('PATH_INFO', $_SERVER)) $config
Definition: metadata.php:85
executeListNamedObjectives(IOWrapper $io, OutputInterface $output)
__construct(AgentFinder $agent_finder, ConfigReader $config_reader, array $preconditions, Refinery $refinery)
A wrapper around an objective that adds some preconditions.
addAgentConfigsToEnvironment(Agent $agent, Config $config, Environment $environment)
execute(InputInterface $input, OutputInterface $output)
getRelevantAgent(InputInterface $input)
Definition: HasAgent.php:40
executeAchieveObjective(IOWrapper $io, InputInterface $input)
readAgentConfig(Agent $agent, InputInterface $input, string $use_config_field=null)
title(string $title)
Definition: IOWrapper.php:119
shouldListNamedObjectives(InputInterface $input)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(Container $dic, ilPlugin $plugin)
trait HasAgent
Add this to an Command that has an agent.
Definition: HasAgent.php:30
Signals that a necessary confirmation from the admin is missing.
trait HasConfigReader
Add this to an Command that has an config reader.
Achieves an objective.