PageRenderTime 59ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/vendors/Zend/Controller/Router/Route/Chain.php

https://bitbucket.org/negge/tlklan2
PHP | 173 lines | 85 code | 27 blank | 61 comment | 16 complexity | 212eea35cd1b10533658bb4b22f5504c MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, BSD-2-Clause, GPL-3.0
  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: Chain.php 24593 2012-01-05 20:35:02Z matthew $
  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. foreach ($this->_routes as $key => $route) {
  72. if ($key > 0
  73. && $matchedPath !== null
  74. && $subPath !== ''
  75. && $subPath !== false
  76. ) {
  77. $separator = substr($subPath, 0, strlen($this->_separators[$key]));
  78. if ($separator !== $this->_separators[$key]) {
  79. return false;
  80. }
  81. $subPath = substr($subPath, strlen($separator));
  82. }
  83. // TODO: Should be an interface method. Hack for 1.0 BC
  84. if (!method_exists($route, 'getVersion') || $route->getVersion() == 1) {
  85. $match = $subPath;
  86. } else {
  87. $request->setPathInfo($subPath);
  88. $match = $request;
  89. }
  90. $res = $route->match($match, true);
  91. if ($res === false) {
  92. return false;
  93. }
  94. $matchedPath = $route->getMatchedPath();
  95. if ($matchedPath !== null) {
  96. $subPath = substr($subPath, strlen($matchedPath));
  97. $separator = substr($subPath, 0, strlen($this->_separators[$key]));
  98. }
  99. $values = $res + $values;
  100. }
  101. $request->setPathInfo($path);
  102. if ($subPath !== '' && $subPath !== false) {
  103. return false;
  104. }
  105. return $values;
  106. }
  107. /**
  108. * Assembles a URL path defined by this route
  109. *
  110. * @param array $data An array of variable and value pairs used as parameters
  111. * @return string Route path with user submitted parameters
  112. */
  113. public function assemble($data = array(), $reset = false, $encode = false)
  114. {
  115. $value = '';
  116. $numRoutes = count($this->_routes);
  117. foreach ($this->_routes as $key => $route) {
  118. if ($key > 0) {
  119. $value .= $this->_separators[$key];
  120. }
  121. $value .= $route->assemble($data, $reset, $encode, (($numRoutes - 1) > $key));
  122. if (method_exists($route, 'getVariables')) {
  123. $variables = $route->getVariables();
  124. foreach ($variables as $variable) {
  125. $data[$variable] = null;
  126. }
  127. }
  128. }
  129. return $value;
  130. }
  131. /**
  132. * Set the request object for this and the child routes
  133. *
  134. * @param Zend_Controller_Request_Abstract|null $request
  135. * @return void
  136. */
  137. public function setRequest(Zend_Controller_Request_Abstract $request = null)
  138. {
  139. $this->_request = $request;
  140. foreach ($this->_routes as $route) {
  141. if (method_exists($route, 'setRequest')) {
  142. $route->setRequest($request);
  143. }
  144. }
  145. }
  146. }