ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
PluggableTrait.php
Go to the documentation of this file.
1<?php
2
4
5use BadMethodCallException;
8use LogicException;
9
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}
An exception for terminatinating execution or to throw for unit testing.
getMethod()
Get the method name.
addPlugin(PluginInterface $plugin)
Register a plugin.
invokePlugin($method, array $arguments, FilesystemInterface $filesystem)
Invoke a plugin by method name.
findPlugin($method)
Find a specific plugin.
__call($method, array $arguments)
Plugins pass-through.