PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/zf2/vendor/Zend/Mvc/Router/Http/Part.php

http://github.com/eryx/php-framework-benchmark
PHP | 215 lines | 99 code | 30 blank | 86 comment | 23 complexity | 69d4919b76bc92e9d013816a836ac754 MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, Apache-2.0, LGPL-2.1, LGPL-3.0, BSD-2-Clause
  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. * @subpackage Http
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace Zend\Mvc\Router\Http;
  25. use Traversable,
  26. Zend\Stdlib\IteratorToArray,
  27. Zend\Stdlib\RequestDescription as Request,
  28. Zend\Mvc\Router\RouteBroker,
  29. Zend\Mvc\Router\Exception,
  30. Zend\Mvc\Router\PriorityList;
  31. /**
  32. * Route part.
  33. *
  34. * @package Zend_Mvc_Router
  35. * @subpackage Http
  36. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. * @see http://manuals.rubyonrails.com/read/chapter/65
  39. */
  40. class Part extends TreeRouteStack implements Route
  41. {
  42. /**
  43. * Route to match.
  44. *
  45. * @var Route
  46. */
  47. protected $route;
  48. /**
  49. * Whether the route may terminate.
  50. *
  51. * @var boolean
  52. */
  53. protected $mayTerminate;
  54. /**
  55. * Child routes.
  56. *
  57. * @var mixed
  58. */
  59. protected $childRoutes;
  60. /**
  61. * Create a new part route.
  62. *
  63. * @param mixed $route
  64. * @param boolean $mayTerminate
  65. * @param RouteBroker $routeBroker
  66. * @param array $childRoutes
  67. * @return void
  68. */
  69. public function __construct($route, $mayTerminate, RouteBroker $routeBroker, array $childRoutes = null)
  70. {
  71. $this->routeBroker = $routeBroker;
  72. if (!$route instanceof Route) {
  73. $route = $this->routeFromArray($route);
  74. }
  75. if ($route instanceof self) {
  76. throw new Exception\InvalidArgumentException('Base route may not be a part route');
  77. }
  78. $this->route = $route;
  79. $this->mayTerminate = $mayTerminate;
  80. $this->childRoutes = $childRoutes;
  81. $this->routes = new PriorityList();
  82. }
  83. /**
  84. * factory(): defined by Route interface.
  85. *
  86. * @see Route::factory()
  87. * @param mixed $options
  88. * @return void
  89. */
  90. public static function factory($options = array())
  91. {
  92. if (!is_array($options) && !$options instanceof Traversable) {
  93. throw new Exception\InvalidArgumentException(__METHOD__ . ' expects an array or Traversable set of options');
  94. }
  95. // Convert options to array if Traversable object not implementing ArrayAccess
  96. if ($options instanceof Traversable && !$options instanceof ArrayAccess) {
  97. $options = IteratorToArray::convert($options);
  98. }
  99. if (!isset($options['route'])) {
  100. throw new Exception\InvalidArgumentException('Missing "route" in options array');
  101. }
  102. if (!isset($options['route_broker'])) {
  103. throw new Exception\InvalidArgumentException('Missing "route_broker" in options array');
  104. }
  105. if (!isset($options['may_terminate'])) {
  106. $options['may_terminate'] = false;
  107. }
  108. if (!isset($options['child_routes']) || !$options['child_routes']) {
  109. $options['child_routes'] = null;
  110. }
  111. return new static($options['route'], $options['may_terminate'], $options['route_broker'], $options['child_routes']);
  112. }
  113. /**
  114. * match(): defined by Route interface.
  115. *
  116. * @see Route::match()
  117. * @param Request $request
  118. * @return RouteMatch|null
  119. */
  120. public function match(Request $request, $pathOffset = null)
  121. {
  122. if ($pathOffset === null) {
  123. $pathOffset = 0;
  124. }
  125. $match = $this->route->match($request, $pathOffset);
  126. if ($match !== null && method_exists($request, 'uri')) {
  127. if ($this->childRoutes !== null) {
  128. $this->addRoutes($this->childRoutes);
  129. $this->childRoutes = null;
  130. }
  131. $nextOffset = $pathOffset + $match->getLength();
  132. $uri = $request->uri();
  133. $pathLength = strlen($uri->getPath());
  134. if ($this->mayTerminate && $nextOffset === $pathLength) {
  135. return $match;
  136. }
  137. foreach ($this->routes as $name => $route) {
  138. if (($subMatch = $route->match($request, $nextOffset)) instanceof RouteMatch) {
  139. if ($match->getLength() + $subMatch->getLength() + $pathOffset === $pathLength) {
  140. return $match->merge($subMatch)->setMatchedRouteName($name);
  141. }
  142. }
  143. }
  144. }
  145. return null;
  146. }
  147. /**
  148. * assemble(): Defined by Route interface.
  149. *
  150. * @see Route::assemble()
  151. * @param array $params
  152. * @param array $options
  153. * @return mixed
  154. */
  155. public function assemble(array $params = array(), array $options = array())
  156. {
  157. if ($this->childRoutes !== null) {
  158. $this->addRoutes($this->childRoutes);
  159. $this->childRoutes = null;
  160. }
  161. $uri = $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 $uri;
  168. }
  169. }
  170. $uri .= parent::assemble($params, $options);
  171. return $uri;
  172. }
  173. /**
  174. * getAssembledParams(): defined by Route interface.
  175. *
  176. * @see Route::getAssembledParams
  177. * @return array
  178. */
  179. public function getAssembledParams()
  180. {
  181. // Part routes may not occur as base route of other part routes, so we
  182. // don't have to return anything here.
  183. return array();
  184. }
  185. }