ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
AgentCollection.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 2019 Richard Klees <richard.klees@concepts-and-training.de> Extended GPL, see docs/LICENSE */
4 
5 namespace ILIAS\Setup;
6 
11 
15 class AgentCollection implements Agent
16 {
20  protected $refinery;
21 
25  protected $agents;
26 
27  public function __construct(
29  array $agents
30  ) {
31  $this->refinery = $refinery;
32  $this->agents = $agents;
33  }
34 
35  public function getAgent(string $key) : ?Agent
36  {
37  return $this->agents[$key] ?? null;
38  }
39 
40  public function withRemovedAgent(string $key) : AgentCollection
41  {
42  $clone = clone $this;
43  unset($clone->agents[$key]);
44  return $clone;
45  }
46 
47  public function withAdditionalAgent(string $key, Agent $agent) : AgentCollection
48  {
49  if (isset($this->agents[$key])) {
50  throw new \LogicException("An agent with the name '$key' already exists.");
51  }
52  $clone = clone $this;
53  $clone->agents[$key] = $agent;
54  return $clone;
55  }
56 
60  public function hasConfig() : bool
61  {
62  foreach ($this->agents as $c) {
63  if ($c->hasConfig()) {
64  return true;
65  }
66  }
67  return false;
68  }
69 
74  {
75  return $this->refinery->in()->series([
76  $this->refinery->custom()->transformation(function ($in) {
77  $out = [];
78  foreach ($this->agents as $key => $agent) {
79  if (!$agent->hasConfig()) {
80  continue;
81  }
82  $val = $in[$key] ?? null;
83  $transformation = $agent->getArrayToConfigTransformation();
84  $out[$key] = $transformation($val);
85  }
86  return $out;
87  }),
88  $this->refinery->custom()->transformation(function ($v) {
89  return [$v];
90  }),
91  $this->refinery->to()->toNew(ConfigCollection::class)
92  ]);
93  }
94 
98  public function getInstallObjective(Config $config = null) : Objective
99  {
100  if (!is_null($config)) {
101  $this->checkConfig($config);
102  }
103 
104  return new ObjectiveCollection(
105  "Collected Install Objectives",
106  false,
107  ...array_values(array_map(
108  function (string $k, Agent $v) use ($config) {
109  if ($v->hasConfig()) {
110  return $v->getInstallObjective($config->getConfig($k));
111  } else {
112  return $v->getInstallObjective();
113  }
114  },
115  array_keys($this->agents),
116  array_values($this->agents)
117  ))
118  );
119  }
120 
124  public function getUpdateObjective(Config $config = null) : Objective
125  {
126  if ($config) {
127  $this->checkConfig($config);
128  }
129 
130  return new ObjectiveCollection(
131  "Collected Update Objectives",
132  false,
133  ...array_values(array_map(
134  function (string $k, Agent $v) use ($config) {
135  if ($config) {
136  return $v->getUpdateObjective($config->maybeGetConfig($k));
137  }
138  return $v->getUpdateObjective();
139  },
140  array_keys($this->agents),
141  array_values($this->agents)
142  ))
143  );
144  }
145 
150  {
151  return new ObjectiveCollection(
152  "Collected Build Artifact Objectives",
153  false,
154  ...array_values(array_map(
155  function (Agent $v) {
156  return $v->getBuildArtifactObjective();
157  },
159  ))
160  );
161  }
162 
166  public function getStatusObjective(Metrics\Storage $storage) : Objective
167  {
168  return new ObjectiveCollection(
169  "Collected Status Objectives",
170  false,
171  ...array_values(array_map(
172  function (string $k, Agent $v) use ($storage) {
173  return $v->getStatusObjective(
174  new Metrics\StorageOnPathWrapper($k, $storage)
175  );
176  },
177  array_keys($this->agents),
178  array_values($this->agents)
179  ))
180  );
181  }
182 
186  public function getMigrations() : array
187  {
188  $migrations = [];
189  foreach ($this->agents as $agent_key => $agent) {
190  foreach ($agent->getMigrations() as $migration) {
194  $key = (new \ReflectionClass($migration))->getShortName();
195  $migrations[$agent_key . "." . $key] = $migration;
196  }
197  }
198 
199  return $migrations;
200  }
201 
205  public function getNamedObjective(string $name, Config $config = null) : Objective
206  {
207  $names = explode(".", $name);
208  $front = array_shift($names);
209  if (!isset($this->agents[$front])) {
210  throw new \InvalidArgumentException(
211  "Can't find named objective '$name'."
212  );
213  }
214 
215  if ($config) {
216  $this->checkConfig($config);
217  $config = $config->maybeGetConfig($front);
218  }
219 
220  try {
221  return $this->agents[$front]->getNamedObjective(implode(".", $names), $config);
222  } catch (\InvalidArgumentException $e) {
223  throw new \InvalidArgumentException(
224  "Can't find named objective '$name'.",
225  0,
226  $e
227  );
228  }
229  }
230 
231  protected function getKey(Setup\Migration $migration) : string
232  {
233  $names = explode("\\", get_class($migration));
234  return array_pop($names);
235  }
236 
237  protected function checkConfig(Config $config)
238  {
239  if (!($config instanceof ConfigCollection)) {
240  throw new \InvalidArgumentException(
241  "Expected ConfigCollection for configuration."
242  );
243  }
244  }
245 }
A objective collection is a objective that is achieved once all subobjectives are achieved...
getStatusObjective(Metrics\Storage $storage)
Get the objective to be achieved when status is requested.
getNamedObjective(string $name, Config $config=null)
getInstallObjective(Config $config=null)
An objective is a desired state of the system that is supposed to be created by the setup...
Definition: Objective.php:14
$c
Definition: cli.php:37
A migration is a potentially long lasting operation that can be broken into discrete steps...
Definition: Migration.php:12
getUpdateObjective(Config $config=null)
Get the goal the agent wants to achieve on update.
withAdditionalAgent(string $key, Agent $agent)
if(!array_key_exists('PATH_INFO', $_SERVER)) $config
Definition: metadata.php:68
An agent that is just a collection of some other agents.
getBuildArtifactObjective()
Get the goal the agent wants to achieve to build artifacts.
getInstallObjective(Config $config=null)
Get the goals the agent wants to achieve on setup.
A agent is some component that performs part of the setup process.
Definition: Agent.php:13
hasConfig()
Does this agent require a configuration?
if($format !==null) $name
Definition: metadata.php:230
__construct(Refinery $refinery, array $agents)
getMigrations()
Get a named map of migrations available for this Agent.
getKey(Setup\Migration $migration)
getUpdateObjective(Config $config=null)
if(php_sapi_name() !='cli') $in
Definition: Utf8Test.php:37
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
A transformation is a function from one datatype to another.
A configuration for the setup.
Definition: Config.php:10
A collection of some configurations.
getStatusObjective(Metrics\Storage $storage)