ILIAS  release_8 Revision v8.24
ImplementationOfAgentFinder.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
21namespace ILIAS\Setup;
22
23use ILIAS\Refinery\Factory as Refinery;
24use ILIAS\Data;
25
27{
28 protected Refinery $refinery;
30 protected \ilSetupLanguage $lng;
32
33 protected array $predefined_agents;
34
38 public function __construct(
39 Refinery $refinery,
43 array $predefined_agents = []
44 ) {
45 $this->refinery = $refinery;
46 $this->data_factory = $data_factory;
47 $this->lng = $lng;
48 $this->interface_finder = $interface_finder;
49 $this->predefined_agents = $predefined_agents;
50 }
51
57 public function getAgents(): AgentCollection
58 {
59 $agents = $this->getCoreAgents();
60
61 // Get a list of existing plugins in the system.
62 $plugins = $this->getPluginNames();
63
64 foreach ($plugins as $plugin_name) {
65 $agents = $agents->withAdditionalAgent(
66 $plugin_name,
67 $this->getPluginAgent($plugin_name)
68 );
69 }
70
71 return $agents;
72 }
73
74
78 public function getCoreAgents(): AgentCollection
79 {
80 // Initialize the agents.
81 $agents = new AgentCollection(
82 $this->refinery,
83 $this->predefined_agents
84 );
85
86 // This is a list of all agent classes in the system (which we don't want to ignore).
87 $agent_classes = $this->interface_finder->getMatchingClassNames(
88 Agent::class,
89 ["[/]Customizing/.*"]
90 );
91 foreach ($agent_classes as $class_name) {
92 $agents = $agents->withAdditionalAgent(
93 $this->getAgentNameByClassName($class_name),
94 new $class_name(
95 $this->refinery,
96 $this->data_factory,
97 $this->lng
98 )
99 );
100 }
101
102 return $agents;
103 }
104
113 public function getPluginAgent(string $name): Agent
114 {
115 // TODO: This seems to be something that rather belongs to Services/Component/
116 // but we put it here anyway for the moment. This seems to be something that
117 // could go away when we unify Services/Modules/Plugins to one common concept.
118 $path = "[/]Customizing/global/plugins/.*/.*/" . $name . "/.*";
119 $agent_classes = iterator_to_array($this->interface_finder->getMatchingClassNames(
120 Agent::class,
121 [],
122 $path
123 ));
124
125 if ($agent_classes === []) {
126 return new class ($name) extends \ilPluginDefaultAgent {
127 };
128 }
129
130 $agents = [];
131 foreach ($agent_classes as $class_name) {
132 $agents[] = new $class_name(
133 $this->refinery,
134 $this->data_factory,
135 $this->lng
136 );
137 }
138
139 if (count($agents) === 1) {
140 return $agents[0];
141 }
142
143 return new AgentCollection(
144 $this->refinery,
145 $agents
146 );
147 }
148
149 public function getAgentByClassName(string $class_name): Agent
150 {
151 if (!class_exists($class_name)) {
152 throw new \InvalidArgumentException("Class '" . $class_name . "' not found.");
153 }
154
155 return new $class_name(
156 $this->refinery,
157 $this->data_factory,
158 $this->lng
159 );
160 }
161
165 public function getAgentNameByClassName(string $class_name): string
166 {
167 // We assume that the name of an agent in the class ilXYZSetupAgent really
168 // is XYZ. If that does not fit we just use the class name.
169 $match = [];
170 if (preg_match("/il(\w+)SetupAgent/", $class_name, $match)) {
171 return strtolower($match[1]);
172 }
173 return $class_name;
174 }
175
179 protected function getPluginNames(): \Generator
180 {
181 $directories =
182 new \RecursiveIteratorIterator(
183 new \RecursiveDirectoryIterator(__DIR__ . "/../../Customizing/global/plugins/")
184 );
185 $names = [];
186 foreach ($directories as $dir) {
187 $groups = [];
188 if (preg_match("%^" . __DIR__ . "/[.][.]/[.][.]/Customizing/global/plugins/((Modules)|(Services))/((\\w+/){2})([^/\.]+)(/|$)%", (string) $dir, $groups)) {
189 $name = $groups[6];
190 if (isset($names[$name])) {
191 continue;
192 }
193 $names[$name] = true;
194 yield $name;
195 }
196 }
197 }
198}
Builds data types.
Definition: Factory.php:21
An agent that is just a collection of some other agents.
ImplementationOfInterfaceFinder $interface_finder
__construct(Refinery $refinery, Data\Factory $data_factory, \ilSetupLanguage $lng, ImplementationOfInterfaceFinder $interface_finder, array $predefined_agents=[])
getAgents()
Collect all agents from the system, core and plugin, bundled in a collection.
getCoreAgents()
Collect core agents from the system 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.
getAgentByClassName(string $class_name)
Get an agent by class name.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
A agent is some component that performs part of the setup process.
Definition: Agent.php:30
$path
Definition: ltiservices.php:32
if($format !==null) $name
Definition: metadata.php:247
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...