ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
AgentCollection.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Setup;
22
23use ILIAS\Refinery\Factory as Refinery;
25use Symfony\Component\Console\Input\InputInterface;
26
30class AgentCollection implements Agent
31{
33
37 protected array $agents;
38
39 public function __construct(
41 array $agents
42 ) {
43 $this->refinery = $refinery;
44 $this->agents = $agents;
45 }
46
47 public function getAgent(string $key): ?Agent
48 {
49 return $this->agents[$key] ?? null;
50 }
51
52 public function withRemovedAgent(string $key): AgentCollection
53 {
54 $clone = clone $this;
55 unset($clone->agents[$key]);
56 return $clone;
57 }
58
59 public function withAdditionalAgent(string $key, Agent $agent): AgentCollection
60 {
61 if (isset($this->agents[$key])) {
62 throw new \LogicException("An agent with the name '$key' already exists.");
63 }
64 $clone = clone $this;
65 $clone->agents[$key] = $agent;
66 return $clone;
67 }
68
72 public function hasConfig(): bool
73 {
74 foreach ($this->agents as $c) {
75 if ($c->hasConfig()) {
76 return true;
77 }
78 }
79 return false;
80 }
81
86 {
87 return $this->refinery->in()->series([
88 $this->refinery->custom()->transformation(function ($in): array {
89 $out = [];
90 foreach ($this->agents as $key => $agent) {
91 if (!$agent->hasConfig()) {
92 continue;
93 }
94 $val = $in[$key] ?? null;
95 $transformation = $agent->getArrayToConfigTransformation();
96 $out[$key] = $transformation($val);
97 }
98 return $out;
99 }),
100 $this->refinery->custom()->transformation(fn($v): array => [$v]),
101 $this->refinery->to()->toNew(ConfigCollection::class)
102 ]);
103 }
104
108 public function getInstallObjective(?Config $config = null): Objective
109 {
110 if (!is_null($config)) {
111 $this->checkConfig($config);
112 }
113
114 return new ObjectiveCollection(
115 "Collected Install Objectives",
116 false,
117 ...array_values(array_map(
118 function (string $k, Agent $v) use ($config) {
119 if ($v->hasConfig()) {
120 return $v->getInstallObjective($config->getConfig($k));
121 } else {
122 return $v->getInstallObjective();
123 }
124 },
125 array_keys($this->agents),
126 array_values($this->agents)
127 ))
128 );
129 }
130
134 public function getUpdateObjective(?Config $config = null): Objective
135 {
136 if ($config !== null) {
137 $this->checkConfig($config);
138 }
139
140 return new ObjectiveCollection(
141 "Collected Update Objectives",
142 false,
143 ...array_values(array_map(
144 function (string $k, Agent $v) use ($config): \ILIAS\Setup\Objective {
145 if ($config !== null) {
146 return $v->getUpdateObjective($config->maybeGetConfig($k));
147 }
148 return $v->getUpdateObjective();
149 },
150 array_keys($this->agents),
151 array_values($this->agents)
152 ))
153 );
154 }
155
159 public function getBuildObjective(): Objective
160 {
161 return new ObjectiveCollection(
162 "Collected Build Artifact Objectives",
163 false,
164 ...array_values(array_map(
165 fn(Agent $v): \ILIAS\Setup\Objective => $v->getBuildObjective(),
166 $this->agents
167 ))
168 );
169 }
170
174 public function getStatusObjective(Metrics\Storage $storage): Objective
175 {
176 return new ObjectiveCollection(
177 "Collected Status Objectives",
178 false,
179 ...array_values(array_map(
180 fn(string $k, Agent $v): \ILIAS\Setup\Objective => $v->getStatusObjective(
181 new Metrics\StorageOnPathWrapper($k, $storage)
182 ),
183 array_keys($this->agents),
184 array_values($this->agents)
185 ))
186 );
187 }
188
192 public function getMigrations(): array
193 {
194 $migrations = [];
195 foreach ($this->agents as $agent_key => $agent) {
196 foreach ($agent->getMigrations() as $migration) {
200 $key = (new \ReflectionClass($migration))->getShortName();
201 $migrations[$agent_key . "." . $key] = $migration;
202 }
203 }
204
205 return $migrations;
206 }
207
208 protected function getKey(Migration $migration): string
209 {
210 $names = explode("\\", get_class($migration));
211 return array_pop($names);
212 }
213
214 protected function checkConfig(Config $config): void
215 {
216 if (!($config instanceof ConfigCollection)) {
217 throw new \InvalidArgumentException(
218 "Expected ConfigCollection for configuration."
219 );
220 }
221 }
222
226 public function getAgents(): array
227 {
228 return $this->agents;
229 }
230
232 public function getNamedObjectives(?Config $config = null): array
233 {
234 if (!is_null($config)) {
235 $this->checkConfig($config);
236 }
237
239 $namedObjectives = [];
240
241 foreach ($agents as $k => $agent) {
242 if ($config) {
243 $objectives = $agent->getNamedObjectives($config->maybeGetConfig($k));
244 } else {
245 $objectives = $agent->getNamedObjectives();
246 }
247 foreach ($objectives as $name => $constructor) {
248 $namedObjectives["$k.$name"] = $constructor;
249 }
250 }
251
252 ksort($namedObjectives);
253 return $namedObjectives;
254 }
255}
$out
Definition: buildRTE.php:24
Builds data types.
Definition: Factory.php:36
An agent that is just a collection of some other agents.
__construct(Refinery $refinery, array $agents)
getKey(Migration $migration)
getInstallObjective(?Config $config=null)
@inheritdocs
getUpdateObjective(?Config $config=null)
@inheritdocs
getNamedObjectives(?Config $config=null)
@inheritDoc
withAdditionalAgent(string $key, Agent $agent)
getArrayToConfigTransformation()
@inheritdocs
getStatusObjective(Metrics\Storage $storage)
@inheritdocs
A collection of some configurations.
A objective collection is a objective that is achieved once all subobjectives are achieved.
$c
Definition: deliver.php:25
A transformation is a function from one datatype to another.
A agent is some component that performs part of the setup process.
Definition: Agent.php:30
getMigrations()
Get a named map of migrations available for this Agent.
hasConfig()
Does this agent require a configuration?
getBuildObjective()
Get the goal the agent wants to achieve to build artifacts.
getStatusObjective(Metrics\Storage $storage)
Get the objective to be achieved when status is requested.
getUpdateObjective(?Config $config=null)
Get the goal the agent wants to achieve on update.
A configuration for the setup.
Definition: Config.php:27
A migration is a potentially long lasting operation that can be broken into discrete steps.
Definition: Migration.php:29
An objective is a desired state of the system that is supposed to be created by the setup.
Definition: Objective.php:31
Storage is simple key/value store without further schema definition.
Definition: Storage.php:30
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
$objectives