ILIAS  release_8 Revision v8.24
class.ilArtifactComponentRepository.php
Go to the documentation of this file.
1<?php
2
20declare(strict_types=1);
21
22use ILIAS\Data;
23
28{
29 public const COMPONENT_DATA_PATH = "Services/Component/artifacts/component_data.php";
30 public const PLUGIN_DATA_PATH = "Services/Component/artifacts/plugin_data.php";
31
35
36 protected array $components;
38 protected array $pluginslot_by_id;
39 protected array $plugin_by_id;
40 protected array $plugin_by_name;
41
43 {
44 $this->data_factory = $data_factory;
45 $this->plugin_state_db = $plugin_state_db;
46 $this->ilias_version = $ilias_version;
47
48 $this->buildDatabase();
49 }
50
51 protected function buildDatabase(): void
52 {
53 $component_data = $this->readComponentData();
54 $plugin_data = $this->readPluginData();
55 $this->components = [];
56 $this->component_id_by_type_and_name = [
57 "Modules" => [],
58 "Services" => []
59 ];
60 $this->pluginslot_by_id = [];
61 $plugins_per_slot = [];
62 foreach ($component_data as $comp_id => [$type, $comp_name, $slot_data]) {
63 $slots = [];
64 $component = new ilComponentInfo(
65 $comp_id,
66 $type,
67 $comp_name,
68 $slots
69 );
70 foreach ($slot_data as [$slot_id, $slot_name]) {
71 $plugins_per_slot[$slot_id] = [];
72 $slots[$slot_id] = new ilPluginSlotInfo(
73 $component,
74 $slot_id,
75 $slot_name,
76 $plugins_per_slot[$slot_id]
77 );
78 $this->pluginslot_by_id[$slot_id] = $slots[$slot_id];
79 }
80 $this->components[$comp_id] = $component;
81 $this->component_id_by_type_and_name[$type][$comp_name] = $comp_id;
82 unset($slots);
83 }
84 $this->plugin_by_id = [];
85 foreach ($plugin_data as $plugin_id => $data) {
86 [
87 $type,
88 $comp_name,
89 $slot_name,
90 $plugin_name,
91 $plugin_version,
98 $supports_cli_setup
99 ] = $data;
100 if (!$this->hasComponent($type, $comp_name)) {
101 throw new \InvalidArgumentException(
102 "Can't find component $type/$comp_name for plugin $plugin_name"
103 );
104 }
105 $component = $this->getComponentByTypeAndName($type, $comp_name);
106 if (!$component->hasPluginSlotName($slot_name)) {
107 throw new \InvalidArgumentException(
108 "Can't find slot $type/$comp_name/$slot_name for plugin $plugin_name"
109 );
110 }
111 $slot = $component->getPluginSlotByName($slot_name);
112 $this->plugin_by_id[$plugin_id] = new ilPluginInfo(
113 $this->ilias_version,
114 $slot,
115 $plugin_id,
116 $plugin_name,
117 $this->plugin_state_db->isPluginActivated($plugin_id),
118 $this->plugin_state_db->getCurrentPluginVersion($plugin_id),
119 $this->plugin_state_db->getCurrentPluginDBVersion($plugin_id),
120 $this->data_factory->version($plugin_version),
121 $this->data_factory->version($ilias_min_version),
122 $this->data_factory->version($ilias_max_version),
125 $learning_progress ?? false,
126 $supports_export ?? false,
127 $supports_cli_setup ?? true
128 );
129 $plugins_per_slot[$slot->getId()][$plugin_id] = $this->plugin_by_id[$plugin_id];
130 }
131 }
132
133 protected function readComponentData(): array
134 {
135 return require self::COMPONENT_DATA_PATH;
136 }
137
138 protected function readPluginData(): array
139 {
140 return require self::PLUGIN_DATA_PATH;
141 }
142
146 public function hasComponent(string $type, string $name): bool
147 {
148 if (!in_array($type, ilComponentInfo::TYPES)) {
149 throw new \InvalidArgumentException(
150 "Unknown component type $type."
151 );
152 }
153
154 return isset($this->component_id_by_type_and_name[$type][$name]);
155 }
156
160 public function hasComponentId(string $id): bool
161 {
162 return isset($this->components[$id]);
163 }
164
168 public function getComponents(): Iterator
169 {
170 foreach ($this->components as $id => $comp) {
171 yield $id => $comp;
172 }
173 }
174
178 public function getComponentById(string $id): ilComponentInfo
179 {
180 if (!$this->hasComponentId($id)) {
181 throw new \InvalidArgumentException(
182 "Unknown component $id"
183 );
184 }
185 return $this->components[$id];
186 }
187
191 public function getComponentByTypeAndName(string $type, string $name): ilComponentInfo
192 {
193 if (!$this->hasComponent($type, $name)) {
194 throw new \InvalidArgumentException(
195 "Unknown component $type/$name"
196 );
197 }
198 return $this->components[$this->component_id_by_type_and_name[$type][$name]];
199 }
200
201
205 public function hasPluginSlotId(string $id): bool
206 {
207 return isset($this->pluginslot_by_id[$id]);
208 }
209
213 public function getPluginSlots(): Iterator
214 {
215 foreach ($this->pluginslot_by_id as $id => $slot) {
216 yield $id => $slot;
217 }
218 }
219
223 public function getPluginSlotById(string $id): ilPluginSlotInfo
224 {
225 if (!$this->hasPluginSlotId($id)) {
226 throw new \InvalidArgumentException(
227 "Unknown pluginslot $id"
228 );
229 }
230 return $this->pluginslot_by_id[$id];
231 }
232
236 public function hasPluginId(string $id): bool
237 {
238 return isset($this->plugin_by_id[$id]);
239 }
240
248 public function getPlugins(): Iterator
249 {
250 foreach ($this->plugin_by_id as $id => $plugin) {
251 yield $id => $plugin;
252 }
253 }
254
260 public function getPluginById(string $id): ilPluginInfo
261 {
262 if (!$this->hasPluginId($id)) {
263 throw new \InvalidArgumentException(
264 "Unknown plugin $id."
265 );
266 }
267 return $this->plugin_by_id[$id];
268 }
269
275 public function getPluginByName(string $name): ilPluginInfo
276 {
277 foreach ($this->getPlugins() as $plugin) {
278 if ($plugin->getName() === $name) {
279 return $plugin;
280 }
281 }
282 throw new \InvalidArgumentException(
283 "No plugin with name $name."
284 );
285 }
286
287 public function setCurrentPluginVersion(string $plugin_id, Data\Version $version, int $db_version): void
288 {
289 $plugin = $this->getPluginById($plugin_id);
290 if ($plugin->getCurrentVersion() !== null && $plugin->getCurrentVersion()->isGreaterThan($version)) {
291 throw new \RuntimeException(
292 "Cannot downgrade plugins version from {$plugin->getCurrentVersion()} to $version"
293 );
294 }
295 if ($plugin->getCurrentDBVersion() !== null && $plugin->getCurrentDBVersion() > $db_version) {
296 throw new \RuntimeException(
297 "Cannot downgrade plugins db version from {$plugin->getCurrentDBVersion()} to $db_version"
298 );
299 }
300 $this->plugin_state_db->setCurrentPluginVersion($plugin_id, $version, $db_version);
301 $this->buildDatabase();
302 }
303
304 public function setActivation(string $plugin_id, bool $activated): void
305 {
306 if (!$this->hasPluginId($plugin_id)) {
307 throw new \InvalidArgumentException(
308 "Unknown plugin $plugin_id."
309 );
310 }
311 $this->plugin_state_db->setActivation($plugin_id, $activated);
312 $this->buildDatabase();
313 }
314
315 public function removeStateInformationOf(string $plugin_id): void
316 {
317 if (!$this->hasPluginId($plugin_id)) {
318 throw new \InvalidArgumentException(
319 "Unknown plugin $plugin_id."
320 );
321 }
322 $this->plugin_state_db->remove($plugin_id);
323 $this->buildDatabase();
324 }
325
326 public function hasActivatedPlugin(string $id): bool
327 {
328 return ($this->hasPluginId($id) && $this->getPluginById($id)->isActivated());
329 }
330}
$ilias_min_version
Definition: plugin.php:25
$version
Definition: plugin.php:24
$ilias_max_version
Definition: plugin.php:26
$learning_progress
Definition: plugin.php:29
$responsible_mail
Definition: plugin.php:28
$supports_export
Definition: plugin.php:30
$responsible
Definition: plugin.php:27
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Builds data types.
Definition: Factory.php:21
A version number that consists of three numbers (major, minor, patch).
Definition: Version.php:27
Repository for component data implemented over artifacts.
hasPluginId(string $id)
Check if a plugin exists.
hasActivatedPlugin(string $id)
Check if a plugin exists and is activated.
getComponentByTypeAndName(string $type, string $name)
@inheritdocs
setCurrentPluginVersion(string $plugin_id, Data\Version $version, int $db_version)
setActivation(string $plugin_id, bool $activated)
hasComponent(string $type, string $name)
@inheritdocs
__construct(Data\Factory $data_factory, ilPluginStateDB $plugin_state_db, Data\Version $ilias_version)
getPluginByName(string $name)
Get a plugin by name.
Simple value class for basic information about a component.
Simple value class for information about a plugin.
Simple value class for basic information about a pluginslot.
Writeable part of repository interface to ilComponentDataDB.
Repository interface for plugin state data.
if($format !==null) $name
Definition: metadata.php:247
$type