Chain.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. */
  41. public static function getInstance(Zend_Config $config)
  42. {
  43. $defs = ($config->defaults instanceof Zend_Config) ? $config->defaults->toArray() : array();
  44. return new self($config->route, $defs);
  45. }
  46. /**
  47. * Add a route to this chain
  48. *
  49. * @param Zend_Controller_Router_Route_Abstract $route
  50. * @param string $separator
  51. * @return Zend_Controller_Router_Route_Chain
  52. */
  53. public function chain(Zend_Controller_Router_Route_Abstract $route, $separator = self::URI_DELIMITER)
  54. {
  55. $this->_routes[] = $route;
  56. $this->_separators[] = $separator;
  57. return $this;
  58. }
  59. /**
  60. * Matches a user submitted path with a previously defined route.
  61. * Assigns and returns an array of defaults on a successful match.
  62. *
  63. * @param Zend_Controller_Request_Http $request Request to get the path info from
  64. * @return array|false An array of assigned values or a false on a mismatch
  65. */
  66. public function match($request, $partial = null)
  67. {
  68. $path = trim($request->getPathInfo(), self::URI_DELIMITER);
  69. $subPath = $path;
  70. $values = array();
  71. $numRoutes = count($this->_routes);
  72. foreach ($this->_routes as $key => $route) {
  73. if ($key > 0
  74. && $matchedPath !== null
  75. && $subPath !== ''
  76. && $subPath !== false
  77. ) {
  78. $separator = substr($subPath, 0, strlen($this->_separators[$key]));
  79. if ($separator !== $this->_separators[$key]) {
  80. return false;
  81. }
  82. $subPath = substr($subPath, strlen($separator));
  83. }
  84. // TODO: Should be an interface method. Hack for 1.0 BC
  85. if (!method_exists($route, 'getVersion') || $route->getVersion() == 1) {
  86. $match = $subPath;
  87. } else {
  88. $request->setPathInfo($subPath);
  89. $match = $request;
  90. }
  91. $res = $route->match($match, true, ($key == $numRoutes - 1));
  92. if ($res === false) {
  93. return false;
  94. }
  95. $matchedPath = $route->getMatchedPath();
  96. if ($matchedPath !== null) {
  97. $subPath = substr($subPath, strlen($matchedPath));
  98. $separator = substr($subPath, 0, strlen($this->_separators[$key]));
  99. }
  100. $values = $res + $values;
  101. }
  102. $request->setPathInfo($path);
  103. if ($subPath !== '' && $subPath !== false) {
  104. return false;
  105. }
  106. return $values;
  107. }
  108. /**
  109. * Assembles a URL path defined by this route
  110. *
  111. * @param array $data An array of variable and value pairs used as parameters
  112. * @return string Route path with user submitted parameters
  113. */
  114. public function assemble($data = array(), $reset = false, $encode = false)
  115. {
  116. $value = '';
  117. $numRoutes = count($this->_routes);
  118. foreach ($this->_routes as $key => $route) {
  119. if ($key > 0) {
  120. $value .= $this->_separators[$key];
  121. }
  122. $value .= $route->assemble($data, $reset, $encode, (($numRoutes - 1) > $key));
  123. if (method_exists($route, 'getVariables')) {
  124. $variables = $route->getVariables();
  125. foreach ($variables as $variable) {
  126. $data[$variable] = null;
  127. }
  128. }
  129. }
  130. return $value;
  131. }
  132. /**
  133. * Set the request object for this and the child routes
  134. *
  135. * @param Zend_Controller_Request_Abstract|null $request
  136. * @return void
  137. */
  138. public function setRequest(Zend_Controller_Request_Abstract $request = null)
  139. {
  140. $this->_request = $request;
  141. foreach ($this->_routes as $route) {
  142. if (method_exists($route, 'setRequest')) {
  143. $route->setRequest($request);
  144. }
  145. }
  146. }
  147. }