PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tine20/library/Zend/Controller/Router/Route/Chain.php

https://gitlab.com/rsilveira1987/Expresso
PHP | 169 lines | 81 code | 27 blank | 61 comment | 14 complexity | 0381447264b0b77d291bde4648d206fd MD5 | raw file
  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-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @version $Id: Chain.php 10020 2009-08-18 14:34:09Z j.fischer@metaways.de $
  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-2009 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 = '/')
  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(), '/');
  69. $subPath = $path;
  70. $values = array();
  71. foreach ($this->_routes as $key => $route) {
  72. if ($key > 0 && $matchedPath !== null) {
  73. $separator = substr($subPath, 0, strlen($this->_separators[$key]));
  74. if ($separator !== $this->_separators[$key]) {
  75. return false;
  76. }
  77. $subPath = substr($subPath, strlen($separator));
  78. }
  79. // TODO: Should be an interface method. Hack for 1.0 BC
  80. if (!method_exists($route, 'getVersion') || $route->getVersion() == 1) {
  81. $match = $subPath;
  82. } else {
  83. $request->setPathInfo($subPath);
  84. $match = $request;
  85. }
  86. $res = $route->match($match, true);
  87. if ($res === false) {
  88. return false;
  89. }
  90. $matchedPath = $route->getMatchedPath();
  91. if ($matchedPath !== null) {
  92. $subPath = substr($subPath, strlen($matchedPath));
  93. $separator = substr($subPath, 0, strlen($this->_separators[$key]));
  94. }
  95. $values = $res + $values;
  96. }
  97. $request->setPathInfo($path);
  98. if ($subPath !== '' && $subPath !== false) {
  99. return false;
  100. }
  101. return $values;
  102. }
  103. /**
  104. * Assembles a URL path defined by this route
  105. *
  106. * @param array $data An array of variable and value pairs used as parameters
  107. * @return string Route path with user submitted parameters
  108. */
  109. public function assemble($data = array(), $reset = false, $encode = false)
  110. {
  111. $value = '';
  112. $numRoutes = count($this->_routes);
  113. foreach ($this->_routes as $key => $route) {
  114. if ($key > 0) {
  115. $value .= $this->_separators[$key];
  116. }
  117. $value .= $route->assemble($data, $reset, $encode, (($numRoutes - 1) > $key));
  118. if (method_exists($route, 'getVariables')) {
  119. $variables = $route->getVariables();
  120. foreach ($variables as $variable) {
  121. $data[$variable] = null;
  122. }
  123. }
  124. }
  125. return $value;
  126. }
  127. /**
  128. * Set the request object for this and the child routes
  129. *
  130. * @param Zend_Controller_Request_Abstract|null $request
  131. * @return void
  132. */
  133. public function setRequest(Zend_Controller_Request_Abstract $request = null)
  134. {
  135. $this->_request = $request;
  136. foreach ($this->_routes as $route) {
  137. if (method_exists($route, 'setRequest')) {
  138. $route->setRequest($request);
  139. }
  140. }
  141. }
  142. }