6 * Use to manage, load and execute plugins.
11 * List of authorized plugins from configuration file.
12 * @var array $authorizedPlugins
14 private $authorizedPlugins;
17 * List of loaded plugins.
18 * @var array $loadedPlugins
20 private $loadedPlugins = array();
23 * @var ConfigManager Configuration Manager instance.
28 * @var array List of plugin errors.
33 * Plugins subdirectory.
34 * @var string $PLUGINS_PATH
36 public static $PLUGINS_PATH = 'plugins';
39 * Plugins meta files extension.
40 * @var string $META_EXT
42 public static $META_EXT = 'meta';
47 * @param ConfigManager $conf Configuration Manager instance.
49 public function __construct(&$conf)
52 $this->errors
= array();
56 * Load plugins listed in $authorizedPlugins.
58 * @param array $authorizedPlugins Names of plugin authorized to be loaded.
62 public function load($authorizedPlugins)
64 $this->authorizedPlugins
= $authorizedPlugins;
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);
71 // plugin authorized, but its folder isn't listed
72 if ($index === false) {
77 $this->loadPlugin($dirs[$index], $plugin);
78 } catch (PluginFileNotFoundException
$e) {
79 error_log($e->getMessage());
85 * Execute all plugins registered hook.
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.
93 public function executeHooks($hook, &$data, $params = array())
95 if (!empty($params['target'])) {
96 $data['_PAGE_'] = $params['target'];
99 if (isset($params['loggedin'])) {
100 $data['_LOGGEDIN_'] = $params['loggedin'];
103 foreach ($this->loadedPlugins
as $plugin) {
104 $hookFunction = $this->buildHookName($hook, $plugin);
106 if (function_exists($hookFunction)) {
107 $data = call_user_func($hookFunction, $data, $this->conf
);
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.
117 * @param string $dir plugin's directory.
118 * @param string $pluginName plugin's name.
121 * @throws PluginFileNotFoundException - plugin files not found.
123 private function loadPlugin($dir, $pluginName)
126 throw new PluginFileNotFoundException($pluginName);
129 $pluginFilePath = $dir . '/' . $pluginName . '.php';
130 if (!is_file($pluginFilePath)) {
131 throw new PluginFileNotFoundException($pluginName);
135 include_once $pluginFilePath;
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);
145 $this->loadedPlugins
[] = $pluginName;
149 * Construct normalize hook name for a specific plugin.
152 * hook_<plugin_name>_<hook_name>
154 * @param string $hook hook name.
155 * @param string $pluginName plugin name.
157 * @return string - plugin's hook name.
159 public function buildHookName($hook, $pluginName)
161 return 'hook_' . $pluginName . '_' . $hook;
165 * Retrieve plugins metadata from *.meta (INI) files into an array.
167 * - plugin description [description]
168 * - parameters split with ';' [parameters]
170 * Respects plugins order from settings.
172 * @return array plugins metadata.
174 public function getPluginsMeta()
177 $dirs = glob(self
::$PLUGINS_PATH . '/*', GLOB_ONLYDIR
| GLOB_MARK
);
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)) {
187 $metaData[$plugin] = parse_ini_file($metaFile);
188 $metaData[$plugin]['order'] = array_search($plugin, $this->authorizedPlugins
);
190 if (isset($metaData[$plugin]['description'])) {
191 $metaData[$plugin]['description'] = t($metaData[$plugin]['description']);
193 // Read parameters and format them into an array.
194 if (isset($metaData[$plugin]['parameters'])) {
195 $params = explode(';', $metaData[$plugin]['parameters']);
199 $metaData[$plugin]['parameters'] = array();
200 foreach ($params as $param) {
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]);
217 * Return the list of encountered errors.
219 * @return array List of errors (empty array if none exists).
221 public function getErrors()
223 return $this->errors
;
228 * Class PluginFileNotFoundException
230 * Raise when plugin files can't be found.
232 class PluginFileNotFoundException
extends Exception
235 * Construct exception with plugin name.
238 * @param string $pluginName name of the plugin not found
240 public function __construct($pluginName)
242 $this->message
= sprintf(t('Plugin "%s" files not found.'), $pluginName);