ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
ImplementationOfAgentFinder.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\Setup;
22 
24 use ILIAS\Data;
25 
33 {
34  protected array|AgentCollection $component_agents;
35 
36  public function __construct(
37  protected Refinery $refinery,
38  protected Data\Factory $data_factory,
39  protected \ILIAS\Language\Language $lng,
40  protected ImplementationOfInterfaceFinder $interface_finder,
41  $component_agents
42  ) {
43  $this->component_agents = $component_agents;
44  }
45 
51  public function getAgents(): AgentCollection
52  {
53  $agents = $this->getComponentAgents();
54 
55  // Get a list of existing plugins in the system.
56  $plugins = $this->getPluginNames();
57 
58  foreach ($plugins as $plugin_name) {
59  $agents = $agents->withAdditionalAgent(
60  $plugin_name,
61  $this->getPluginAgent($plugin_name)
62  );
63  }
64 
65  return $agents;
66  }
67 
68 
72  public function getComponentAgents(): AgentCollection
73  {
74  if ($this->component_agents instanceof AgentCollection) {
76  }
77 
78  $agents = new AgentCollection($this->refinery, []);
79 
80  foreach ($this->component_agents as $agent) {
81  $agents = $agents->withAdditionalAgent(
82  $this->getAgentNameByClassName(get_class($agent)),
83  $agent
84  );
85  }
86 
87  $this->component_agents = $agents;
89  }
90 
99  public function getPluginAgent(string $name): Agent
100  {
101  // TODO: This seems to be something that rather belongs to Services/Component/
102  // but we put it here anyway for the moment. This seems to be something that
103  // could go away when we unify Services/Modules/Plugins to one common concept.
104  $path = "[/]public/Customizing/global/plugins/.*/.*/" . $name . "/.*";
105  $agent_classes = iterator_to_array($this->interface_finder->getMatchingClassNames(
106  Agent::class,
107  [],
108  $path
109  ));
110 
111  if ($agent_classes === []) {
112  return new class ($name) extends \ilPluginDefaultAgent {
113  };
114  }
115 
116  $agents = [];
117  foreach ($agent_classes as $class_name) {
118  $agents[] = new $class_name(
119  $this->refinery,
120  $this->data_factory,
121  $this->lng
122  );
123  }
124 
125  if (count($agents) === 1) {
126  return $agents[0];
127  }
128 
129  return new AgentCollection(
130  $this->refinery,
131  $agents
132  );
133  }
134 
135  public function getAgentByClassName(string $class_name): Agent
136  {
137  if (!class_exists($class_name)) {
138  throw new \InvalidArgumentException("Class '" . $class_name . "' not found.");
139  }
140 
141  return new $class_name(
142  $this->refinery,
143  $this->data_factory,
144  $this->lng
145  );
146  }
147 
151  public function getAgentNameByClassName(string $class_name): string
152  {
153  // We assume that the name of an agent in the class ilXYZSetupAgent really
154  // is XYZ. If that does not fit we just use the class name.
155  $match = [];
156  if (preg_match("/il(\w+)SetupAgent/", $class_name, $match)) {
157  return strtolower($match[1]);
158  }
159  return $class_name;
160  }
161 
165  protected function getPluginNames(): \Generator
166  {
167  $directories =
168  new \RecursiveIteratorIterator(
169  new \RecursiveDirectoryIterator(__DIR__ . "/../../../../public/Customizing/global/plugins")
170  );
171  $names = [];
172  foreach ($directories as $dir) {
173  $groups = [];
174  if (preg_match("%^" . __DIR__ . "/[.][.]/[.][.]/[.][.]/[.][.]/public/Customizing/global/plugins/(Services|Modules)/((\\w+/){2})([^/\.]+)(/|$)%", (string) $dir, $groups)) {
175  $name = $groups[4];
176  if (isset($names[$name])) {
177  continue;
178  }
179  $names[$name] = true;
180  yield $name;
181  }
182  }
183  }
184 
190  public function getName(): string
191  {
192  return 'Agent Finder Adapter';
193  }
194 
200  public function enter(): int
201  {
202  global $DIC;
203  $DIC['setup.agentfinder'] = fn() => $this;
204  return 0;
205  }
206 
207 }
enter()
Using the AgentFinder as EntryPoint is an exception/abomination for 10 only.
Interface Observer Contains several chained tasks and infos about them.
$path
Definition: ltiservices.php:30
__construct(protected Refinery $refinery, protected Data\Factory $data_factory, protected \ILIAS\Language\Language $lng, protected ImplementationOfInterfaceFinder $interface_finder, $component_agents)
getPluginAgent(string $name)
Get a agent from a specific plugin.
global $DIC
Definition: shib_login.php:25
Builds data types.
Definition: Factory.php:35
getName()
Using the AgentFinder as EntryPoint is an exception/abomination for 10 only.
Using the AgentFinder as EntryPoint is an exception/abomination for 10 only.
An entrypoint is where the programm execution starts.
Definition: EntryPoint.php:27
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...
global $lng
Definition: privfeed.php:32
getComponentAgents()
Collect core agents from the system bundled in a collection.
getAgents()
Collect all agents from the system, core and plugin, bundled in a collection.
getAgentNameByClassName(string $class_name)
Derive a name for the agent based on a class name.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...