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 * Plugins subdirectory.
29 * @var string $PLUGINS_PATH
31 public static $PLUGINS_PATH = 'plugins';
34 * Plugins meta files extension.
35 * @var string $META_EXT
37 public static $META_EXT = 'meta';
42 * @param ConfigManager $conf Configuration Manager instance.
44 public function __construct(&$conf)
50 * Load plugins listed in $authorizedPlugins.
52 * @param array $authorizedPlugins Names of plugin authorized to be loaded.
56 public function load($authorizedPlugins)
58 $this->authorizedPlugins
= $authorizedPlugins;
60 $dirs = glob(self
::$PLUGINS_PATH . '/*', GLOB_ONLYDIR
);
61 $dirnames = array_map('basename', $dirs);
62 foreach ($this->authorizedPlugins
as $plugin) {
63 $index = array_search($plugin, $dirnames);
65 // plugin authorized, but its folder isn't listed
66 if ($index === false) {
71 $this->loadPlugin($dirs[$index], $plugin);
73 catch (PluginFileNotFoundException
$e) {
74 error_log($e->getMessage());
80 * Execute all plugins registered hook.
82 * @param string $hook name of the hook to trigger.
83 * @param array $data list of data to manipulate passed by reference.
84 * @param array $params additional parameters such as page target.
88 public function executeHooks($hook, &$data, $params = array())
90 if (!empty($params['target'])) {
91 $data['_PAGE_'] = $params['target'];
94 if (isset($params['loggedin'])) {
95 $data['_LOGGEDIN_'] = $params['loggedin'];
98 foreach ($this->loadedPlugins
as $plugin) {
99 $hookFunction = $this->buildHookName($hook, $plugin);
101 if (function_exists($hookFunction)) {
102 $data = call_user_func($hookFunction, $data, $this->conf
);
108 * Load a single plugin from its files.
109 * Add them in $loadedPlugins if successful.
111 * @param string $dir plugin's directory.
112 * @param string $pluginName plugin's name.
115 * @throws PluginFileNotFoundException - plugin files not found.
117 private function loadPlugin($dir, $pluginName)
120 throw new PluginFileNotFoundException($pluginName);
123 $pluginFilePath = $dir . '/' . $pluginName . '.php';
124 if (!is_file($pluginFilePath)) {
125 throw new PluginFileNotFoundException($pluginName);
129 include_once $pluginFilePath;
131 $this->loadedPlugins
[] = $pluginName;
135 * Construct normalize hook name for a specific plugin.
138 * hook_<plugin_name>_<hook_name>
140 * @param string $hook hook name.
141 * @param string $pluginName plugin name.
143 * @return string - plugin's hook name.
145 public function buildHookName($hook, $pluginName)
147 return 'hook_' . $pluginName . '_' . $hook;
151 * Retrieve plugins metadata from *.meta (INI) files into an array.
153 * - plugin description [description]
154 * - parameters split with ';' [parameters]
156 * Respects plugins order from settings.
158 * @return array plugins metadata.
160 public function getPluginsMeta()
163 $dirs = glob(self
::$PLUGINS_PATH . '/*', GLOB_ONLYDIR
| GLOB_MARK
);
165 // Browse all plugin directories.
166 foreach ($dirs as $pluginDir) {
167 $plugin = basename($pluginDir);
168 $metaFile = $pluginDir . $plugin . '.' . self
::$META_EXT;
169 if (!is_file($metaFile) || !is_readable($metaFile)) {
173 $metaData[$plugin] = parse_ini_file($metaFile);
174 $metaData[$plugin]['order'] = array_search($plugin, $this->authorizedPlugins
);
176 // Read parameters and format them into an array.
177 if (isset($metaData[$plugin]['parameters'])) {
178 $params = explode(';', $metaData[$plugin]['parameters']);
182 $metaData[$plugin]['parameters'] = array();
183 foreach ($params as $param) {
188 $metaData[$plugin]['parameters'][$param] = '';
197 * Class PluginFileNotFoundException
199 * Raise when plugin files can't be found.
201 class PluginFileNotFoundException
extends Exception
204 * Construct exception with plugin name.
207 * @param string $pluginName name of the plugin not found
209 public function __construct($pluginName)
211 $this->message
= 'Plugin "'. $pluginName .'" files not found.';