ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ImplementationOfAgentFinder.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;
24use ILIAS\Data;
25
27{
29
30 public function __construct(
31 protected Refinery $refinery,
32 protected Data\Factory $data_factory,
33 protected \ILIAS\Language\Language $lng,
34 protected ImplementationOfInterfaceFinder $interface_finder,
36 ) {
37 $this->component_agents = $component_agents;
38 }
39
45 public function getAgents(): AgentCollection
46 {
47 $agents = $this->getComponentAgents();
48
49 // Get a list of existing plugins in the system.
50 $plugins = $this->getPluginNames();
51
52 foreach ($plugins as $plugin_name) {
53 $agents = $agents->withAdditionalAgent(
54 $plugin_name,
55 $this->getPluginAgent($plugin_name)
56 );
57 }
58
59 return $agents;
60 }
61
62
67 {
68 if ($this->component_agents instanceof AgentCollection) {
70 }
71
72 $agents = new AgentCollection($this->refinery, []);
73
74 foreach ($this->component_agents as $agent) {
75 $agents = $agents->withAdditionalAgent(
76 $this->getAgentNameByClassName(get_class($agent)),
77 $agent
78 );
79 }
80
81 $this->component_agents = $agents;
83 }
84
93 public function getPluginAgent(string $name): Agent
94 {
95 // TODO: This seems to be something that rather belongs to Services/Component/
96 // but we put it here anyway for the moment. This seems to be something that
97 // could go away when we unify Services/Modules/Plugins to one common concept.
98 $path = "[/]public/Customizing/global/plugins/.*/.*/" . $name . "/.*";
99 $agent_classes = iterator_to_array($this->interface_finder->getMatchingClassNames(
100 Agent::class,
101 [],
102 $path
103 ));
104
105 if ($agent_classes === []) {
106 return new class ($name) extends \ilPluginDefaultAgent {
107 };
108 }
109
110 $agents = [];
111 foreach ($agent_classes as $class_name) {
112 $agents[] = new $class_name(
113 $this->refinery,
114 $this->data_factory,
115 $this->lng
116 );
117 }
118
119 if (count($agents) === 1) {
120 return $agents[0];
121 }
122
123 return new AgentCollection(
124 $this->refinery,
125 $agents
126 );
127 }
128
129 public function getAgentByClassName(string $class_name): Agent
130 {
131 if (!class_exists($class_name)) {
132 throw new \InvalidArgumentException("Class '" . $class_name . "' not found.");
133 }
134
135 return new $class_name(
136 $this->refinery,
137 $this->data_factory,
138 $this->lng
139 );
140 }
141
145 public function getAgentNameByClassName(string $class_name): string
146 {
147 // We assume that the name of an agent in the class ilXYZSetupAgent really
148 // is XYZ. If that does not fit we just use the class name.
149 $match = [];
150 if (preg_match("/il(\w+)SetupAgent/", $class_name, $match)) {
151 return strtolower($match[1]);
152 }
153 return $class_name;
154 }
155
159 protected function getPluginNames(): \Generator
160 {
161 $directories =
162 new \RecursiveIteratorIterator(
163 new \RecursiveDirectoryIterator(__DIR__ . "/../../../../public/Customizing/global/plugins")
164 );
165 $names = [];
166 foreach ($directories as $dir) {
167 $groups = [];
168 if (preg_match("%^" . __DIR__ . "/[.][.]/[.][.]/[.][.]/[.][.]/public/Customizing/global/plugins/(Services|Modules)/((\\w+/){2})([^/\.]+)(/|$)%", (string) $dir, $groups)) {
169 $name = $groups[4];
170 if (isset($names[$name])) {
171 continue;
172 }
173 $names[$name] = true;
174 yield $name;
175 }
176 }
177 }
178}
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Builds data types.
Definition: Factory.php:36
An agent that is just a collection of some other agents.
__construct(protected Refinery $refinery, protected Data\Factory $data_factory, protected \ILIAS\Language\Language $lng, protected ImplementationOfInterfaceFinder $interface_finder, $component_agents)
getAgents()
Collect all agents from the system, core and plugin, bundled in a collection.
getPluginAgent(string $name)
Get a agent from a specific plugin.
getAgentNameByClassName(string $class_name)
Derive a name for the agent based on a class name.
getComponentAgents()
Collect core agents from the system bundled in a collection.
getAgentByClassName(string $class_name)
Get an agent by class name.
A agent is some component that performs part of the setup process.
Definition: Agent.php:30
$path
Definition: ltiservices.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.
global $lng
Definition: privfeed.php:31