ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
ImplementationOfAgentFinder.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 2020 Daniel Weise <daniel.weise@concepts-and-training.de> Extended GPL, see docs/LICENSE */
4 
5 declare(strict_types=1);
6 
7 namespace ILIAS\Setup;
8 
10 use ILIAS\Data;
11 
13 {
17  protected $refinery;
18 
22  protected $data_factory;
23 
27  protected $lng;
28 
32  protected $plugin_raw_reader;
33 
37  protected $interface_finder;
38 
42  protected $predefined_agents;
43 
47  public function __construct(
53  array $predefined_agents = []
54  ) {
55  $this->refinery = $refinery;
56  $this->data_factory = $data_factory;
57  $this->lng = $lng;
58  $this->interface_finder = $interface_finder;
59  $this->plugin_raw_reader = $plugin_raw_reader;
60  $this->predefined_agents = $predefined_agents;
61  }
62 
68  public function getAgents() : AgentCollection
69  {
70  $agents = $this->getCoreAgents();
71 
72  // Get a list of existing plugins in the system.
73  $plugins = $this->plugin_raw_reader->getPluginNames();
74 
75  foreach ($plugins as $plugin_name) {
76  $agents = $agents->withAdditionalAgent(
77  strtolower($plugin_name),
78  $this->getPluginAgent($plugin_name)
79  );
80  }
81 
82  return $agents;
83  }
84 
85 
89  public function getCoreAgents() : AgentCollection
90  {
91  // Initialize the agents.
92  $agents = new AgentCollection(
93  $this->refinery,
94  $this->predefined_agents
95  );
96 
97  // This is a list of all agent classes in the system (which we don't want to ignore).
98  $agent_classes = $this->interface_finder->getMatchingClassNames(
99  Agent::class,
100  ["[/]Customizing/.*"]
101  );
102  foreach ($agent_classes as $class_name) {
103  $agents = $agents->withAdditionalAgent(
104  $this->getAgentNameByClassName($class_name),
105  new $class_name(
106  $this->refinery,
107  $this->data_factory,
108  $this->lng
109  )
110  );
111  }
112 
113  return $agents;
114  }
115 
124  public function getPluginAgent(string $name) : Agent
125  {
126  if (!$this->plugin_raw_reader->hasPlugin($name)) {
127  throw new \InvalidArgumentException(
128  "Cannot find plugin with name '$name'"
129  );
130  }
131 
132  // TODO: This seems to be something that rather belongs to Services/Component/
133  // but we put it here anyway for the moment. This seems to be something that
134  // could go away when we unify Services/Modules/Plugins to one common concept.
135  $path = "[/]Customizing/global/plugins/.*/.*/" . $name . "/.*";
136  $agent_classes = iterator_to_array($this->interface_finder->getMatchingClassNames(
137  Agent::class,
138  [],
139  $path
140  ));
141 
142  if (count($agent_classes) === 0) {
143  return new class($name) extends \ilPluginDefaultAgent {
144  };
145  }
146 
147  $agents = [];
148  foreach ($agent_classes as $class_name) {
149  $agents[] = new $class_name(
150  $this->refinery,
151  $this->data_factory,
152  $this->lng
153  );
154  }
155 
156  if (count($agents) === 1) {
157  return $agents[0];
158  }
159 
160  return new AgentCollection(
161  $this->refinery,
162  $agents
163  );
164  }
165 
166  public function getAgentByClassName(string $class_name) : Agent
167  {
168  if (!class_exists($class_name)) {
169  throw new \InvalidArgumentException("Class '" . $class_name . "' not found.");
170  }
171 
172  return new $class_name(
173  $this->refinery,
174  $this->data_factory,
175  $this->lng
176  );
177  }
178 
182  public function getAgentNameByClassName(string $class_name) : string
183  {
184  // We assume that the name of an agent in the class ilXYZSetupAgent really
185  // is XYZ. If that does not fit we just use the class name.
186  $match = [];
187  if (preg_match("/il(\w+)SetupAgent/", $class_name, $match)) {
188  return strtolower($match[1]);
189  }
190  return $class_name;
191  }
192 }
An agent that is just a collection of some other agents.
getCoreAgents()
Collect core agents from the system bundled in a collection.
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:13
if($format !==null) $name
Definition: metadata.php:230
getPluginAgent(string $name)
Get a agent from a specific plugin.
__construct(Refinery $refinery, Data\Factory $data_factory, \ilSetupLanguage $lng, ImplementationOfInterfaceFinder $interface_finder, \ilPluginRawReader $plugin_raw_reader, array $predefined_agents=[])
Builds data types.
Definition: Factory.php:19
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...
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.