ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
PluggableTrait.php
Go to the documentation of this file.
1 <?php
2 
4 
9 
10 trait PluggableTrait
11 {
15  protected $plugins = [];
16 
26  public function addPlugin(PluginInterface $plugin)
27  {
28  if ( ! method_exists($plugin, 'handle')) {
29  throw new LogicException(get_class($plugin) . ' does not have a handle method.');
30  }
31 
32  $this->plugins[$plugin->getMethod()] = $plugin;
33 
34  return $this;
35  }
36 
46  protected function findPlugin($method)
47  {
48  if ( ! isset($this->plugins[$method])) {
49  throw new PluginNotFoundException('Plugin not found for method: ' . $method);
50  }
51 
52  return $this->plugins[$method];
53  }
54 
66  protected function invokePlugin($method, array $arguments, FilesystemInterface $filesystem)
67  {
68  $plugin = $this->findPlugin($method);
69  $plugin->setFilesystem($filesystem);
70  $callback = [$plugin, 'handle'];
71 
72  return call_user_func_array($callback, $arguments);
73  }
74 
85  public function __call($method, array $arguments)
86  {
87  try {
88  return $this->invokePlugin($method, $arguments, $this);
89  } catch (PluginNotFoundException $e) {
90  throw new BadMethodCallException(
91  'Call to undefined method '
92  . get_class($this)
93  . '::' . $method
94  );
95  }
96  }
97 }
findPlugin($method)
Find a specific plugin.
addPlugin(PluginInterface $plugin)
Register a plugin.
__call($method, array $arguments)
Plugins pass-through.
invokePlugin($method, array $arguments, FilesystemInterface $filesystem)
Invoke a plugin by method name.
getMethod()
Get the method name.
Create styles array
The data for the language used.