3 namespace Shaarli\Plugin
;
5 use Shaarli\Config\ConfigManager
;
6 use Shaarli\Plugin\Exception\PluginFileNotFoundException
;
11 * Use to manage, load and execute plugins.
16 * List of authorized plugins from configuration file.
18 * @var array $authorizedPlugins
20 private $authorizedPlugins = [];
23 * List of loaded plugins.
25 * @var array $loadedPlugins
27 private $loadedPlugins = [];
30 * @var ConfigManager Configuration Manager instance.
35 * @var array List of plugin errors.
40 * Plugins subdirectory.
42 * @var string $PLUGINS_PATH
44 public static $PLUGINS_PATH = 'plugins';
47 * Plugins meta files extension.
49 * @var string $META_EXT
51 public static $META_EXT = 'meta';
56 * @param ConfigManager $conf Configuration Manager instance.
58 public function __construct(&$conf)
65 * Load plugins listed in $authorizedPlugins.
67 * @param array $authorizedPlugins Names of plugin authorized to be loaded.
71 public function load($authorizedPlugins)
73 $this->authorizedPlugins
= $authorizedPlugins;
75 $dirs = glob(self
::$PLUGINS_PATH . '/*', GLOB_ONLYDIR
);
76 $dirnames = array_map('basename', $dirs);
77 foreach ($this->authorizedPlugins
as $plugin) {
78 $index = array_search($plugin, $dirnames);
80 // plugin authorized, but its folder isn't listed
81 if ($index === false) {
86 $this->loadPlugin($dirs[$index], $plugin);
87 } catch (PluginFileNotFoundException
$e) {
88 error_log($e->getMessage());
94 * Execute all plugins registered hook.
96 * @param string $hook name of the hook to trigger.
97 * @param array $data list of data to manipulate passed by reference.
98 * @param array $params additional parameters such as page target.
102 public function executeHooks($hook, &$data, $params = [])
104 $metadataParameters = [
105 'target' => '_PAGE_',
106 'loggedin' => '_LOGGEDIN_',
107 'basePath' => '_BASE_PATH_',
108 'rootPath' => '_ROOT_PATH_',
109 'bookmarkService' => '_BOOKMARK_SERVICE_',
112 foreach ($metadataParameters as $parameter => $metaKey) {
113 if (array_key_exists($parameter, $params)) {
114 $data[$metaKey] = $params[$parameter];
118 foreach ($this->loadedPlugins
as $plugin) {
119 $hookFunction = $this->buildHookName($hook, $plugin);
121 if (function_exists($hookFunction)) {
123 $data = call_user_func($hookFunction, $data, $this->conf
);
124 } catch (\Throwable
$e) {
125 $error = $plugin . t(' [plugin incompatibility]: ') . $e->getMessage();
126 $this->errors
= array_unique(array_merge($this->errors
, [$error]));
131 foreach ($metadataParameters as $metaKey) {
132 unset($data[$metaKey]);
137 * Load a single plugin from its files.
138 * Call the init function if it exists, and collect errors.
139 * Add them in $loadedPlugins if successful.
141 * @param string $dir plugin's directory.
142 * @param string $pluginName plugin's name.
145 * @throws \Shaarli\Plugin\Exception\PluginFileNotFoundException - plugin files not found.
147 private function loadPlugin($dir, $pluginName)
150 throw new PluginFileNotFoundException($pluginName);
153 $pluginFilePath = $dir . '/' . $pluginName . '.php';
154 if (!is_file($pluginFilePath)) {
155 throw new PluginFileNotFoundException($pluginName);
159 include_once $pluginFilePath;
161 $initFunction = $pluginName . '_init';
162 if (function_exists($initFunction)) {
163 $errors = call_user_func($initFunction, $this->conf
);
164 if (!empty($errors)) {
165 $this->errors
= array_merge($this->errors
, $errors);
169 $this->loadedPlugins
[] = $pluginName;
173 * Construct normalize hook name for a specific plugin.
176 * hook_<plugin_name>_<hook_name>
178 * @param string $hook hook name.
179 * @param string $pluginName plugin name.
181 * @return string - plugin's hook name.
183 public function buildHookName($hook, $pluginName)
185 return 'hook_' . $pluginName . '_' . $hook;
189 * Retrieve plugins metadata from *.meta (INI) files into an array.
191 * - plugin description [description]
192 * - parameters split with ';' [parameters]
194 * Respects plugins order from settings.
196 * @return array plugins metadata.
198 public function getPluginsMeta()
201 $dirs = glob(self
::$PLUGINS_PATH . '/*', GLOB_ONLYDIR
| GLOB_MARK
);
203 // Browse all plugin directories.
204 foreach ($dirs as $pluginDir) {
205 $plugin = basename($pluginDir);
206 $metaFile = $pluginDir . $plugin . '.' . self
::$META_EXT;
207 if (!is_file($metaFile) || !is_readable($metaFile)) {
211 $metaData[$plugin] = parse_ini_file($metaFile);
212 $metaData[$plugin]['order'] = array_search($plugin, $this->authorizedPlugins
);
214 if (isset($metaData[$plugin]['description'])) {
215 $metaData[$plugin]['description'] = t($metaData[$plugin]['description']);
217 // Read parameters and format them into an array.
218 if (isset($metaData[$plugin]['parameters'])) {
219 $params = explode(';', $metaData[$plugin]['parameters']);
223 $metaData[$plugin]['parameters'] = [];
224 foreach ($params as $param) {
229 $metaData[$plugin]['parameters'][$param]['value'] = '';
230 // Optional parameter description in parameter.PARAM_NAME=
231 if (isset($metaData[$plugin]['parameter.' . $param])) {
232 $metaData[$plugin]['parameters'][$param]['desc'] = t($metaData[$plugin]['parameter.' . $param]);
241 * Return the list of encountered errors.
243 * @return array List of errors (empty array if none exists).
245 public function getErrors()
247 return $this->errors
;