diff options
Diffstat (limited to 'application/PluginManager.php')
-rw-r--r-- | application/PluginManager.php | 83 |
1 files changed, 45 insertions, 38 deletions
diff --git a/application/PluginManager.php b/application/PluginManager.php index 787ac6a9..59ece4fa 100644 --- a/application/PluginManager.php +++ b/application/PluginManager.php | |||
@@ -4,18 +4,10 @@ | |||
4 | * Class PluginManager | 4 | * Class PluginManager |
5 | * | 5 | * |
6 | * Use to manage, load and execute plugins. | 6 | * Use to manage, load and execute plugins. |
7 | * | ||
8 | * Using Singleton design pattern. | ||
9 | */ | 7 | */ |
10 | class PluginManager | 8 | class PluginManager |
11 | { | 9 | { |
12 | /** | 10 | /** |
13 | * PluginManager singleton instance. | ||
14 | * @var PluginManager $instance | ||
15 | */ | ||
16 | private static $instance; | ||
17 | |||
18 | /** | ||
19 | * List of authorized plugins from configuration file. | 11 | * List of authorized plugins from configuration file. |
20 | * @var array $authorizedPlugins | 12 | * @var array $authorizedPlugins |
21 | */ | 13 | */ |
@@ -28,45 +20,36 @@ class PluginManager | |||
28 | private $loadedPlugins = array(); | 20 | private $loadedPlugins = array(); |
29 | 21 | ||
30 | /** | 22 | /** |
31 | * Plugins subdirectory. | 23 | * @var ConfigManager Configuration Manager instance. |
32 | * @var string $PLUGINS_PATH | ||
33 | */ | 24 | */ |
34 | public static $PLUGINS_PATH = 'plugins'; | 25 | protected $conf; |
35 | 26 | ||
36 | /** | 27 | /** |
37 | * Plugins meta files extension. | 28 | * @var array List of plugin errors. |
38 | * @var string $META_EXT | ||
39 | */ | 29 | */ |
40 | public static $META_EXT = 'meta'; | 30 | protected $errors; |
41 | 31 | ||
42 | /** | 32 | /** |
43 | * Private constructor: new instances not allowed. | 33 | * Plugins subdirectory. |
34 | * @var string $PLUGINS_PATH | ||
44 | */ | 35 | */ |
45 | private function __construct() | 36 | public static $PLUGINS_PATH = 'plugins'; |
46 | { | ||
47 | } | ||
48 | 37 | ||
49 | /** | 38 | /** |
50 | * Cloning isn't allowed either. | 39 | * Plugins meta files extension. |
51 | * | 40 | * @var string $META_EXT |
52 | * @return void | ||
53 | */ | 41 | */ |
54 | private function __clone() | 42 | public static $META_EXT = 'meta'; |
55 | { | ||
56 | } | ||
57 | 43 | ||
58 | /** | 44 | /** |
59 | * Return existing instance of PluginManager, or create it. | 45 | * Constructor. |
60 | * | 46 | * |
61 | * @return PluginManager instance. | 47 | * @param ConfigManager $conf Configuration Manager instance. |
62 | */ | 48 | */ |
63 | public static function getInstance() | 49 | public function __construct(&$conf) |
64 | { | 50 | { |
65 | if (!(self::$instance instanceof self)) { | 51 | $this->conf = $conf; |
66 | self::$instance = new self(); | 52 | $this->errors = array(); |
67 | } | ||
68 | |||
69 | return self::$instance; | ||
70 | } | 53 | } |
71 | 54 | ||
72 | /** | 55 | /** |
@@ -102,9 +85,9 @@ class PluginManager | |||
102 | /** | 85 | /** |
103 | * Execute all plugins registered hook. | 86 | * Execute all plugins registered hook. |
104 | * | 87 | * |
105 | * @param string $hook name of the hook to trigger. | 88 | * @param string $hook name of the hook to trigger. |
106 | * @param array $data list of data to manipulate passed by reference. | 89 | * @param array $data list of data to manipulate passed by reference. |
107 | * @param array $params additional parameters such as page target. | 90 | * @param array $params additional parameters such as page target. |
108 | * | 91 | * |
109 | * @return void | 92 | * @return void |
110 | */ | 93 | */ |
@@ -122,13 +105,14 @@ class PluginManager | |||
122 | $hookFunction = $this->buildHookName($hook, $plugin); | 105 | $hookFunction = $this->buildHookName($hook, $plugin); |
123 | 106 | ||
124 | if (function_exists($hookFunction)) { | 107 | if (function_exists($hookFunction)) { |
125 | $data = call_user_func($hookFunction, $data); | 108 | $data = call_user_func($hookFunction, $data, $this->conf); |
126 | } | 109 | } |
127 | } | 110 | } |
128 | } | 111 | } |
129 | 112 | ||
130 | /** | 113 | /** |
131 | * Load a single plugin from its files. | 114 | * Load a single plugin from its files. |
115 | * Call the init function if it exists, and collect errors. | ||
132 | * Add them in $loadedPlugins if successful. | 116 | * Add them in $loadedPlugins if successful. |
133 | * | 117 | * |
134 | * @param string $dir plugin's directory. | 118 | * @param string $dir plugin's directory. |
@@ -148,8 +132,17 @@ class PluginManager | |||
148 | throw new PluginFileNotFoundException($pluginName); | 132 | throw new PluginFileNotFoundException($pluginName); |
149 | } | 133 | } |
150 | 134 | ||
135 | $conf = $this->conf; | ||
151 | include_once $pluginFilePath; | 136 | include_once $pluginFilePath; |
152 | 137 | ||
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); | ||
143 | } | ||
144 | } | ||
145 | |||
153 | $this->loadedPlugins[] = $pluginName; | 146 | $this->loadedPlugins[] = $pluginName; |
154 | } | 147 | } |
155 | 148 | ||
@@ -207,12 +200,26 @@ class PluginManager | |||
207 | continue; | 200 | continue; |
208 | } | 201 | } |
209 | 202 | ||
210 | $metaData[$plugin]['parameters'][$param] = ''; | 203 | $metaData[$plugin]['parameters'][$param]['value'] = ''; |
204 | // Optional parameter description in parameter.PARAM_NAME= | ||
205 | if (isset($metaData[$plugin]['parameter.'. $param])) { | ||
206 | $metaData[$plugin]['parameters'][$param]['desc'] = $metaData[$plugin]['parameter.'. $param]; | ||
207 | } | ||
211 | } | 208 | } |
212 | } | 209 | } |
213 | 210 | ||
214 | return $metaData; | 211 | return $metaData; |
215 | } | 212 | } |
213 | |||
214 | /** | ||
215 | * Return the list of encountered errors. | ||
216 | * | ||
217 | * @return array List of errors (empty array if none exists). | ||
218 | */ | ||
219 | public function getErrors() | ||
220 | { | ||
221 | return $this->errors; | ||
222 | } | ||
216 | } | 223 | } |
217 | 224 | ||
218 | /** | 225 | /** |
@@ -232,4 +239,4 @@ class PluginFileNotFoundException extends Exception | |||
232 | { | 239 | { |
233 | $this->message = 'Plugin "'. $pluginName .'" files not found.'; | 240 | $this->message = 'Plugin "'. $pluginName .'" files not found.'; |
234 | } | 241 | } |
235 | } \ No newline at end of file | 242 | } |