Chain.php 6.4 KB

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