This repository has been archived by the owner on Mar 12, 2020. It is now read-only.
/
Handler.php
executable file
·357 lines (322 loc) · 10.6 KB
/
Handler.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
<?php
/**
* Phergie
*
* PHP version 5
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.
* It is also available through the world-wide-web at this URL:
* http://phergie.org/license
*
* @category Phergie
* @package Phergie
* @author Phergie Development Team <team@phergie.org>
* @copyright 2008-2010 Phergie Development Team (http://phergie.org)
* @license http://phergie.org/license New BSD License
* @link http://pear.phergie.org/package/Phergie
*/
/**
* Handles on-demand loading of, iteration over, and access to plugins.
*
* @category Phergie
* @package Phergie
* @author Phergie Development Team <team@phergie.org>
* @license http://phergie.org/license New BSD License
* @link http://pear.phergie.org/package/Phergie
*/
class Phergie_Plugin_Handler implements IteratorAggregate
{
/**
* Current list of plugin instances
*
* @var array
*/
protected $plugins;
/**
* Paths in which to search for plugin class files
*
* @var array
*/
protected $paths;
/**
* Flag indicating whether plugin classes should be instantiated on
* demand if they are requested but no instance currently exists
*
* @var bool
*/
protected $autoload;
/**
* Constructor to initialize class properties and add the path for core
* plugins.
*
* @return void
*/
public function __construct()
{
$this->plugins = array();
$this->paths = array();
$this->autoload = false;
$this->addPath(dirname(__FILE__), 'Phergie_Plugin_');
}
/**
* Adds a path to search for plugin class files. Paths are searched in
* the reverse order in which they are added.
*
* @param string $path Filesystem directory path
* @param string $prefix Optional class name prefix corresponding to the
* path
*
* @return Phergie_Plugin_Handler Provides a fluent interface
* @throws Phergie_Plugin_Exception
*/
public function addPath($path, $prefix = '')
{
if (!is_readable($path)) {
throw new Phergie_Plugin_Exception(
'Path "' . $path . '" does not reference a readable directory',
Phergie_Plugin_Exception::ERR_DIRECTORY_NOT_READABLE
);
}
$this->paths[] = array(
'path' => rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR,
'prefix' => $prefix
);
return $this;
}
/**
* Adds a plugin instance to the handler.
*
* @param string|Phergie_Plugin_Abstract $plugin Short name of the
* plugin class or a plugin object
* @param array $args Optional array of
* arguments to pass to the plugin constructor if a short name is
* passed for $plugin
*
* @return Phergie_Plugin_Abstract New plugin instance
*/
public function addPlugin($plugin, array $args = null)
{
// If a short plugin name is specified...
if (is_string($plugin)) {
$index = strtolower($plugin);
if (isset($this->plugins[$index])) {
return $this->plugins[$index];
}
// Attempt to locate and load the class
foreach (array_reverse($this->paths) as $path) {
$file = $path['path'] . $plugin . '.php';
if (file_exists($file)) {
include $file;
$class = $path['prefix'] . $plugin;
if (class_exists($class)) {
break;
}
unset($class);
}
}
// If the class can't be found, display an error
if (!isset($class)) {
throw new Phergie_Plugin_Exception(
'Class file for plugin "' . $plugin . '" cannot be found',
Phergie_Plugin_Exception::ERR_CLASS_NOT_FOUND
);
}
// Check to ensure the class is a plugin class
if (!is_subclass_of($class, 'Phergie_Plugin_Abstract')) {
$msg
= 'Class for plugin "' . $plugin .
'" does not extend Phergie_Plugin_Abstract';
throw new Phergie_Plugin_Exception(
$msg,
Phergie_Plugin_Exception::ERR_INCORRECT_BASE_CLASS
);
}
// Check to ensure the class can be instantiated
$reflection = new ReflectionClass($class);
if (!$reflection->isInstantiable()) {
throw new Phergie_Plugin_Exception(
'Class for plugin "' . $plugin . '" cannot be instantiated',
Phergie_Plugin_Exception::ERR_CLASS_NOT_INSTANTIABLE
);
}
// If the class is found, instantiate it
if (!empty($args)) {
$instance = $reflection->newInstanceArgs($args);
} else {
$instance = new $class;
}
// Configure and add the instance
$instance->setPluginHandler($this);
$this->plugins[$index] = $instance;
$plugin = $instance;
} elseif ($plugin instanceof Phergie_Plugin_Abstract) {
// If a plugin instance is specified...
// Add the plugin instance to the list of plugins
$this->plugins[strtolower($plugin->getName())] = $plugin;
}
return $plugin;
}
/**
* Adds multiple plugin instances to the handler.
*
* @param array $plugins List of elements where each is of the form
* 'ShortPluginName' or array('ShortPluginName', array($arg1,
* ..., $argN))
*
* @return Phergie_Plugin_Handler Provides a fluent interface
*/
public function addPlugins(array $plugins)
{
foreach ($plugins as $plugin) {
if (is_array($plugin)) {
$this->addPlugin($plugin[0], $plugin[1]);
} else {
$this->addPlugin($plugin);
}
}
return $this;
}
/**
* Removes a plugin instance from the handler.
*
* @param string|Phergie_Plugin_Abstract $plugin Short name of the
* plugin class or a plugin object
*
* @return Phergie_Plugin_Handler Provides a fluent interface
*/
public function removePlugin($plugin)
{
if ($plugin instanceof Phergie_Plugin_Abstract) {
$plugin = $plugin->getName();
}
$plugin = strtolower($plugin);
unset($this->plugins[$plugin]);
return $this;
}
/**
* Returns the corresponding instance for a specified plugin, loading it
* if it is not already loaded and autoloading is enabled.
*
* @param string $name Short name of the plugin class
*
* @return Phergie_Plugin_Abstract Plugin instance
*/
public function getPlugin($name)
{
// If the plugin is loaded, return the instance
$index = strtolower($name);
if (isset($this->plugins[$lower])) {
return $this->plugins[$lower];
}
// If autoloading is disabled, display an error
if (!$this->autoload) {
$msg
= 'Plugin "' . $name . '" has been requested, ' .
'is not loaded, and autoload is disabled';
throw new Phergie_Plugin_Exception(
$msg,
Phergie_Plugin_Exception::ERR_PLUGIN_NOT_LOADED
);
}
// If autoloading is enabled, attempt to load the plugin
$this->addPlugin($name);
// Return the added plugin
return $this->plugins[$index];
}
/**
* Returns the corresponding instances for multiple specified plugins,
* loading them if they are not already loaded and autoloading is
* enabled.
*
* @param array $names List of short names of the plugin classes
*
* @return array Associative array mapping plugin class short names to
* corresponding plugin instances
*/
public function getPlugins(array $names)
{
$plugins = array();
foreach ($names as $name) {
$plugins[$name] = $this->getPlugin($name);
}
return $plugins;
}
/**
* Returns whether or not at least one instance of a specified plugin
* class is loaded.
*
* @param string $name Short name of the plugin class
*
* @return bool TRUE if an instance exists, FALSE otherwise
*/
public function hasPlugin($name)
{
return isset($this->plugins[strtolower($name)]);
}
/**
* Sets a flag used to determine whether plugins should be loaded
* automatically if they have not been explicitly loaded.
*
* @param bool $flag TRUE to have plugins autoload (default), FALSE
* otherwise
*
* @return Phergie_Plugin_Handler Provides a fluent interface.
*/
public function setAutoload($flag = true)
{
$this->autoload = $flag;
return $this;
}
/**
* Returns the value of a flag used to determine whether plugins should
* be loaded automatically if they have not been explicitly loaded.
*
* @return bool TRUE if autoloading is enabled, FALSE otherwise
*/
public function getAutoload()
{
return $this->autoload;
}
/**
* Allows plugin instances to be accessed as properties of the handler.
*
* @param string $name Short name of the plugin
*
* @return Phergie_Plugin_Abstract Requested plugin instance
*/
public function __get($name)
{
return $this->getPlugin(ucfirst($name));
}
/**
* Returns an iterator for all currently loaded plugin instances.
*
* @return ArrayIterator
*/
public function getIterator()
{
return new ArrayIterator($this->plugins);
}
/**
* Proxies method calls to all plugins containing the called method. An
* individual plugin may short-circuit this process by explicitly
* returning false.
*
* @param string $name Name of the method called
* @param array $args Arguments passed in the method call
*
* @return Phergie_Plugin_Handler Provides a fluent interface
*/
public function __call($name, array $args)
{
foreach ($this->plugins as $plugin) {
if (call_user_func_array(array($plugin, $name), $args) === false) {
break;
}
}
return $this;
}
}