2015-07-15 11:42:15 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class PluginManager
|
|
|
|
*
|
|
|
|
* Use to manage, load and execute plugins.
|
|
|
|
*/
|
|
|
|
class PluginManager
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* List of authorized plugins from configuration file.
|
|
|
|
* @var array $authorizedPlugins
|
|
|
|
*/
|
|
|
|
private $authorizedPlugins;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List of loaded plugins.
|
|
|
|
* @var array $loadedPlugins
|
|
|
|
*/
|
|
|
|
private $loadedPlugins = array();
|
|
|
|
|
2016-06-09 20:04:32 +02:00
|
|
|
/**
|
|
|
|
* @var ConfigManager Configuration Manager instance.
|
|
|
|
*/
|
|
|
|
protected $conf;
|
|
|
|
|
2015-07-15 11:42:15 +02:00
|
|
|
/**
|
|
|
|
* Plugins subdirectory.
|
|
|
|
* @var string $PLUGINS_PATH
|
|
|
|
*/
|
|
|
|
public static $PLUGINS_PATH = 'plugins';
|
|
|
|
|
2015-11-18 17:40:42 +01:00
|
|
|
/**
|
|
|
|
* Plugins meta files extension.
|
|
|
|
* @var string $META_EXT
|
|
|
|
*/
|
|
|
|
public static $META_EXT = 'meta';
|
|
|
|
|
2015-07-15 11:42:15 +02:00
|
|
|
/**
|
2016-06-09 20:04:32 +02:00
|
|
|
* Constructor.
|
2015-07-15 11:42:15 +02:00
|
|
|
*
|
2016-06-09 20:04:32 +02:00
|
|
|
* @param ConfigManager $conf Configuration Manager instance.
|
2015-07-15 11:42:15 +02:00
|
|
|
*/
|
2016-06-09 20:04:32 +02:00
|
|
|
public function __construct(&$conf)
|
2015-07-15 11:42:15 +02:00
|
|
|
{
|
2016-06-09 20:04:32 +02:00
|
|
|
$this->conf = $conf;
|
2015-07-15 11:42:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load plugins listed in $authorizedPlugins.
|
|
|
|
*
|
|
|
|
* @param array $authorizedPlugins Names of plugin authorized to be loaded.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function load($authorizedPlugins)
|
|
|
|
{
|
|
|
|
$this->authorizedPlugins = $authorizedPlugins;
|
|
|
|
|
|
|
|
$dirs = glob(self::$PLUGINS_PATH . '/*', GLOB_ONLYDIR);
|
|
|
|
$dirnames = array_map('basename', $dirs);
|
|
|
|
foreach ($this->authorizedPlugins as $plugin) {
|
|
|
|
$index = array_search($plugin, $dirnames);
|
|
|
|
|
|
|
|
// plugin authorized, but its folder isn't listed
|
|
|
|
if ($index === false) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->loadPlugin($dirs[$index], $plugin);
|
|
|
|
}
|
|
|
|
catch (PluginFileNotFoundException $e) {
|
|
|
|
error_log($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute all plugins registered hook.
|
|
|
|
*
|
2016-06-09 20:04:32 +02:00
|
|
|
* @param string $hook name of the hook to trigger.
|
|
|
|
* @param array $data list of data to manipulate passed by reference.
|
|
|
|
* @param array $params additional parameters such as page target.
|
2015-07-15 11:47:12 +02:00
|
|
|
*
|
2015-07-15 11:42:15 +02:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function executeHooks($hook, &$data, $params = array())
|
|
|
|
{
|
|
|
|
if (!empty($params['target'])) {
|
|
|
|
$data['_PAGE_'] = $params['target'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($params['loggedin'])) {
|
|
|
|
$data['_LOGGEDIN_'] = $params['loggedin'];
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->loadedPlugins as $plugin) {
|
|
|
|
$hookFunction = $this->buildHookName($hook, $plugin);
|
|
|
|
|
|
|
|
if (function_exists($hookFunction)) {
|
2016-06-09 20:04:32 +02:00
|
|
|
$data = call_user_func($hookFunction, $data, $this->conf);
|
2015-07-15 11:42:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load a single plugin from its files.
|
|
|
|
* Add them in $loadedPlugins if successful.
|
|
|
|
*
|
|
|
|
* @param string $dir plugin's directory.
|
|
|
|
* @param string $pluginName plugin's name.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
* @throws PluginFileNotFoundException - plugin files not found.
|
|
|
|
*/
|
|
|
|
private function loadPlugin($dir, $pluginName)
|
|
|
|
{
|
|
|
|
if (!is_dir($dir)) {
|
|
|
|
throw new PluginFileNotFoundException($pluginName);
|
|
|
|
}
|
|
|
|
|
|
|
|
$pluginFilePath = $dir . '/' . $pluginName . '.php';
|
|
|
|
if (!is_file($pluginFilePath)) {
|
|
|
|
throw new PluginFileNotFoundException($pluginName);
|
|
|
|
}
|
|
|
|
|
2016-06-09 20:04:32 +02:00
|
|
|
$conf = $this->conf;
|
2015-07-15 11:42:15 +02:00
|
|
|
include_once $pluginFilePath;
|
|
|
|
|
|
|
|
$this->loadedPlugins[] = $pluginName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct normalize hook name for a specific plugin.
|
|
|
|
*
|
|
|
|
* Format:
|
|
|
|
* hook_<plugin_name>_<hook_name>
|
|
|
|
*
|
|
|
|
* @param string $hook hook name.
|
|
|
|
* @param string $pluginName plugin name.
|
|
|
|
*
|
|
|
|
* @return string - plugin's hook name.
|
|
|
|
*/
|
|
|
|
public function buildHookName($hook, $pluginName)
|
|
|
|
{
|
|
|
|
return 'hook_' . $pluginName . '_' . $hook;
|
|
|
|
}
|
2015-11-18 17:40:42 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve plugins metadata from *.meta (INI) files into an array.
|
|
|
|
* Metadata contains:
|
|
|
|
* - plugin description [description]
|
|
|
|
* - parameters split with ';' [parameters]
|
|
|
|
*
|
|
|
|
* Respects plugins order from settings.
|
|
|
|
*
|
|
|
|
* @return array plugins metadata.
|
|
|
|
*/
|
|
|
|
public function getPluginsMeta()
|
|
|
|
{
|
|
|
|
$metaData = array();
|
|
|
|
$dirs = glob(self::$PLUGINS_PATH . '/*', GLOB_ONLYDIR | GLOB_MARK);
|
|
|
|
|
|
|
|
// Browse all plugin directories.
|
|
|
|
foreach ($dirs as $pluginDir) {
|
|
|
|
$plugin = basename($pluginDir);
|
|
|
|
$metaFile = $pluginDir . $plugin . '.' . self::$META_EXT;
|
|
|
|
if (!is_file($metaFile) || !is_readable($metaFile)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$metaData[$plugin] = parse_ini_file($metaFile);
|
|
|
|
$metaData[$plugin]['order'] = array_search($plugin, $this->authorizedPlugins);
|
|
|
|
|
|
|
|
// Read parameters and format them into an array.
|
|
|
|
if (isset($metaData[$plugin]['parameters'])) {
|
|
|
|
$params = explode(';', $metaData[$plugin]['parameters']);
|
|
|
|
} else {
|
|
|
|
$params = array();
|
|
|
|
}
|
|
|
|
$metaData[$plugin]['parameters'] = array();
|
|
|
|
foreach ($params as $param) {
|
|
|
|
if (empty($param)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-08-02 11:02:20 +02:00
|
|
|
$metaData[$plugin]['parameters'][$param]['value'] = '';
|
|
|
|
// Optional parameter description in parameter.PARAM_NAME=
|
|
|
|
if (isset($metaData[$plugin]['parameter.'. $param])) {
|
|
|
|
$metaData[$plugin]['parameters'][$param]['desc'] = $metaData[$plugin]['parameter.'. $param];
|
|
|
|
}
|
2015-11-18 17:40:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $metaData;
|
|
|
|
}
|
2015-07-15 11:42:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class PluginFileNotFoundException
|
|
|
|
*
|
|
|
|
* Raise when plugin files can't be found.
|
|
|
|
*/
|
|
|
|
class PluginFileNotFoundException extends Exception
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Construct exception with plugin name.
|
|
|
|
* Generate message.
|
|
|
|
*
|
|
|
|
* @param string $pluginName name of the plugin not found
|
|
|
|
*/
|
|
|
|
public function __construct($pluginName)
|
|
|
|
{
|
|
|
|
$this->message = 'Plugin "'. $pluginName .'" files not found.';
|
|
|
|
}
|
2016-08-13 14:22:22 +02:00
|
|
|
}
|