diff options
author | VirtualTam <virtualtam@flibidi.net> | 2018-12-04 00:26:50 +0100 |
---|---|---|
committer | VirtualTam <virtualtam@flibidi.net> | 2019-01-12 23:11:19 +0100 |
commit | e1850388348d4bfdf463a5aa341bc470da79cf32 (patch) | |
tree | a97cfd137b545e7b3ada43c4b5bfe143bd494948 /application/PluginManager.php | |
parent | 349b0144011e25f2b1a727b1d28d49d55b3b2ebb (diff) | |
download | Shaarli-e1850388348d4bfdf463a5aa341bc470da79cf32.tar.gz Shaarli-e1850388348d4bfdf463a5aa341bc470da79cf32.tar.zst Shaarli-e1850388348d4bfdf463a5aa341bc470da79cf32.zip |
namespacing: \Shaarli\Plugin\PluginManager
Signed-off-by: VirtualTam <virtualtam@flibidi.net>
Diffstat (limited to 'application/PluginManager.php')
-rw-r--r-- | application/PluginManager.php | 244 |
1 files changed, 0 insertions, 244 deletions
diff --git a/application/PluginManager.php b/application/PluginManager.php deleted file mode 100644 index 1ed4db4b..00000000 --- a/application/PluginManager.php +++ /dev/null | |||
@@ -1,244 +0,0 @@ | |||
1 | <?php | ||
2 | |||
3 | /** | ||
4 | * Class PluginManager | ||
5 | * | ||
6 | * Use to manage, load and execute plugins. | ||
7 | */ | ||
8 | class PluginManager | ||
9 | { | ||
10 | /** | ||
11 | * List of authorized plugins from configuration file. | ||
12 | * @var array $authorizedPlugins | ||
13 | */ | ||
14 | private $authorizedPlugins; | ||
15 | |||
16 | /** | ||
17 | * List of loaded plugins. | ||
18 | * @var array $loadedPlugins | ||
19 | */ | ||
20 | private $loadedPlugins = array(); | ||
21 | |||
22 | /** | ||
23 | * @var ConfigManager Configuration Manager instance. | ||
24 | */ | ||
25 | protected $conf; | ||
26 | |||
27 | /** | ||
28 | * @var array List of plugin errors. | ||
29 | */ | ||
30 | protected $errors; | ||
31 | |||
32 | /** | ||
33 | * Plugins subdirectory. | ||
34 | * @var string $PLUGINS_PATH | ||
35 | */ | ||
36 | public static $PLUGINS_PATH = 'plugins'; | ||
37 | |||
38 | /** | ||
39 | * Plugins meta files extension. | ||
40 | * @var string $META_EXT | ||
41 | */ | ||
42 | public static $META_EXT = 'meta'; | ||
43 | |||
44 | /** | ||
45 | * Constructor. | ||
46 | * | ||
47 | * @param ConfigManager $conf Configuration Manager instance. | ||
48 | */ | ||
49 | public function __construct(&$conf) | ||
50 | { | ||
51 | $this->conf = $conf; | ||
52 | $this->errors = array(); | ||
53 | } | ||
54 | |||
55 | /** | ||
56 | * Load plugins listed in $authorizedPlugins. | ||
57 | * | ||
58 | * @param array $authorizedPlugins Names of plugin authorized to be loaded. | ||
59 | * | ||
60 | * @return void | ||
61 | */ | ||
62 | public function load($authorizedPlugins) | ||
63 | { | ||
64 | $this->authorizedPlugins = $authorizedPlugins; | ||
65 | |||
66 | $dirs = glob(self::$PLUGINS_PATH . '/*', GLOB_ONLYDIR); | ||
67 | $dirnames = array_map('basename', $dirs); | ||
68 | foreach ($this->authorizedPlugins as $plugin) { | ||
69 | $index = array_search($plugin, $dirnames); | ||
70 | |||
71 | // plugin authorized, but its folder isn't listed | ||
72 | if ($index === false) { | ||
73 | continue; | ||
74 | } | ||
75 | |||
76 | try { | ||
77 | $this->loadPlugin($dirs[$index], $plugin); | ||
78 | } catch (PluginFileNotFoundException $e) { | ||
79 | error_log($e->getMessage()); | ||
80 | } | ||
81 | } | ||
82 | } | ||
83 | |||
84 | /** | ||
85 | * Execute all plugins registered hook. | ||
86 | * | ||
87 | * @param string $hook name of the hook to trigger. | ||
88 | * @param array $data list of data to manipulate passed by reference. | ||
89 | * @param array $params additional parameters such as page target. | ||
90 | * | ||
91 | * @return void | ||
92 | */ | ||
93 | public function executeHooks($hook, &$data, $params = array()) | ||
94 | { | ||
95 | if (!empty($params['target'])) { | ||
96 | $data['_PAGE_'] = $params['target']; | ||
97 | } | ||
98 | |||
99 | if (isset($params['loggedin'])) { | ||
100 | $data['_LOGGEDIN_'] = $params['loggedin']; | ||
101 | } | ||
102 | |||
103 | foreach ($this->loadedPlugins as $plugin) { | ||
104 | $hookFunction = $this->buildHookName($hook, $plugin); | ||
105 | |||
106 | if (function_exists($hookFunction)) { | ||
107 | $data = call_user_func($hookFunction, $data, $this->conf); | ||
108 | } | ||
109 | } | ||
110 | } | ||
111 | |||
112 | /** | ||
113 | * Load a single plugin from its files. | ||
114 | * Call the init function if it exists, and collect errors. | ||
115 | * Add them in $loadedPlugins if successful. | ||
116 | * | ||
117 | * @param string $dir plugin's directory. | ||
118 | * @param string $pluginName plugin's name. | ||
119 | * | ||
120 | * @return void | ||
121 | * @throws PluginFileNotFoundException - plugin files not found. | ||
122 | */ | ||
123 | private function loadPlugin($dir, $pluginName) | ||
124 | { | ||
125 | if (!is_dir($dir)) { | ||
126 | throw new PluginFileNotFoundException($pluginName); | ||
127 | } | ||
128 | |||
129 | $pluginFilePath = $dir . '/' . $pluginName . '.php'; | ||
130 | if (!is_file($pluginFilePath)) { | ||
131 | throw new PluginFileNotFoundException($pluginName); | ||
132 | } | ||
133 | |||
134 | $conf = $this->conf; | ||
135 | include_once $pluginFilePath; | ||
136 | |||
137 | $initFunction = $pluginName . '_init'; | ||
138 | if (function_exists($initFunction)) { | ||
139 | $errors = call_user_func($initFunction, $this->conf); | ||
140 | if (!empty($errors)) { | ||
141 | $this->errors = array_merge($this->errors, $errors); | ||
142 | } | ||
143 | } | ||
144 | |||
145 | $this->loadedPlugins[] = $pluginName; | ||
146 | } | ||
147 | |||
148 | /** | ||
149 | * Construct normalize hook name for a specific plugin. | ||
150 | * | ||
151 | * Format: | ||
152 | * hook_<plugin_name>_<hook_name> | ||
153 | * | ||
154 | * @param string $hook hook name. | ||
155 | * @param string $pluginName plugin name. | ||
156 | * | ||
157 | * @return string - plugin's hook name. | ||
158 | */ | ||
159 | public function buildHookName($hook, $pluginName) | ||
160 | { | ||
161 | return 'hook_' . $pluginName . '_' . $hook; | ||
162 | } | ||
163 | |||
164 | /** | ||
165 | * Retrieve plugins metadata from *.meta (INI) files into an array. | ||
166 | * Metadata contains: | ||
167 | * - plugin description [description] | ||
168 | * - parameters split with ';' [parameters] | ||
169 | * | ||
170 | * Respects plugins order from settings. | ||
171 | * | ||
172 | * @return array plugins metadata. | ||
173 | */ | ||
174 | public function getPluginsMeta() | ||
175 | { | ||
176 | $metaData = array(); | ||
177 | $dirs = glob(self::$PLUGINS_PATH . '/*', GLOB_ONLYDIR | GLOB_MARK); | ||
178 | |||
179 | // Browse all plugin directories. | ||
180 | foreach ($dirs as $pluginDir) { | ||
181 | $plugin = basename($pluginDir); | ||
182 | $metaFile = $pluginDir . $plugin . '.' . self::$META_EXT; | ||
183 | if (!is_file($metaFile) || !is_readable($metaFile)) { | ||
184 | continue; | ||
185 | } | ||
186 | |||
187 | $metaData[$plugin] = parse_ini_file($metaFile); | ||
188 | $metaData[$plugin]['order'] = array_search($plugin, $this->authorizedPlugins); | ||
189 | |||
190 | if (isset($metaData[$plugin]['description'])) { | ||
191 | $metaData[$plugin]['description'] = t($metaData[$plugin]['description']); | ||
192 | } | ||
193 | // Read parameters and format them into an array. | ||
194 | if (isset($metaData[$plugin]['parameters'])) { | ||
195 | $params = explode(';', $metaData[$plugin]['parameters']); | ||
196 | } else { | ||
197 | $params = array(); | ||
198 | } | ||
199 | $metaData[$plugin]['parameters'] = array(); | ||
200 | foreach ($params as $param) { | ||
201 | if (empty($param)) { | ||
202 | continue; | ||
203 | } | ||
204 | |||
205 | $metaData[$plugin]['parameters'][$param]['value'] = ''; | ||
206 | // Optional parameter description in parameter.PARAM_NAME= | ||
207 | if (isset($metaData[$plugin]['parameter.'. $param])) { | ||
208 | $metaData[$plugin]['parameters'][$param]['desc'] = t($metaData[$plugin]['parameter.'. $param]); | ||
209 | } | ||
210 | } | ||
211 | } | ||
212 | |||
213 | return $metaData; | ||
214 | } | ||
215 | |||
216 | /** | ||
217 | * Return the list of encountered errors. | ||
218 | * | ||
219 | * @return array List of errors (empty array if none exists). | ||
220 | */ | ||
221 | public function getErrors() | ||
222 | { | ||
223 | return $this->errors; | ||
224 | } | ||
225 | } | ||
226 | |||
227 | /** | ||
228 | * Class PluginFileNotFoundException | ||
229 | * | ||
230 | * Raise when plugin files can't be found. | ||
231 | */ | ||
232 | class PluginFileNotFoundException extends Exception | ||
233 | { | ||
234 | /** | ||
235 | * Construct exception with plugin name. | ||
236 | * Generate message. | ||
237 | * | ||
238 | * @param string $pluginName name of the plugin not found | ||
239 | */ | ||
240 | public function __construct($pluginName) | ||
241 | { | ||
242 | $this->message = sprintf(t('Plugin "%s" files not found.'), $pluginName); | ||
243 | } | ||
244 | } | ||