7
* This source file is subject to the new BSD license that is bundled
8
* with this package in the file LICENSE.txt.
9
* It is also available through the world-wide-web at this URL:
10
* http://framework.zend.com/license/new-bsd
11
* If you did not receive a copy of the license and are unable to
12
* obtain it through the world-wide-web, please send an email
13
* to license@zend.com so we can send you a copy immediately.
16
* @package Zend_Loader
17
* @subpackage PluginLoader
18
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
19
* @license http://framework.zend.com/license/new-bsd New BSD License
22
/** Zend_Loader_PluginLoader_Interface */
23
require_once 'Zend/Loader/PluginLoader/Interface.php';
26
require_once 'Zend/Loader.php';
29
* Generic plugin class loader
32
* @package Zend_Loader
33
* @subpackage PluginLoader
34
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
35
* @license http://framework.zend.com/license/new-bsd New BSD License
37
class Zend_Loader_PluginLoader implements Zend_Loader_PluginLoader_Interface
40
* Static registry property
44
static protected $_staticPrefixToPaths = array();
47
* Instance registry property
51
protected $_prefixToPaths = array();
54
* Statically loaded plugins
58
static protected $_staticLoadedPlugins = array();
61
* Instance loaded plugins
65
protected $_loadedPlugins = array();
68
* Whether to use a statically named registry for loading plugins
72
protected $_useStaticRegistry = null;
77
* @param array $prefixToPaths
78
* @param string $staticRegistryName OPTIONAL
80
public function __construct(Array $prefixToPaths = array(), $staticRegistryName = null)
82
if (is_string($staticRegistryName) && !empty($staticRegistryName)) {
83
$this->_useStaticRegistry = $staticRegistryName;
84
self::$_staticPrefixToPaths[$staticRegistryName] = array();
85
self::$_staticLoadedPlugins[$staticRegistryName] = array();
88
foreach ($prefixToPaths as $prefix => $path) {
89
$this->addPrefixPath($prefix, $path);
94
* Format prefix for internal use
96
* @param string $prefix
99
protected function _formatPrefix($prefix)
101
return rtrim($prefix, '_') . '_';
105
* Add prefixed paths to the registry of paths
107
* @param string $prefix
108
* @param string $path
109
* @return Zend_Loader_PluginLoader
111
public function addPrefixPath($prefix, $path)
113
if (!is_string($prefix) || !is_string($path)) {
114
require_once 'Zend/Loader/PluginLoader/Exception.php';
115
throw new Zend_Loader_PluginLoader_Exception('Zend_Loader_PluginLoader::addPrefixPath() method only takes strings for prefix and path.');
118
$prefix = $this->_formatPrefix($prefix);
119
$path = rtrim($path, '/\\') . '/';
121
if ($this->_useStaticRegistry) {
122
self::$_staticPrefixToPaths[$this->_useStaticRegistry][$prefix][] = $path;
124
$this->_prefixToPaths[$prefix][] = $path;
132
* @param string $prefix
133
* @return false|array False if prefix does not exist, array otherwise
135
public function getPaths($prefix = null)
137
if ((null !== $prefix) && is_string($prefix)) {
138
$prefix = $this->_formatPrefix($prefix);
139
if ($this->_useStaticRegistry) {
140
if (isset(self::$_staticPrefixToPaths[$this->_useStaticRegistry][$prefix])) {
141
return self::$_staticPrefixToPaths[$this->_useStaticRegistry][$prefix];
147
if (isset($this->_prefixToPaths[$prefix])) {
148
return $this->_prefixToPaths[$prefix];
154
if ($this->_useStaticRegistry) {
155
return self::$_staticPrefixToPaths[$this->_useStaticRegistry];
158
return $this->_prefixToPaths;
164
* @param string $prefix
165
* @return bool False only if $prefix does not exist
167
public function clearPaths($prefix = null)
169
if ((null !== $prefix) && is_string($prefix)) {
170
$prefix = $this->_formatPrefix($prefix);
171
if ($this->_useStaticRegistry) {
172
if (isset(self::$_staticPrefixToPaths[$this->_useStaticRegistry][$prefix])) {
173
unset(self::$_staticPrefixToPaths[$this->_useStaticRegistry][$prefix]);
180
if (isset($this->_prefixToPaths[$prefix])) {
181
unset($this->_prefixToPaths[$prefix]);
188
if ($this->_useStaticRegistry) {
189
self::$_staticPrefixToPaths[$this->_useStaticRegistry] = array();
191
$this->_prefixToPaths = array();
198
* Remove a prefix (or prefixed-path) from the registry
200
* @param string $prefix
201
* @param string $path OPTIONAL
202
* @return Zend_Loader_PluginLoader
204
public function removePrefixPath($prefix, $path = null)
206
$prefix = $this->_formatPrefix($prefix);
207
if ($this->_useStaticRegistry) {
208
$registry =& self::$_staticPrefixToPaths[$this->_useStaticRegistry];
210
$registry =& $this->_prefixToPaths;
213
if (!isset($registry[$prefix])) {
214
require_once 'Zend/Loader/PluginLoader/Exception.php';
215
throw new Zend_Loader_PluginLoader_Exception('Prefix ' . $prefix . ' was not found in the PluginLoader.');
219
$pos = array_search($path, $registry[$prefix]);
221
throw new Zend_Loader_PluginLoader_Exception('Prefix ' . $prefix . ' / Path ' . $path . ' was not found in the PluginLoader.');
223
unset($registry[$prefix][$pos]);
225
unset($registry[$prefix]);
232
* Normalize plugin name
234
* @param string $name
237
protected function _formatName($name)
239
return ucfirst((string) $name);
243
* Whether or not a Plugin by a specific name is loaded
245
* @param string $name
246
* @return Zend_Loader_PluginLoader
248
public function isLoaded($name)
250
$name = $this->_formatName($name);
251
if ($this->_useStaticRegistry) {
252
return isset(self::$_staticLoadedPlugins[$this->_useStaticRegistry][$name]);
255
return isset($this->_loadedPlugins[$name]);
259
* Return full class name for a named plugin
261
* @param string $name
262
* @return string|false False if class not found, class name otherwise
264
public function getClassName($name)
266
$name = $this->_formatName($name);
267
if ($this->_useStaticRegistry &&
268
isset(self::$_staticLoadedPlugins[$this->_useStaticRegistry][$name]))
270
return self::$_staticLoadedPlugins[$this->_useStaticRegistry][$name];
271
} elseif (isset($this->_loadedPlugins[$name])) {
272
return $this->_loadedPlugins[$name];
279
* Load a plugin via the name provided
281
* @param string $name
284
public function load($name)
286
$name = $this->_formatName($name);
287
if ($this->_useStaticRegistry) {
288
$registry = self::$_staticPrefixToPaths[$this->_useStaticRegistry];
290
$registry = $this->_prefixToPaths;
293
if ($this->isLoaded($name)) {
294
return $this->getClassName($name);
299
$registry = array_reverse($registry, true);
300
foreach ($registry as $prefix => $paths) {
301
$paths = array_reverse($paths, true);
302
foreach ($paths as $path) {
304
$classFile = str_replace('_', DIRECTORY_SEPARATOR, $name) . '.php';
305
$className = $prefix . $name;
307
if (class_exists($className, false)) {
312
if (Zend_Loader::isReadable($path . $classFile)) {
313
include_once $path . $classFile;
315
if (!class_exists($className, false)) {
316
throw new Zend_Loader_PluginLoader_Exception('File ' . $classFile . ' was loaded but class named ' . $className . ' was not found within it.');
326
if ($this->_useStaticRegistry) {
327
self::$_staticLoadedPlugins[$this->_useStaticRegistry][$name] = $className;
329
$this->_loadedPlugins[$name] = $className;
334
require_once 'Zend/Loader/PluginLoader/Exception.php';
335
throw new Zend_Loader_PluginLoader_Exception('Plugin by name ' . $name . ' was not found in the registry.');