ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilArtifactComponentRepository.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 use ILIAS\Data;
22 
27 {
28  protected Data\Factory $data_factory;
31 
32  protected array $components;
34  protected array $pluginslot_by_id;
35  protected array $plugin_by_id;
36  protected array $plugin_by_name;
37 
38  public function __construct(Data\Factory $data_factory, ilPluginStateDB $plugin_state_db, Data\Version $ilias_version)
39  {
40  $this->data_factory = $data_factory;
41  $this->plugin_state_db = $plugin_state_db;
42  $this->ilias_version = $ilias_version;
43 
44  $this->buildDatabase();
45  }
46 
47  protected function buildDatabase(): void
48  {
49  $component_data = $this->readComponentData();
50  $plugin_data = $this->readPluginData();
51  $this->components = [];
52  $this->component_id_by_type_and_name = [
53  "components/ILIAS" => []
54  ];
55  $this->pluginslot_by_id = [];
56  $plugins_per_slot = [];
57  foreach ($component_data as $comp_id => [$type, $comp_name, $slot_data]) {
58  $slots = [];
59  $component = new ilComponentInfo(
60  $comp_id,
61  $type,
62  $comp_name,
63  $slots
64  );
65  foreach ($slot_data as [$slot_id, $slot_name]) {
66  $plugins_per_slot[$slot_id] = [];
67  $slots[$slot_id] = new ilPluginSlotInfo(
68  $component,
69  $slot_id,
70  $slot_name,
71  $plugins_per_slot[$slot_id]
72  );
73  $this->pluginslot_by_id[$slot_id] = $slots[$slot_id];
74  }
75  $this->components[$comp_id] = $component;
76  $this->component_id_by_type_and_name[$type][$comp_name] = $comp_id;
77  unset($slots);
78  }
79  $this->plugin_by_id = [];
80  foreach ($plugin_data as $plugin_id => $data) {
81  [
82  $type,
83  $comp_name,
84  $slot_name,
85  $plugin_name,
86  $plugin_version,
93  $supports_cli_setup
94  ] = $data;
95 
96  if (!$this->hasComponent('components/ILIAS', $comp_name)) {
97  throw new \InvalidArgumentException(
98  "Can't find component $type/$comp_name for plugin $plugin_name"
99  );
100  }
101 
102  $component = $this->getComponentByTypeAndName('components/ILIAS', $comp_name);
103  if (!$component->hasPluginSlotName($slot_name)) {
104  throw new \InvalidArgumentException(
105  "Can't find slot $type/$comp_name/$slot_name for plugin $plugin_name"
106  );
107  }
108  $slot = $component->getPluginSlotByName($slot_name);
109  $this->plugin_by_id[$plugin_id] = new ilPluginInfo(
110  $this->ilias_version,
111  $slot,
112  $plugin_id,
113  $plugin_name,
114  $type,
115  $this->plugin_state_db->isPluginActivated($plugin_id),
116  $this->plugin_state_db->getCurrentPluginVersion($plugin_id),
117  $this->plugin_state_db->getCurrentPluginDBVersion($plugin_id),
118  $this->data_factory->version($plugin_version),
119  $this->data_factory->version($ilias_min_version),
120  $this->data_factory->version($ilias_max_version),
121  $responsible,
123  $learning_progress ?? false,
124  $supports_export ?? false,
125  $supports_cli_setup ?? true
126  );
127  $plugins_per_slot[$slot->getId()][$plugin_id] = $this->plugin_by_id[$plugin_id];
128  }
129  }
130 
131  protected function readComponentData(): array
132  {
133  return require ilComponentBuildComponentInfoObjective::PATH();
134  }
135 
136  protected function readPluginData(): array
137  {
138  return require ilComponentBuildPluginInfoObjective::PATH();
139  }
140 
144  public function hasComponent(string $type, string $name): bool
145  {
146  $types = ilComponentInfo::TYPES;
149  if (!in_array($type, $types)) {
150  throw new \InvalidArgumentException(
151  "Unknown component type $type."
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 }
hasActivatedPlugin(string $id)
Check if a plugin exists and is activated.
$supports_export
Definition: plugin.php:30
setCurrentPluginVersion(string $plugin_id, Data\Version $version, int $db_version)
$responsible_mail
Definition: plugin.php:28
$version
Definition: plugin.php:24
Writeable part of repository interface to ilComponentDataDB.
$ilias_max_version
Definition: plugin.php:26
Simple value class for basic information about a pluginslot.
hasPluginId(string $id)
Check if a plugin exists.
getPluginByName(string $name)
Get a plugin by name.
__construct(Data\Factory $data_factory, ilPluginStateDB $plugin_state_db, Data\Version $ilias_version)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
$ilias_min_version
Definition: plugin.php:25
setActivation(string $plugin_id, bool $activated)
Repository interface for plugin state data.
Builds data types.
Definition: Factory.php:35
Simple value class for information about a plugin.
$learning_progress
Definition: plugin.php:29
Repository for component data implemented over artifacts.
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
A version number that consists of three numbers (major, minor, patch).
Definition: Version.php:26
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$responsible
Definition: plugin.php:27
Simple value class for basic information about a component.