Chain.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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_Controller
  17. * @subpackage Router
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @version $Id$
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. /** Zend_Controller_Router_Route_Abstract */
  23. require_once 'Zend/Controller/Router/Route/Abstract.php';
  24. /**
  25. * Chain route is used for managing route chaining.
  26. *
  27. * @package Zend_Controller
  28. * @subpackage Router
  29. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_Controller_Router_Route_Chain extends Zend_Controller_Router_Route_Abstract
  33. {
  34. /**
  35. * Routes
  36. *
  37. * @var array
  38. */
  39. protected $_routes = array();
  40. /**
  41. * Separators
  42. *
  43. * @var array
  44. */
  45. protected $_separators = array();
  46. /**
  47. * Instantiates route based on passed Zend_Config structure
  48. *
  49. * @param Zend_Config $config Configuration object
  50. * @return Zend_Controller_Router_Route_Chain
  51. */
  52. public static function getInstance(Zend_Config $config)
  53. {
  54. $defs = ($config->defaults instanceof Zend_Config) ? $config->defaults->toArray() : array();
  55. return new self($config->route, $defs);
  56. }
  57. /**
  58. * Add a route to this chain
  59. *
  60. * @param Zend_Controller_Router_Route_Abstract $route
  61. * @param string $separator
  62. * @return Zend_Controller_Router_Route_Chain
  63. */
  64. public function chain(Zend_Controller_Router_Route_Abstract $route, $separator = self::URI_DELIMITER)
  65. {
  66. $this->_routes[] = $route;
  67. $this->_separators[] = $separator;
  68. return $this;
  69. }
  70. /**
  71. * Matches a user submitted path with a previously defined route.
  72. * Assigns and returns an array of defaults on a successful match.
  73. *
  74. * @param Zend_Controller_Request_Http $request Request to get the path info from
  75. * @param null $partial
  76. * @return array|false An array of assigned values or a false on a mismatch
  77. */
  78. public function match($request, $partial = null)
  79. {
  80. $rawPath = $request->getPathInfo();
  81. $path = trim($request->getPathInfo(), self::URI_DELIMITER);
  82. $subPath = $path;
  83. $values = array();
  84. $matchedPath = null;
  85. foreach ($this->_routes as $key => $route) {
  86. if ($key > 0
  87. && $matchedPath !== null
  88. && $subPath !== ''
  89. && $subPath !== false
  90. ) {
  91. $separator = substr($subPath, 0, strlen($this->_separators[$key]));
  92. if ($separator !== $this->_separators[$key]) {
  93. $request->setPathInfo($rawPath);
  94. return false;
  95. }
  96. $subPath = substr($subPath, strlen($separator));
  97. }
  98. // TODO: Should be an interface method. Hack for 1.0 BC
  99. if (!method_exists($route, 'getVersion') || $route->getVersion() == 1) {
  100. $match = $subPath;
  101. } else {
  102. $request->setPathInfo($subPath);
  103. $match = $request;
  104. }
  105. $res = $route->match($match, true);
  106. if ($res === false) {
  107. $request->setPathInfo($rawPath);
  108. return false;
  109. }
  110. $matchedPath = $route->getMatchedPath();
  111. if ($matchedPath !== null) {
  112. $subPath = substr($subPath, strlen($matchedPath));
  113. }
  114. $values = $res + $values;
  115. }
  116. $request->setPathInfo($path);
  117. if ($subPath !== '' && $subPath !== false) {
  118. return false;
  119. }
  120. return $values;
  121. }
  122. /**
  123. * Assembles a URL path defined by this route
  124. *
  125. * @param array $data An array of variable and value pairs used as parameters
  126. * @param bool $reset
  127. * @param bool $encode
  128. * @return string Route path with user submitted parameters
  129. */
  130. public function assemble($data = array(), $reset = false, $encode = false)
  131. {
  132. $value = '';
  133. $numRoutes = count($this->_routes);
  134. foreach ($this->_routes as $key => $route) {
  135. if ($key > 0) {
  136. $value .= $this->_separators[$key];
  137. }
  138. $value .= $route->assemble($data, $reset, $encode, (($numRoutes - 1) > $key));
  139. if (method_exists($route, 'getVariables')) {
  140. $variables = $route->getVariables();
  141. foreach ($variables as $variable) {
  142. $data[$variable] = null;
  143. }
  144. }
  145. }
  146. return $value;
  147. }
  148. /**
  149. * Set the request object for this and the child routes
  150. *
  151. * @param Zend_Controller_Request_Abstract|null $request
  152. * @return void
  153. */
  154. public function setRequest(Zend_Controller_Request_Abstract $request = null)
  155. {
  156. $this->_request = $request;
  157. foreach ($this->_routes as $route) {
  158. if (method_exists($route, 'setRequest')) {
  159. $route->setRequest($request);
  160. }
  161. }
  162. }
  163. /**
  164. * Return a single parameter of route's defaults
  165. *
  166. * @param string $name Array key of the parameter
  167. * @return string Previously set default
  168. */
  169. public function getDefault($name)
  170. {
  171. $default = null;
  172. foreach ($this->_routes as $route) {
  173. if (method_exists($route, 'getDefault')) {
  174. $current = $route->getDefault($name);
  175. if (null !== $current) {
  176. $default = $current;
  177. }
  178. }
  179. }
  180. return $default;
  181. }
  182. /**
  183. * Return an array of defaults
  184. *
  185. * @return array Route defaults
  186. */
  187. public function getDefaults()
  188. {
  189. $defaults = array();
  190. foreach ($this->_routes as $route) {
  191. if (method_exists($route, 'getDefaults')) {
  192. $defaults = array_merge($defaults, $route->getDefaults());
  193. }
  194. }
  195. return $defaults;
  196. }
  197. }