]>
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); | |
f211e417 | 78 | } catch (PluginFileNotFoundException $e) { |
6fc14d53 A |
79 | error_log($e->getMessage()); |
80 | } | |
81 | } | |
82 | } | |
83 | ||
84 | /** | |
85 | * Execute all plugins registered hook. | |
86 | * | |
51def0d8 A |
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. | |
567967fd | 90 | * |
6fc14d53 A |
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)) { | |
51def0d8 | 107 | $data = call_user_func($hookFunction, $data, $this->conf); |
6fc14d53 A |
108 | } |
109 | } | |
110 | } | |
111 | ||
112 | /** | |
113 | * Load a single plugin from its files. | |
7fde6de1 | 114 | * Call the init function if it exists, and collect errors. |
6fc14d53 A |
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 | ||
51def0d8 | 134 | $conf = $this->conf; |
6fc14d53 A |
135 | include_once $pluginFilePath; |
136 | ||
7fde6de1 A |
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 | ||
6fc14d53 A |
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 | } | |
dea0ba28 A |
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 | ||
12266213 A |
190 | if (isset($metaData[$plugin]['description'])) { |
191 | $metaData[$plugin]['description'] = t($metaData[$plugin]['description']); | |
192 | } | |
dea0ba28 A |
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 | ||
15170b51 A |
205 | $metaData[$plugin]['parameters'][$param]['value'] = ''; |
206 | // Optional parameter description in parameter.PARAM_NAME= | |
207 | if (isset($metaData[$plugin]['parameter.'. $param])) { | |
12266213 | 208 | $metaData[$plugin]['parameters'][$param]['desc'] = t($metaData[$plugin]['parameter.'. $param]); |
15170b51 | 209 | } |
dea0ba28 A |
210 | } |
211 | } | |
212 | ||
213 | return $metaData; | |
214 | } | |
7fde6de1 A |
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 | } | |
6fc14d53 A |
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 | { | |
12266213 | 242 | $this->message = sprintf(t('Plugin "%s" files not found.'), $pluginName); |
6fc14d53 | 243 | } |
db6dec0d | 244 | } |