ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
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
5namespace ILIAS\Setup;
6
9use ILIAS\Refinery\Factory as Refinery;
11
15class AgentCollection implements Agent
16{
20 protected $field_factory;
21
25 protected $refinery;
26
30 protected $agents;
31
32 public function __construct(
33 FieldFactory $field_factory,
34 Refinery $refinery,
35 array $agents
36 ) {
37 $this->field_factory = $field_factory;
38 $this->refinery = $refinery;
39 $this->agents = $agents;
40 }
41
45 public function hasConfig() : bool
46 {
47 foreach ($this->agents as $c) {
48 if ($c->hasConfig()) {
49 return true;
50 }
51 }
52 return false;
53 }
54
58 public function getConfigInput(Config $config = null) : Input
59 {
60 if ($config !== null) {
61 $this->checkConfig($config);
62 }
63
64 $inputs = [];
65 foreach ($this->getAgentsWithConfig() as $k => $c) {
66 if ($config) {
67 $inputs[$k] = $c->getConfigInput($config->getConfig($k));
68 } else {
69 $inputs[$k] = $c->getConfigInput();
70 }
71 }
72
73 return $this->field_factory->group($inputs)
74 ->withAdditionalTransformation(
75 $this->refinery->in()->series([
76 $this->refinery->custom()->transformation(function ($v) {
77 return [$v];
78 }),
79 $this->refinery->to()->toNew(ConfigCollection::class)
80 ])
81 );
82 }
83
88 {
89 return $this->refinery->in()->series([
90 $this->refinery->custom()->transformation(function ($in) {
91 $out = [];
92 foreach ($this->agents as $key => $agent) {
93 if (!$agent->hasConfig()) {
94 continue;
95 }
96 $val = $in[$key] ?? null;
97 $transformation = $agent->getArrayToConfigTransformation();
98 $out[$key] = $transformation($val);
99 }
100 return $out;
101 }),
102 $this->refinery->custom()->transformation(function ($v) {
103 return [$v];
104 }),
105 $this->refinery->to()->toNew(ConfigCollection::class)
106 ]);
107 }
108
112 public function getInstallObjective(Config $config = null) : Objective
113 {
114 return $this->getXObjective("getInstallObjective", $config);
115 }
116
120 public function getUpdateObjective(Config $config = null) : Objective
121 {
122 return $this->getXObjective("getUpdateObjective", $config);
123 }
124
129 {
130 $gs = [];
131 foreach ($this->agents as $k => $c) {
132 $gs[] = $c->getBuildArtifactObjective();
133 }
134 return new ObjectiveCollection("Collected Build Artifact Objectives", false, ...$gs);
135 }
136
137 protected function getXObjective(string $which, Config $config = null) : Objective
138 {
139 $this->checkConfig($config);
140
141 $gs = [];
142 foreach ($this->agents as $k => $c) {
143 if ($c->hasConfig()) {
144 $gs[] = call_user_func([$c, $which], $config->getConfig($k));
145 } else {
146 $gs[] = call_user_func([$c, $which]);
147 }
148 }
149
150 return new ObjectiveCollection("Collected Objectives", false, ...$gs);
151 }
152
153 protected function checkConfig(Config $config)
154 {
155 if (!($config instanceof ConfigCollection)) {
156 throw new \InvalidArgumentException(
157 "Expected ConfigCollection for configuration."
158 );
159 }
160 }
161
162 protected function getAgentsWithConfig() : \Traversable
163 {
164 foreach ($this->agents as $k => $c) {
165 if ($c->hasConfig()) {
166 yield $k => $c;
167 }
168 }
169 }
170}
if(php_sapi_name() !='cli') $in
Definition: Utf8Test.php:37
An exception for terminatinating execution or to throw for unit testing.
Builds data types.
Definition: Factory.php:20
An agent that is just a collection of some other agents.
getXObjective(string $which, Config $config=null)
getInstallObjective(Config $config=null)
@inheritdocs
getConfigInput(Config $config=null)
@inheritdocs
getBuildArtifactObjective()
@inheritdocs
__construct(FieldFactory $field_factory, Refinery $refinery, array $agents)
getArrayToConfigTransformation()
@inheritdocs
getUpdateObjective(Config $config=null)
@inheritdocs
A collection of some configurations.
A objective collection is a objective that is achieved once all subobjectives are achieved.
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:14
A configuration for the setup.
Definition: Config.php:11
An objective is a desired state of the system that is supposed to be created by the setup.
Definition: Objective.php:15
This is what a factory for input fields looks like.
Definition: Factory.php:11
This describes commonalities between all inputs.
Definition: Input.php:32
if(!array_key_exists('PATH_INFO', $_SERVER)) $config
Definition: metadata.php:68