PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/app/classes/Zend/Mvc/Router/Http/Part.php

https://gitlab.com/jalon/doadoronline
PHP | 234 lines | 127 code | 33 blank | 74 comment | 26 complexity | 134caa39a1215ac6cf8535110fa51b2f MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  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. */
  9. namespace Zend\Mvc\Router\Http;
  10. use ArrayObject;
  11. use Traversable;
  12. use Zend\Mvc\Router\Exception;
  13. use Zend\Mvc\Router\PriorityList;
  14. use Zend\Mvc\Router\RoutePluginManager;
  15. use Zend\Stdlib\ArrayUtils;
  16. use Zend\Stdlib\RequestInterface as Request;
  17. /**
  18. * Part route.
  19. */
  20. class Part extends TreeRouteStack implements RouteInterface
  21. {
  22. /**
  23. * RouteInterface to match.
  24. *
  25. * @var RouteInterface
  26. */
  27. protected $route;
  28. /**
  29. * Whether the route may terminate.
  30. *
  31. * @var bool
  32. */
  33. protected $mayTerminate;
  34. /**
  35. * Child routes.
  36. *
  37. * @var mixed
  38. */
  39. protected $childRoutes;
  40. /**
  41. * Create a new part route.
  42. *
  43. * @param mixed $route
  44. * @param bool $mayTerminate
  45. * @param RoutePluginManager $routePlugins
  46. * @param array|null $childRoutes
  47. * @param ArrayObject|null $prototypes
  48. * @throws Exception\InvalidArgumentException
  49. */
  50. public function __construct($route, $mayTerminate, RoutePluginManager $routePlugins, array $childRoutes = null, ArrayObject $prototypes = null)
  51. {
  52. $this->routePluginManager = $routePlugins;
  53. if (!$route instanceof RouteInterface) {
  54. $route = $this->routeFromArray($route);
  55. }
  56. if ($route instanceof self) {
  57. throw new Exception\InvalidArgumentException('Base route may not be a part route');
  58. }
  59. $this->route = $route;
  60. $this->mayTerminate = $mayTerminate;
  61. $this->childRoutes = $childRoutes;
  62. $this->prototypes = $prototypes;
  63. $this->routes = new PriorityList();
  64. }
  65. /**
  66. * factory(): defined by RouteInterface interface.
  67. *
  68. * @see \Zend\Mvc\Router\RouteInterface::factory()
  69. * @param mixed $options
  70. * @return Part
  71. * @throws Exception\InvalidArgumentException
  72. */
  73. public static function factory($options = array())
  74. {
  75. if ($options instanceof Traversable) {
  76. $options = ArrayUtils::iteratorToArray($options);
  77. } elseif (!is_array($options)) {
  78. throw new Exception\InvalidArgumentException(__METHOD__ . ' expects an array or Traversable set of options');
  79. }
  80. if (!isset($options['route'])) {
  81. throw new Exception\InvalidArgumentException('Missing "route" in options array');
  82. }
  83. if (!isset($options['route_plugins'])) {
  84. throw new Exception\InvalidArgumentException('Missing "route_plugins" in options array');
  85. }
  86. if (!isset($options['prototypes'])) {
  87. $options['prototypes'] = null;
  88. }
  89. if (!isset($options['may_terminate'])) {
  90. $options['may_terminate'] = false;
  91. }
  92. if (!isset($options['child_routes']) || !$options['child_routes']) {
  93. $options['child_routes'] = null;
  94. }
  95. if ($options['child_routes'] instanceof Traversable) {
  96. $options['child_routes'] = ArrayUtils::iteratorToArray($options['child_routes']);
  97. }
  98. return new static(
  99. $options['route'],
  100. $options['may_terminate'],
  101. $options['route_plugins'],
  102. $options['child_routes'],
  103. $options['prototypes']
  104. );
  105. }
  106. /**
  107. * match(): defined by RouteInterface interface.
  108. *
  109. * @see \Zend\Mvc\Router\RouteInterface::match()
  110. * @param Request $request
  111. * @param integer|null $pathOffset
  112. * @param array $options
  113. * @return RouteMatch|null
  114. */
  115. public function match(Request $request, $pathOffset = null, array $options = array())
  116. {
  117. if ($pathOffset === null) {
  118. $pathOffset = 0;
  119. }
  120. $match = $this->route->match($request, $pathOffset, $options);
  121. if ($match !== null && method_exists($request, 'getUri')) {
  122. if ($this->childRoutes !== null) {
  123. $this->addRoutes($this->childRoutes);
  124. $this->childRoutes = null;
  125. }
  126. $nextOffset = $pathOffset + $match->getLength();
  127. $uri = $request->getUri();
  128. $pathLength = strlen($uri->getPath());
  129. if ($this->mayTerminate && $nextOffset === $pathLength) {
  130. $query = $uri->getQuery();
  131. if ('' == trim($query) || !$this->hasQueryChild()) {
  132. return $match;
  133. }
  134. }
  135. foreach ($this->routes as $name => $route) {
  136. if (($subMatch = $route->match($request, $nextOffset, $options)) instanceof RouteMatch) {
  137. if ($match->getLength() + $subMatch->getLength() + $pathOffset === $pathLength) {
  138. return $match->merge($subMatch)->setMatchedRouteName($name);
  139. }
  140. }
  141. }
  142. }
  143. return null;
  144. }
  145. /**
  146. * assemble(): Defined by RouteInterface interface.
  147. *
  148. * @see \Zend\Mvc\Router\RouteInterface::assemble()
  149. * @param array $params
  150. * @param array $options
  151. * @return mixed
  152. * @throws Exception\RuntimeException
  153. */
  154. public function assemble(array $params = array(), array $options = array())
  155. {
  156. if ($this->childRoutes !== null) {
  157. $this->addRoutes($this->childRoutes);
  158. $this->childRoutes = null;
  159. }
  160. $options['has_child'] = (isset($options['name']));
  161. $path = $this->route->assemble($params, $options);
  162. $params = array_diff_key($params, array_flip($this->route->getAssembledParams()));
  163. if (!isset($options['name'])) {
  164. if (!$this->mayTerminate) {
  165. throw new Exception\RuntimeException('Part route may not terminate');
  166. } else {
  167. return $path;
  168. }
  169. }
  170. unset($options['has_child']);
  171. $options['only_return_path'] = true;
  172. $path .= parent::assemble($params, $options);
  173. return $path;
  174. }
  175. /**
  176. * getAssembledParams(): defined by RouteInterface interface.
  177. *
  178. * @see RouteInterface::getAssembledParams
  179. * @return array
  180. */
  181. public function getAssembledParams()
  182. {
  183. // Part routes may not occur as base route of other part routes, so we
  184. // don't have to return anything here.
  185. return array();
  186. }
  187. /**
  188. * Is one of the child routes a query route?
  189. *
  190. * @return bool
  191. */
  192. protected function hasQueryChild()
  193. {
  194. foreach ($this->routes as $route) {
  195. if ($route instanceof Query) {
  196. return true;
  197. }
  198. }
  199. return false;
  200. }
  201. }