2 namespace Shaarli\Plugin
;
4 use Shaarli\Config\ConfigManager
;
5 use Shaarli\Plugin\Exception\PluginFileNotFoundException
;
10 * Use to manage, load and execute plugins.
15 * List of authorized plugins from configuration file.
17 * @var array $authorizedPlugins
19 private $authorizedPlugins = [];
22 * List of loaded plugins.
24 * @var array $loadedPlugins
26 private $loadedPlugins = array();
29 * @var ConfigManager Configuration Manager instance.
34 * @var array List of plugin errors.
39 * Plugins subdirectory.
41 * @var string $PLUGINS_PATH
43 public static $PLUGINS_PATH = 'plugins';
46 * Plugins meta files extension.
48 * @var string $META_EXT
50 public static $META_EXT = 'meta';
55 * @param ConfigManager $conf Configuration Manager instance.
57 public function __construct(&$conf)
60 $this->errors
= array();
64 * Load plugins listed in $authorizedPlugins.
66 * @param array $authorizedPlugins Names of plugin authorized to be loaded.
70 public function load($authorizedPlugins)
72 $this->authorizedPlugins
= $authorizedPlugins;
74 $dirs = glob(self
::$PLUGINS_PATH . '/*', GLOB_ONLYDIR
);
75 $dirnames = array_map('basename', $dirs);
76 foreach ($this->authorizedPlugins
as $plugin) {
77 $index = array_search($plugin, $dirnames);
79 // plugin authorized, but its folder isn't listed
80 if ($index === false) {
85 $this->loadPlugin($dirs[$index], $plugin);
86 } catch (PluginFileNotFoundException
$e) {
87 error_log($e->getMessage());
93 * Execute all plugins registered hook.
95 * @param string $hook name of the hook to trigger.
96 * @param array $data list of data to manipulate passed by reference.
97 * @param array $params additional parameters such as page target.
101 public function executeHooks($hook, &$data, $params = array())
103 $metadataParameters = [
104 'target' => '_PAGE_',
105 'loggedin' => '_LOGGEDIN_',
106 'basePath' => '_BASE_PATH_',
107 'bookmarkService' => '_BOOKMARK_SERVICE_',
110 foreach ($metadataParameters as $parameter => $metaKey) {
111 if (array_key_exists($parameter, $params)) {
112 $data[$metaKey] = $params[$parameter];
116 foreach ($this->loadedPlugins
as $plugin) {
117 $hookFunction = $this->buildHookName($hook, $plugin);
119 if (function_exists($hookFunction)) {
121 $data = call_user_func($hookFunction, $data, $this->conf
);
122 } catch (\Throwable
$e) {
123 $error = $plugin . t(' [plugin incompatibility]: ') . $e->getMessage();
124 $this->errors
= array_unique(array_merge($this->errors
, [$error]));
129 foreach ($metadataParameters as $metaKey) {
130 unset($data[$metaKey]);
135 * Load a single plugin from its files.
136 * Call the init function if it exists, and collect errors.
137 * Add them in $loadedPlugins if successful.
139 * @param string $dir plugin's directory.
140 * @param string $pluginName plugin's name.
143 * @throws \Shaarli\Plugin\Exception\PluginFileNotFoundException - plugin files not found.
145 private function loadPlugin($dir, $pluginName)
148 throw new PluginFileNotFoundException($pluginName);
151 $pluginFilePath = $dir . '/' . $pluginName . '.php';
152 if (!is_file($pluginFilePath)) {
153 throw new PluginFileNotFoundException($pluginName);
157 include_once $pluginFilePath;
159 $initFunction = $pluginName . '_init';
160 if (function_exists($initFunction)) {
161 $errors = call_user_func($initFunction, $this->conf
);
162 if (!empty($errors)) {
163 $this->errors
= array_merge($this->errors
, $errors);
167 $this->loadedPlugins
[] = $pluginName;
171 * Construct normalize hook name for a specific plugin.
174 * hook_<plugin_name>_<hook_name>
176 * @param string $hook hook name.
177 * @param string $pluginName plugin name.
179 * @return string - plugin's hook name.
181 public function buildHookName($hook, $pluginName)
183 return 'hook_' . $pluginName . '_' . $hook;
187 * Retrieve plugins metadata from *.meta (INI) files into an array.
189 * - plugin description [description]
190 * - parameters split with ';' [parameters]
192 * Respects plugins order from settings.
194 * @return array plugins metadata.
196 public function getPluginsMeta()
199 $dirs = glob(self
::$PLUGINS_PATH . '/*', GLOB_ONLYDIR
| GLOB_MARK
);
201 // Browse all plugin directories.
202 foreach ($dirs as $pluginDir) {
203 $plugin = basename($pluginDir);
204 $metaFile = $pluginDir . $plugin . '.' . self
::$META_EXT;
205 if (!is_file($metaFile) || !is_readable($metaFile)) {
209 $metaData[$plugin] = parse_ini_file($metaFile);
210 $metaData[$plugin]['order'] = array_search($plugin, $this->authorizedPlugins
);
212 if (isset($metaData[$plugin]['description'])) {
213 $metaData[$plugin]['description'] = t($metaData[$plugin]['description']);
215 // Read parameters and format them into an array.
216 if (isset($metaData[$plugin]['parameters'])) {
217 $params = explode(';', $metaData[$plugin]['parameters']);
221 $metaData[$plugin]['parameters'] = array();
222 foreach ($params as $param) {
227 $metaData[$plugin]['parameters'][$param]['value'] = '';
228 // Optional parameter description in parameter.PARAM_NAME=
229 if (isset($metaData[$plugin]['parameter.' . $param])) {
230 $metaData[$plugin]['parameters'][$param]['desc'] = t($metaData[$plugin]['parameter.' . $param]);
239 * Return the list of encountered errors.
241 * @return array List of errors (empty array if none exists).
243 public function getErrors()
245 return $this->errors
;