This repository has been archived by the owner on Oct 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPluginManager.php
380 lines (328 loc) · 11.6 KB
/
PluginManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<?php
/**
* Plugin management tools
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* @copyright 2005 Camptocamp SA
* @package Common
* @version $Id$
*/
/**
* Class used to manage pool of plugins
* @package Common
*/
class PluginManager {
/**
* @var Logger
*/
private $log;
const CLIENT = 1;
const SERVER = 2;
/**
* Plugin objects storage
* @var array
*/
private $plugins = array();
private $helpers = array();
/**
* @var int
*/
private $kind;
/**
* @var ProjectHandler
*/
private $projectHandler;
static private $replacePlugin = NULL;
/**
* Constructor
* @param ProjectHandler
*/
public function __construct($kind, $projectHandler) {
$this->log = LoggerManager::getLogger(__CLASS__);
$this->kind = $kind;
$this->projectHandler = $projectHandler;
}
/**
* Tells what plugin the current one replaces.
* @param string replacement plugin name
*/
static public function replacePlugin($name) {
self::$replacePlugin = $name;
}
/**
* Returns the plugins objects list.
* @return array
*/
public function getPlugins() {
return $this->plugins;
}
/**
* Returns full plugin base path
* @param string path to plugins root
* @param string plugin name
* @return string
*/
private function getBasePluginPath($relativePath, $name) {
return CARTOWEB_HOME . $relativePath . $name . '/';
}
/**
* Returns plugin's main class file name
*
* Also depends on the project.
* @param string path to plugins root
* @param string plugin name
* @return string
*/
private function getPluginFilename($relativePath, $name) {
$lastPath = $this->kind == self::CLIENT ?
'client/' : 'server/';
return CARTOWEB_HOME .
$this->projectHandler->getPath($relativePath .
$name . '/' . $lastPath . $this->getClassName($name) . '.php');
}
/**
* Returns plugin's common file path
*
* Also depends on the project.
* @param string path to plugins root
* @param string plugin name
* @return string
*/
private function getCommonPath($relativePath, $name) {
return CARTOWEB_HOME .
$this->projectHandler->getPath($relativePath .
$name . '/' . 'common/' . ucfirst($name) . '.php');
}
/**
* Constructs a plugin class name
*
* Class names are in the form ClientMyPlugin or ServerMyPlugin.
* @param string
* @return string
*/
private function getClassName($name) {
$prefix = $this->kind == self::CLIENT ?
'Client' : 'Server';
return $prefix . ucfirst($name);
}
/**
* Returns the relative path to the plugin parent directory. The directory
* layout is as follow:
*
* CARTOWEB_HOME / relativePath / pluginName / {client,common,server,...}
*
*/
public function getRelativePath($name) {
$pluginPath = $this->projectHandler->getPath('coreplugins/' . $name);
$isCorePlugin = is_dir(CARTOWEB_HOME . $pluginPath);
return $isCorePlugin ? 'coreplugins/' : 'plugins/';
}
/**
* Tries to include plugin PHP scripts
* @param string plugin name
*/
private function includeClassFiles($name) {
$relativePath = $this->getRelativePath($name);
$includePath = $this->getPluginFilename($relativePath, $name);
$this->log->debug("trying to load class $includePath");
$this->log->debug($this->getCommonPath($relativePath, $name));
// FIXME: this won't work in case of non absolute paths
if (is_readable($this->getCommonPath($relativePath, $name)))
include_once($this->getCommonPath($relativePath, $name));
if (is_readable($includePath))
include_once($includePath);
}
/**
* Loads plugins
*
* Includes all plugin files and creates plugin object.
* @param array array of plugin names
* @param mixed optional initialization arguments
*/
public function loadPlugins($names, $initArgs=NULL) {
// TODO: manage plugin dependency, ...
if (empty($names)) {
$this->log->warn('no plugin to load');
return;
}
foreach ($names as $name) {
$className = $this->getClassName($name);
if (isset($this->$name)) {
$msg = "Plugin $className already loaded";
throw new CartocommonException($msg);
}
$this->includeClassFiles($name);
if (!class_exists($className)) {
$msg = "Couldn't load plugin $className";
throw new CartocommonException($msg);
}
$plugin = new $className();
$extendedName = $name;
if (!is_null($plugin->replacePlugin())) {
$name = $plugin->replacePlugin();
}
$plugin->setBasePath($this->getBasePluginPath(
$this->getRelativePath($name), $name));
$plugin->setName($name);
$plugin->setExtendedName($extendedName);
if ($initArgs !== NULL) {
$plugin->initializeConfig($initArgs);
}
$found = NULL;
if (!is_null($plugin->replacePlugin())) {
foreach ($this->plugins as $key => $oldPlugin) {
if ($oldPlugin->getName() == $plugin->replacePlugin()) {
$found = $key;
break;
}
}
if (!is_null($found)) {
$this->plugins[$found] = $plugin;
}
}
if (is_null($found)) {
$this->plugins[] = $plugin;
}
$this->$name = $plugin;
}
}
/**
* Calls a function on the given plugin that implements the given
* interface.
* @param PluginBase
* @param string interface name
* @param string function name
* @param array function arguments
*/
public function callPluginImplementing($plugin, $interface, $functionName,
$args = array()) {
if ($plugin instanceof $interface) {
$helperClass = $interface . 'Helper';
if (class_exists($helperClass)) {
$helperMethod = $functionName . 'Helper';
if (is_callable(array($helperClass, $helperMethod))) {
if (!array_key_exists($interface, $this->helpers)) {
$this->helpers[$interface] = new $helperClass;
}
if (!is_array($args)) {
$args = array($args);
}
$helperArgs = array_merge(array($plugin), $args);
return call_user_func_array(array(
$this->helpers[$interface],
$helperMethod),
$helperArgs);
}
}
return call_user_func_array(array($plugin, $functionName), $args);
}
}
/**
* Calls a function on plugins implementing an interface
* @param string interface name
* @param string function name
* @param array function arguments
*/
public function callPluginsImplementing($interface, $functionName,
$args = array()) {
foreach ($this->plugins as $plugin) {
$this->callPluginImplementing($plugin, $interface, $functionName,
$args);
}
}
/**
* Calls a function on a plugin implementing an interface
* IF the plugin is enabled.
* An enabled plugin is a plugin whose enableLevel property value is
* bigger or equal to the given $enableLevel argument.
* @param string interface name
* @param string function name
* @param array function arguments
*/
public function callEnabledPluginImplementing($enableLevel, $pluginName,
$interface, $functionName, $args = array()) {
$plugin = $this->getPlugin($pluginName);
if ($plugin->isEnabledAtLevel($enableLevel)) {
$this->callPluginImplementing($plugin, $interface, $functionName,
$args);
}
}
/**
* Calls a function on enabled plugins implementing an interface.
* @param string interface name
* @param string function name
* @param array function arguments
*/
public function callEnabledPluginsImplementing($enableLevel, $interface,
$functionName,
$args = array()) {
foreach ($this->plugins as $plugin) {
if ($plugin->isEnabledAtLevel($enableLevel)) {
$this->callPluginImplementing($plugin, $interface,
$functionName, $args);
}
}
}
/**
* Calls a function on plugins
* @param string function name
* @param array function arguments
*/
public function callPlugins($functionName, $args = array()) {
foreach ($this->plugins as $plugin) {
call_user_func_array(array($plugin, $functionName), $args);
}
}
/**
* Returns plugin object for a plugin name or NULL if this plugin
* is not loaded
* @param string name
* @return PluginBase
*/
public function getPlugin($pluginName) {
foreach ($this->plugins as $plugin) {
if ($pluginName == $plugin->getName()) {
return $plugin;
}
}
return NULL;
}
/**
* Returns current plugin objet
*
* Plugin name is found using URL.
* @return PluginBase
* @deprecated This method is deprecated and should not be used any more.
* See the corresponding page on how to update your plugin.
* PHP_SELF lead to XSS attack.
*/
public function getCurrentPlugin() {
throw new CartocommonException('getCurrentPlugin : deprecated function, no more usable');
/*
if (preg_match('#^(.*)(\/?)([a-z0-9_-]*)(\/?)([a-z0-9_-]*).php$#iU',
$_SERVER['PHP_SELF'],
$match)) {
$plugin = $match[3];
die('deprecated method used ');
} else {
$plugin = false;
}
if (!$plugin)
throw new CartocommonException('Failed to get current plugin id');
return $this->getPlugin($plugin);
*/
}
}