Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Loader / PluginClassLoader.php
bloba2223d5305ccedb506e21f4ab7334f3bfcd18e71
1 <?php
2 /**
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
8 */
10 namespace Zend\Loader;
12 use ArrayIterator;
13 use IteratorAggregate;
14 use Traversable;
16 /**
17 * Plugin class locator interface
19 class PluginClassLoader implements PluginClassLocator
21 /**
22 * List of plugin name => class name pairs
23 * @var array
25 protected $plugins = array();
27 /**
28 * Static map allow global seeding of plugin loader
29 * @var array
31 protected static $staticMap = array();
33 /**
34 * Constructor
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
46 if ($map !== null) {
47 $this->registerPlugins($map);
51 /**
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
58 * @return void
60 public static function addStaticMap($map)
62 if (null === $map) {
63 static::$staticMap = array();
64 return;
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;
75 /**
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;
85 return $this;
88 /**
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');
111 $map = new $map;
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);
133 continue;
137 $this->registerPlugin($name, $class);
140 return $this;
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]);
155 return $this;
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
172 * @return bool
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)) {
200 return false;
202 return $this->plugins[strtolower($name)];
206 * Defined by IteratorAggregate
208 * Returns an instance of ArrayIterator, containing a map of
209 * all plugins
211 * @return ArrayIterator
213 public function getIterator()
215 return new ArrayIterator($this->plugins);