/library/Zend/Loader/PluginClassLoader.php

https://github.com/bruisedlee/zf2 · PHP · 226 lines · 91 code · 19 blank · 116 comment · 12 complexity · c95b9fff8acef59b0f0b64624797ff2b MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  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.
  14. *
  15. * @category Zend
  16. * @package Zend_Loader
  17. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace Zend\Loader;
  25. use ArrayIterator,
  26. IteratorAggregate,
  27. Traversable;
  28. /**
  29. * Plugin class locator interface
  30. *
  31. * @category Zend
  32. * @package Zend_Loader
  33. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class PluginClassLoader implements PluginClassLocator
  37. {
  38. /**
  39. * List of plugin name => class name pairs
  40. * @var array
  41. */
  42. protected $plugins = array();
  43. /**
  44. * Static map allow global seeding of plugin loader
  45. * @var array
  46. */
  47. protected static $staticMap = array();
  48. /**
  49. * Constructor
  50. *
  51. * @param null|array|Traversable $map If provided, seeds the loader with a map
  52. * @return void
  53. */
  54. public function __construct($map = null)
  55. {
  56. // Merge in static overrides
  57. if (!empty(static::$staticMap)) {
  58. $this->registerPlugins(static::$staticMap);
  59. }
  60. // Merge in constructor arguments
  61. if ($map !== null) {
  62. $this->registerPlugins($map);
  63. }
  64. }
  65. /**
  66. * Add a static map of plugins
  67. *
  68. * A null value will clear the static map.
  69. *
  70. * @param null|array|Traversable $map
  71. * @return void
  72. */
  73. public static function addStaticMap($map)
  74. {
  75. if (null === $map) {
  76. static::$staticMap = array();
  77. return;
  78. }
  79. if (!is_array($map) && !$map instanceof \Traversable) {
  80. throw new Exception\InvalidArgumentException('Expects an array or Traversable object');
  81. }
  82. foreach ($map as $key => $value) {
  83. static::$staticMap[$key] = $value;
  84. }
  85. }
  86. /**
  87. * Register a class to a given short name
  88. *
  89. * @param string $shortName
  90. * @param string $className
  91. * @return PluginClassLoader
  92. */
  93. public function registerPlugin($shortName, $className)
  94. {
  95. $this->plugins[strtolower($shortName)] = $className;
  96. return $this;
  97. }
  98. /**
  99. * Register many plugins at once
  100. *
  101. * If $map is a string, assumes that the map is the class name of a
  102. * Traversable object (likely a ShortNameLocator); it will then instantiate
  103. * this class and use it to register plugins.
  104. *
  105. * If $map is an array or Traversable object, it will iterate it to
  106. * register plugin names/classes.
  107. *
  108. * For all other arguments, or if the string $map is not a class or not a
  109. * Traversable class, an exception will be raised.
  110. *
  111. * @param string|array|Traversable $map
  112. * @return PluginClassLoader
  113. * @throws Exception\InvalidArgumentException
  114. */
  115. public function registerPlugins($map)
  116. {
  117. if (is_string($map)) {
  118. if (!class_exists($map)) {
  119. throw new Exception\InvalidArgumentException('Map class provided is invalid');
  120. }
  121. $map = new $map;
  122. }
  123. if (is_array($map)) {
  124. $map = new ArrayIterator($map);
  125. }
  126. if (!$map instanceof Traversable) {
  127. throw new Exception\InvalidArgumentException('Map provided is invalid; must be traversable');
  128. }
  129. // iterator_apply doesn't work as expected with IteratorAggregate
  130. if ($map instanceof IteratorAggregate) {
  131. $map = $map->getIterator();
  132. }
  133. foreach ($map as $name => $class) {
  134. $this->registerPlugin($name, $class);
  135. }
  136. return $this;
  137. }
  138. /**
  139. * Unregister a short name lookup
  140. *
  141. * @param mixed $shortName
  142. * @return PluginClassLoader
  143. */
  144. public function unregisterPlugin($shortName)
  145. {
  146. $lookup = strtolower($shortName);
  147. if (array_key_exists($lookup, $this->plugins)) {
  148. unset($this->plugins[$lookup]);
  149. }
  150. return $this;
  151. }
  152. /**
  153. * Get a list of all registered plugins
  154. *
  155. * @return array|Traversable
  156. */
  157. public function getRegisteredPlugins()
  158. {
  159. return $this->plugins;
  160. }
  161. /**
  162. * Whether or not a plugin by a specific name has been registered
  163. *
  164. * @param string $name
  165. * @return bool
  166. */
  167. public function isLoaded($name)
  168. {
  169. $lookup = strtolower($name);
  170. return isset($this->plugins[$lookup]);
  171. }
  172. /**
  173. * Return full class name for a named helper
  174. *
  175. * @param string $name
  176. * @return string|false
  177. */
  178. public function getClassName($name)
  179. {
  180. return $this->load($name);
  181. }
  182. /**
  183. * Load a helper via the name provided
  184. *
  185. * @param string $name
  186. * @return string|false
  187. */
  188. public function load($name)
  189. {
  190. if (!$this->isLoaded($name)) {
  191. return false;
  192. }
  193. return $this->plugins[strtolower($name)];
  194. }
  195. /**
  196. * Defined by IteratorAggregate
  197. *
  198. * Returns an instance of ArrayIterator, containing a map of
  199. * all plugins
  200. *
  201. * @return Iterator
  202. */
  203. public function getIterator()
  204. {
  205. return new ArrayIterator($this->plugins);
  206. }
  207. }