/libraries/Zend/Mvc/Router/RouteBroker.php

https://github.com/kiranatama/sagalaya · PHP · 252 lines · 113 code · 28 blank · 111 comment · 16 complexity · d275e8b2394819fe4f3ea7400a787c86 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_Mvc_Router
  17. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. namespace Zend\Mvc\Router;
  21. use Zend\Loader\Broker,
  22. Zend\Loader\PluginClassLocator,
  23. Zend\Loader\ShortNameLocator,
  24. Zend\Mvc\Router\Exception;
  25. /**
  26. * RouteInterface broker.
  27. *
  28. * @category Zend
  29. * @package Zend_Mvc_Router
  30. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class RouteBroker implements Broker
  34. {
  35. /**
  36. * Default class loader to utilize with this broker.
  37. *
  38. * @var string
  39. */
  40. protected $defaultClassLoader = 'Zend\Loader\PluginClassLoader';
  41. /**
  42. * Plugin class loader used by this instance.
  43. *
  44. * @var PluginClassLocator
  45. */
  46. protected $classLoader;
  47. /**
  48. * Constructor.
  49. *
  50. * Allow configuration via options; see {@link setOptions()} for details.
  51. *
  52. * @param null|array|\Traversable $options
  53. */
  54. public function __construct($options = null)
  55. {
  56. if (null !== $options) {
  57. $this->setOptions($options);
  58. }
  59. }
  60. /**
  61. * Configure route broker.
  62. *
  63. * @param mixed $options
  64. * @return RouteBroker
  65. */
  66. public function setOptions($options)
  67. {
  68. if (!is_array($options) && !$options instanceof \Traversable) {
  69. throw new Exception\InvalidArgumentException(sprintf(
  70. 'Expected an array or Traversable; received "%s"',
  71. (is_object($options) ? get_class($options) : gettype($options))
  72. ));
  73. }
  74. foreach ($options as $key => $value) {
  75. switch (strtolower($key)) {
  76. case 'class_loader':
  77. if (is_string($value)) {
  78. if (!class_exists($value)) {
  79. throw new Exception\RuntimeException(sprintf(
  80. 'Unknown class "%s" provided as class loader option',
  81. $value
  82. ));
  83. }
  84. $value = new $value;
  85. }
  86. if ($value instanceof ShortNameLocator) {
  87. $this->setClassLoader($value);
  88. break;
  89. }
  90. if (!is_array($value) && !$value instanceof \Traversable) {
  91. throw new Exception\RuntimeException(sprintf(
  92. 'Option passed for class loader (%s) is of an unknown type',
  93. (is_object($value) ? get_class($value) : gettype($value))
  94. ));
  95. }
  96. $class = false;
  97. $options = null;
  98. foreach ($value as $k => $v) {
  99. switch (strtolower($k)) {
  100. case 'class':
  101. $class = $v;
  102. break;
  103. case 'options':
  104. $options = $v;
  105. break;
  106. default:
  107. break;
  108. }
  109. }
  110. if ($class) {
  111. $loader = new $class($options);
  112. $this->setClassLoader($loader);
  113. }
  114. break;
  115. default:
  116. // ignore unknown options
  117. break;
  118. }
  119. }
  120. return $this;
  121. }
  122. /**
  123. * load(): defined by Broker interface.
  124. *
  125. * @see Broker::load()
  126. * @param string $route
  127. * @param array $options
  128. * @return RouteInterface
  129. * @throws Exception\RuntimeException
  130. */
  131. public function load($route, array $options = array())
  132. {
  133. $routeName = strtolower($route);
  134. if (class_exists($route)) {
  135. // Allow loading fully-qualified class names via the broker
  136. $class = $route;
  137. } else {
  138. // Unqualified class names are then passed to the class loader
  139. $class = $this->getClassLoader()->load($route);
  140. if (empty($class)) {
  141. throw new Exception\RuntimeException('Unable to locate class associated with "' . $routeName . '"');
  142. }
  143. }
  144. return $class::factory($options);
  145. }
  146. /**
  147. * getPlugins(): defined by Broker interface.
  148. *
  149. * Not required in the RouteBroker.
  150. *
  151. * @see Broker::getPlugins()
  152. * @return array
  153. */
  154. public function getPlugins()
  155. {
  156. }
  157. /**
  158. * isLoaded(): defined by Broker interface.
  159. *
  160. * Not required in the RouteBroker.
  161. *
  162. * @see Broker::isLoaded()
  163. * @param string $name
  164. * @return bool
  165. */
  166. public function isLoaded($name)
  167. {
  168. }
  169. /**
  170. * register(): defined by Broker interface.
  171. *
  172. * Not required in the RouteBroker.
  173. *
  174. * @see Broker::register()
  175. * @param string $name
  176. * @param mixed $plugin
  177. * @return RouteBroker
  178. */
  179. public function register($name, $plugin)
  180. {
  181. }
  182. /**
  183. * unregister(): defined by Broker interface.
  184. *
  185. * Not required in the RouteBroker.
  186. *
  187. * @see Broker::unregister()
  188. * @param string $name
  189. * @return bool
  190. */
  191. public function unregister($name)
  192. {
  193. }
  194. /**
  195. * setClassLoader(): defined by Broker interface.
  196. *
  197. * @see Broker::setClassLoader()
  198. * @param ShortNameLocator $loader
  199. * @return RouteBroker
  200. * @throws Exception\InvalidArgumentException
  201. */
  202. public function setClassLoader(ShortNameLocator $loader)
  203. {
  204. if (!$loader instanceof PluginClassLocator) {
  205. throw new Exception\InvalidArgumentException('Expected instance of PluginClassLocator');
  206. }
  207. $this->classLoader = $loader;
  208. return $this;
  209. }
  210. /**
  211. * getClassLoader(): defined by Broker interface.
  212. *
  213. * @see Broker::getClassLoader()
  214. * @return ShortNameLocator
  215. */
  216. public function getClassLoader()
  217. {
  218. if (null === $this->classLoader) {
  219. $loaderClass = $this->defaultClassLoader;
  220. $this->setClassLoader(new $loaderClass());
  221. }
  222. return $this->classLoader;
  223. }
  224. }