Chain.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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-2014 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-2014 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. $path = trim($request->getPathInfo(), self::URI_DELIMITER);
  81. $subPath = $path;
  82. $values = array();
  83. $numRoutes = count($this->_routes);
  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. return false;
  94. }
  95. $subPath = substr($subPath, strlen($separator));
  96. }
  97. // TODO: Should be an interface method. Hack for 1.0 BC
  98. if (!method_exists($route, 'getVersion') || $route->getVersion() == 1) {
  99. $match = $subPath;
  100. } else {
  101. $request->setPathInfo($subPath);
  102. $match = $request;
  103. }
  104. $res = $route->match($match, true, ($key == $numRoutes - 1));
  105. if ($res === false) {
  106. return false;
  107. }
  108. $matchedPath = $route->getMatchedPath();
  109. if ($matchedPath !== null) {
  110. $subPath = substr($subPath, strlen($matchedPath));
  111. $separator = substr($subPath, 0, strlen($this->_separators[$key]));
  112. }
  113. $values = $res + $values;
  114. }
  115. $request->setPathInfo($path);
  116. if ($subPath !== '' && $subPath !== false) {
  117. return false;
  118. }
  119. return $values;
  120. }
  121. /**
  122. * Assembles a URL path defined by this route
  123. *
  124. * @param array $data An array of variable and value pairs used as parameters
  125. * @param bool $reset
  126. * @param bool $encode
  127. * @return string Route path with user submitted parameters
  128. */
  129. public function assemble($data = array(), $reset = false, $encode = false)
  130. {
  131. $value = '';
  132. $numRoutes = count($this->_routes);
  133. foreach ($this->_routes as $key => $route) {
  134. if ($key > 0) {
  135. $value .= $this->_separators[$key];
  136. }
  137. $value .= $route->assemble($data, $reset, $encode, (($numRoutes - 1) > $key));
  138. if (method_exists($route, 'getVariables')) {
  139. $variables = $route->getVariables();
  140. foreach ($variables as $variable) {
  141. $data[$variable] = null;
  142. }
  143. }
  144. }
  145. return $value;
  146. }
  147. /**
  148. * Set the request object for this and the child routes
  149. *
  150. * @param Zend_Controller_Request_Abstract|null $request
  151. * @return void
  152. */
  153. public function setRequest(Zend_Controller_Request_Abstract $request = null)
  154. {
  155. $this->_request = $request;
  156. foreach ($this->_routes as $route) {
  157. if (method_exists($route, 'setRequest')) {
  158. $route->setRequest($request);
  159. }
  160. }
  161. }
  162. /**
  163. * Return a single parameter of route's defaults
  164. *
  165. * @param string $name Array key of the parameter
  166. * @return string Previously set default
  167. */
  168. public function getDefault($name)
  169. {
  170. $default = null;
  171. foreach ($this->_routes as $route) {
  172. if (method_exists($route, 'getDefault')) {
  173. $current = $route->getDefault($name);
  174. if (null !== $current) {
  175. $default = $current;
  176. }
  177. }
  178. }
  179. return $default;
  180. }
  181. /**
  182. * Return an array of defaults
  183. *
  184. * @return array Route defaults
  185. */
  186. public function getDefaults()
  187. {
  188. $defaults = array();
  189. foreach ($this->_routes as $route) {
  190. if (method_exists($route, 'getDefaults')) {
  191. $defaults = array_merge($defaults, $route->getDefaults());
  192. }
  193. }
  194. return $defaults;
  195. }
  196. }