3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Loader
;
13 use IteratorAggregate
;
17 * Plugin class locator interface
19 class PluginClassLoader
implements PluginClassLocator
22 * List of plugin name => class name pairs
25 protected $plugins = array();
28 * Static map allow global seeding of plugin loader
31 protected static $staticMap = array();
36 * @param null|array|Traversable $map If provided, seeds the loader with a map
38 public function __construct($map = null)
40 // Merge in static overrides
41 if (!empty(static::$staticMap)) {
42 $this->registerPlugins(static::$staticMap);
45 // Merge in constructor arguments
47 $this->registerPlugins($map);
52 * Add a static map of plugins
54 * A null value will clear the static map.
56 * @param null|array|Traversable $map
57 * @throws Exception\InvalidArgumentException
60 public static function addStaticMap($map)
63 static::$staticMap = array();
67 if (!is_array($map) && !$map instanceof Traversable
) {
68 throw new Exception\
InvalidArgumentException('Expects an array or Traversable object');
70 foreach ($map as $key => $value) {
71 static::$staticMap[$key] = $value;
76 * Register a class to a given short name
78 * @param string $shortName
79 * @param string $className
80 * @return PluginClassLoader
82 public function registerPlugin($shortName, $className)
84 $this->plugins
[strtolower($shortName)] = $className;
89 * Register many plugins at once
91 * If $map is a string, assumes that the map is the class name of a
92 * Traversable object (likely a ShortNameLocator); it will then instantiate
93 * this class and use it to register plugins.
95 * If $map is an array or Traversable object, it will iterate it to
96 * register plugin names/classes.
98 * For all other arguments, or if the string $map is not a class or not a
99 * Traversable class, an exception will be raised.
101 * @param string|array|Traversable $map
102 * @return PluginClassLoader
103 * @throws Exception\InvalidArgumentException
105 public function registerPlugins($map)
107 if (is_string($map)) {
108 if (!class_exists($map)) {
109 throw new Exception\
InvalidArgumentException('Map class provided is invalid');
113 if (is_array($map)) {
114 $map = new ArrayIterator($map);
116 if (!$map instanceof Traversable
) {
117 throw new Exception\
InvalidArgumentException('Map provided is invalid; must be traversable');
120 // iterator_apply doesn't work as expected with IteratorAggregate
121 if ($map instanceof IteratorAggregate
) {
122 $map = $map->getIterator();
125 foreach ($map as $name => $class) {
126 if (is_int($name) ||
is_numeric($name)) {
127 if (!is_object($class) && class_exists($class)) {
128 $class = new $class();
131 if ($class instanceof Traversable
) {
132 $this->registerPlugins($class);
137 $this->registerPlugin($name, $class);
144 * Unregister a short name lookup
146 * @param mixed $shortName
147 * @return PluginClassLoader
149 public function unregisterPlugin($shortName)
151 $lookup = strtolower($shortName);
152 if (array_key_exists($lookup, $this->plugins
)) {
153 unset($this->plugins
[$lookup]);
159 * Get a list of all registered plugins
161 * @return array|Traversable
163 public function getRegisteredPlugins()
165 return $this->plugins
;
169 * Whether or not a plugin by a specific name has been registered
171 * @param string $name
174 public function isLoaded($name)
176 $lookup = strtolower($name);
177 return isset($this->plugins
[$lookup]);
181 * Return full class name for a named helper
183 * @param string $name
184 * @return string|false
186 public function getClassName($name)
188 return $this->load($name);
192 * Load a helper via the name provided
194 * @param string $name
195 * @return string|false
197 public function load($name)
199 if (!$this->isLoaded($name)) {
202 return $this->plugins
[strtolower($name)];
206 * Defined by IteratorAggregate
208 * Returns an instance of ArrayIterator, containing a map of
211 * @return ArrayIterator
213 public function getIterator()
215 return new ArrayIterator($this->plugins
);