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);
79 catch (PluginFileNotFoundException
$e) {
80 error_log($e->getMessage());
86 * Execute all plugins registered hook.
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.
94 public function executeHooks($hook, &$data, $params = array())
96 if (!empty($params['target'])) {
97 $data['_PAGE_'] = $params['target'];
100 if (isset($params['loggedin'])) {
101 $data['_LOGGEDIN_'] = $params['loggedin'];
104 foreach ($this->loadedPlugins
as $plugin) {
105 $hookFunction = $this->buildHookName($hook, $plugin);
107 if (function_exists($hookFunction)) {
108 $data = call_user_func($hookFunction, $data, $this->conf
);
114 * Load a single plugin from its files.
115 * Call the init function if it exists, and collect errors.
116 * Add them in $loadedPlugins if successful.
118 * @param string $dir plugin's directory.
119 * @param string $pluginName plugin's name.
122 * @throws PluginFileNotFoundException - plugin files not found.
124 private function loadPlugin($dir, $pluginName)
127 throw new PluginFileNotFoundException($pluginName);
130 $pluginFilePath = $dir . '/' . $pluginName . '.php';
131 if (!is_file($pluginFilePath)) {
132 throw new PluginFileNotFoundException($pluginName);
136 include_once $pluginFilePath;
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);
146 $this->loadedPlugins
[] = $pluginName;
150 * Construct normalize hook name for a specific plugin.
153 * hook_<plugin_name>_<hook_name>
155 * @param string $hook hook name.
156 * @param string $pluginName plugin name.
158 * @return string - plugin's hook name.
160 public function buildHookName($hook, $pluginName)
162 return 'hook_' . $pluginName . '_' . $hook;
166 * Retrieve plugins metadata from *.meta (INI) files into an array.
168 * - plugin description [description]
169 * - parameters split with ';' [parameters]
171 * Respects plugins order from settings.
173 * @return array plugins metadata.
175 public function getPluginsMeta()
178 $dirs = glob(self
::$PLUGINS_PATH . '/*', GLOB_ONLYDIR
| GLOB_MARK
);
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)) {
188 $metaData[$plugin] = parse_ini_file($metaFile);
189 $metaData[$plugin]['order'] = array_search($plugin, $this->authorizedPlugins
);
191 if (isset($metaData[$plugin]['description'])) {
192 $metaData[$plugin]['description'] = t($metaData[$plugin]['description']);
194 // Read parameters and format them into an array.
195 if (isset($metaData[$plugin]['parameters'])) {
196 $params = explode(';', $metaData[$plugin]['parameters']);
200 $metaData[$plugin]['parameters'] = array();
201 foreach ($params as $param) {
206 $metaData[$plugin]['parameters'][$param]['value'] = '';
207 // Optional parameter description in parameter.PARAM_NAME=
208 if (isset($metaData[$plugin]['parameter.'. $param])) {
209 $metaData[$plugin]['parameters'][$param]['desc'] = t($metaData[$plugin]['parameter.'. $param]);
218 * Return the list of encountered errors.
220 * @return array List of errors (empty array if none exists).
222 public function getErrors()
224 return $this->errors
;
229 * Class PluginFileNotFoundException
231 * Raise when plugin files can't be found.
233 class PluginFileNotFoundException
extends Exception
236 * Construct exception with plugin name.
239 * @param string $pluginName name of the plugin not found
241 public function __construct($pluginName)
243 $this->message
= sprintf(t('Plugin "%s" files not found.'), $pluginName);