]>
Commit | Line | Data |
---|---|---|
6fc14d53 A |
1 | <?php |
2 | ||
3 | /** | |
4 | * Class PluginManager | |
5 | * | |
6 | * Use to manage, load and execute plugins. | |
6fc14d53 A |
7 | */ |
8 | class PluginManager | |
9 | { | |
6fc14d53 A |
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 | ||
51def0d8 A |
22 | /** |
23 | * @var ConfigManager Configuration Manager instance. | |
24 | */ | |
25 | protected $conf; | |
26 | ||
7fde6de1 A |
27 | /** |
28 | * @var array List of plugin errors. | |
29 | */ | |
30 | protected $errors; | |
31 | ||
6fc14d53 A |
32 | /** |
33 | * Plugins subdirectory. | |
34 | * @var string $PLUGINS_PATH | |
35 | */ | |
36 | public static $PLUGINS_PATH = 'plugins'; | |
37 | ||
dea0ba28 A |
38 | /** |
39 | * Plugins meta files extension. | |
40 | * @var string $META_EXT | |
41 | */ | |
42 | public static $META_EXT = 'meta'; | |
43 | ||
6fc14d53 | 44 | /** |
51def0d8 | 45 | * Constructor. |
6fc14d53 | 46 | * |
51def0d8 | 47 | * @param ConfigManager $conf Configuration Manager instance. |
6fc14d53 | 48 | */ |
51def0d8 | 49 | public function __construct(&$conf) |
6fc14d53 | 50 | { |
51def0d8 | 51 | $this->conf = $conf; |
7fde6de1 | 52 | $this->errors = array(); |
6fc14d53 A |
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 | } | |
79 | catch (PluginFileNotFoundException $e) { | |
80 | error_log($e->getMessage()); | |
81 | } | |
82 | } | |
83 | } | |
84 | ||
85 | /** | |
86 | * Execute all plugins registered hook. | |
87 | * | |
51def0d8 A |
88 | * @param string $hook name of the hook to trigger. |
89 | * @param array $data list of data to manipulate passed by reference. | |
90 | * @param array $params additional parameters such as page target. | |
567967fd | 91 | * |
6fc14d53 A |
92 | * @return void |
93 | */ | |
94 | public function executeHooks($hook, &$data, $params = array()) | |
95 | { | |
96 | if (!empty($params['target'])) { | |
97 | $data['_PAGE_'] = $params['target']; | |
98 | } | |
99 | ||
100 | if (isset($params['loggedin'])) { | |
101 | $data['_LOGGEDIN_'] = $params['loggedin']; | |
102 | } | |
103 | ||
104 | foreach ($this->loadedPlugins as $plugin) { | |
105 | $hookFunction = $this->buildHookName($hook, $plugin); | |
106 | ||
107 | if (function_exists($hookFunction)) { | |
51def0d8 | 108 | $data = call_user_func($hookFunction, $data, $this->conf); |
6fc14d53 A |
109 | } |
110 | } | |
111 | } | |
112 | ||
113 | /** | |
114 | * Load a single plugin from its files. | |
7fde6de1 | 115 | * Call the init function if it exists, and collect errors. |
6fc14d53 A |
116 | * Add them in $loadedPlugins if successful. |
117 | * | |
118 | * @param string $dir plugin's directory. | |
119 | * @param string $pluginName plugin's name. | |
120 | * | |
121 | * @return void | |
122 | * @throws PluginFileNotFoundException - plugin files not found. | |
123 | */ | |
124 | private function loadPlugin($dir, $pluginName) | |
125 | { | |
126 | if (!is_dir($dir)) { | |
127 | throw new PluginFileNotFoundException($pluginName); | |
128 | } | |
129 | ||
130 | $pluginFilePath = $dir . '/' . $pluginName . '.php'; | |
131 | if (!is_file($pluginFilePath)) { | |
132 | throw new PluginFileNotFoundException($pluginName); | |
133 | } | |
134 | ||
51def0d8 | 135 | $conf = $this->conf; |
6fc14d53 A |
136 | include_once $pluginFilePath; |
137 | ||
7fde6de1 A |
138 | $initFunction = $pluginName . '_init'; |
139 | if (function_exists($initFunction)) { | |
140 | $errors = call_user_func($initFunction, $this->conf); | |
141 | if (!empty($errors)) { | |
142 | $this->errors = array_merge($this->errors, $errors); | |
143 | } | |
144 | } | |
145 | ||
6fc14d53 A |
146 | $this->loadedPlugins[] = $pluginName; |
147 | } | |
148 | ||
149 | /** | |
150 | * Construct normalize hook name for a specific plugin. | |
151 | * | |
152 | * Format: | |
153 | * hook_<plugin_name>_<hook_name> | |
154 | * | |
155 | * @param string $hook hook name. | |
156 | * @param string $pluginName plugin name. | |
157 | * | |
158 | * @return string - plugin's hook name. | |
159 | */ | |
160 | public function buildHookName($hook, $pluginName) | |
161 | { | |
162 | return 'hook_' . $pluginName . '_' . $hook; | |
163 | } | |
dea0ba28 A |
164 | |
165 | /** | |
166 | * Retrieve plugins metadata from *.meta (INI) files into an array. | |
167 | * Metadata contains: | |
168 | * - plugin description [description] | |
169 | * - parameters split with ';' [parameters] | |
170 | * | |
171 | * Respects plugins order from settings. | |
172 | * | |
173 | * @return array plugins metadata. | |
174 | */ | |
175 | public function getPluginsMeta() | |
176 | { | |
177 | $metaData = array(); | |
178 | $dirs = glob(self::$PLUGINS_PATH . '/*', GLOB_ONLYDIR | GLOB_MARK); | |
179 | ||
180 | // Browse all plugin directories. | |
181 | foreach ($dirs as $pluginDir) { | |
182 | $plugin = basename($pluginDir); | |
183 | $metaFile = $pluginDir . $plugin . '.' . self::$META_EXT; | |
184 | if (!is_file($metaFile) || !is_readable($metaFile)) { | |
185 | continue; | |
186 | } | |
187 | ||
188 | $metaData[$plugin] = parse_ini_file($metaFile); | |
189 | $metaData[$plugin]['order'] = array_search($plugin, $this->authorizedPlugins); | |
190 | ||
191 | // Read parameters and format them into an array. | |
192 | if (isset($metaData[$plugin]['parameters'])) { | |
193 | $params = explode(';', $metaData[$plugin]['parameters']); | |
194 | } else { | |
195 | $params = array(); | |
196 | } | |
197 | $metaData[$plugin]['parameters'] = array(); | |
198 | foreach ($params as $param) { | |
199 | if (empty($param)) { | |
200 | continue; | |
201 | } | |
202 | ||
15170b51 A |
203 | $metaData[$plugin]['parameters'][$param]['value'] = ''; |
204 | // Optional parameter description in parameter.PARAM_NAME= | |
205 | if (isset($metaData[$plugin]['parameter.'. $param])) { | |
206 | $metaData[$plugin]['parameters'][$param]['desc'] = $metaData[$plugin]['parameter.'. $param]; | |
207 | } | |
dea0ba28 A |
208 | } |
209 | } | |
210 | ||
211 | return $metaData; | |
212 | } | |
7fde6de1 A |
213 | |
214 | /** | |
215 | * Return the list of encountered errors. | |
216 | * | |
217 | * @return array List of errors (empty array if none exists). | |
218 | */ | |
219 | public function getErrors() | |
220 | { | |
221 | return $this->errors; | |
222 | } | |
6fc14d53 A |
223 | } |
224 | ||
225 | /** | |
226 | * Class PluginFileNotFoundException | |
227 | * | |
228 | * Raise when plugin files can't be found. | |
229 | */ | |
230 | class PluginFileNotFoundException extends Exception | |
231 | { | |
232 | /** | |
233 | * Construct exception with plugin name. | |
234 | * Generate message. | |
235 | * | |
236 | * @param string $pluginName name of the plugin not found | |
237 | */ | |
238 | public function __construct($pluginName) | |
239 | { | |
240 | $this->message = 'Plugin "'. $pluginName .'" files not found.'; | |
241 | } | |
db6dec0d | 242 | } |