ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
AchieveCommand.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
28use Symfony\Component\Console\Input\InputInterface;
29use Symfony\Component\Console\Input\InputOption;
30use Symfony\Component\Console\Output\OutputInterface;
31use Symfony\Component\Console\Input\InputArgument;
36use InvalidArgumentException;
37
42{
43 use HasAgent;
45 use ObjectiveHelper;
46
47 protected static $defaultName = "achieve";
48
49 protected array $preconditions = [];
50
52
56 public function __construct(
57 AgentFinder $agent_finder,
58 ConfigReader $config_reader,
59 array $preconditions,
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 if ($input->getArgument("config")) {
140 $config = $this->readAgentConfig($agent, $input);
141 }
142
143 $namedObjectives = $agent->getNamedObjectives($config);
144
145 if (isset($namedObjectives[$objective_name])) {
146 $objective = $namedObjectives[$objective_name];
147 } else {
148 throw new InvalidArgumentException(
149 "There is no named objective '$objective_name'"
150 );
151 }
152
153 if ($this->preconditions !== []) {
154 $objective = new ObjectiveWithPreconditions(
155 $objective->create(),
156 ...$this->preconditions
157 );
158 } else {
159 $objective = $objective->create();
160 }
161
162 $environment = new ArrayEnvironment([
164 ]);
165 if ($config !== null) {
166 $environment = $this->addAgentConfigsToEnvironment($agent, $config, $environment);
167 }
168
169 try {
170 $this->achieveObjective($objective, $environment, $io);
171 $io->success("Achieved objective '$objective_name'. Thanks and have fun!");
172 } catch (NoConfirmationException $e) {
173 $io->error("Aborted the attempt to achieve '$objective_name', a necessary confirmation is missing:\n\n" . $e->getRequestedConfirmation());
174 }
175 }
176}
Builds data types.
Definition: Factory.php:36
Achieves an objective.
execute(InputInterface $input, OutputInterface $output)
executeAchieveObjective(IOWrapper $io, InputInterface $input)
shouldListNamedObjectives(InputInterface $input)
executeListNamedObjectives(IOWrapper $io, OutputInterface $output)
__construct(AgentFinder $agent_finder, ConfigReader $config_reader, array $preconditions, Refinery $refinery)
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:33
title(string $title)
Definition: IOWrapper.php:119
Signals that a necessary confirmation from the admin is missing.
A configuration with no content.
Definition: NullConfig.php:27
A wrapper around an objective that adds some preconditions.
An environment holds resources to be used in the setup process.
Definition: Environment.php:28
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
addAgentConfigsToEnvironment(Agent $agent, Config $config, Environment $environment)
trait HasAgent
Add this to an Command that has an agent.
Definition: HasAgent.php:30
getRelevantAgent(InputInterface $input)
Definition: HasAgent.php:40
readAgentConfig(Agent $agent, InputInterface $input, ?string $use_config_field=null)
trait HasConfigReader
Add this to an Command that has an config reader.