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

/lib/Zend/Controller/Router/Route.php

https://github.com/jpratt/cal
PHP | 313 lines | 145 code | 57 blank | 111 comment | 46 complexity | facd3459a821fc922f4f934fdda308f0 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. * @package Zend_Controller
  16. * @subpackage Router
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @version $Id: Route.php 11073 2008-08-26 16:29:59Z dasprid $
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /** Zend_Controller_Router_Route_Abstract */
  22. #require_once 'Zend/Controller/Router/Route/Abstract.php';
  23. /**
  24. * Route
  25. *
  26. * @package Zend_Controller
  27. * @subpackage Router
  28. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. * @see http://manuals.rubyonrails.com/read/chapter/65
  31. */
  32. class Zend_Controller_Router_Route extends Zend_Controller_Router_Route_Abstract
  33. {
  34. protected $_urlVariable = ':';
  35. protected $_urlDelimiter = '/';
  36. protected $_regexDelimiter = '#';
  37. protected $_defaultRegex = null;
  38. /**
  39. * Holds names of all route's pattern variable names. Array index holds a position in URL.
  40. * @var array
  41. */
  42. protected $_variables = array();
  43. /**
  44. * Holds Route patterns for all URL parts. In case of a variable it stores it's regex
  45. * requirement or null. In case of a static part, it holds only it's direct value.
  46. * In case of a wildcard, it stores an asterisk (*)
  47. * @var array
  48. */
  49. protected $_parts = array();
  50. /**
  51. * Holds user submitted default values for route's variables. Name and value pairs.
  52. * @var array
  53. */
  54. protected $_defaults = array();
  55. /**
  56. * Holds user submitted regular expression patterns for route's variables' values.
  57. * Name and value pairs.
  58. * @var array
  59. */
  60. protected $_requirements = array();
  61. /**
  62. * Associative array filled on match() that holds matched path values
  63. * for given variable names.
  64. * @var array
  65. */
  66. protected $_values = array();
  67. /**
  68. * Associative array filled on match() that holds wildcard variable
  69. * names and values.
  70. * @var array
  71. */
  72. protected $_wildcardData = array();
  73. /**
  74. * Helper var that holds a count of route pattern's static parts
  75. * for validation
  76. * @var int
  77. */
  78. protected $_staticCount = 0;
  79. public function getVersion() {
  80. return 1;
  81. }
  82. /**
  83. * Instantiates route based on passed Zend_Config structure
  84. *
  85. * @param Zend_Config $config Configuration object
  86. */
  87. public static function getInstance(Zend_Config $config)
  88. {
  89. $reqs = ($config->reqs instanceof Zend_Config) ? $config->reqs->toArray() : array();
  90. $defs = ($config->defaults instanceof Zend_Config) ? $config->defaults->toArray() : array();
  91. return new self($config->route, $defs, $reqs);
  92. }
  93. /**
  94. * Prepares the route for mapping by splitting (exploding) it
  95. * to a corresponding atomic parts. These parts are assigned
  96. * a position which is later used for matching and preparing values.
  97. *
  98. * @param string $route Map used to match with later submitted URL path
  99. * @param array $defaults Defaults for map variables with keys as variable names
  100. * @param array $reqs Regular expression requirements for variables (keys as variable names)
  101. */
  102. public function __construct($route, $defaults = array(), $reqs = array())
  103. {
  104. $route = trim($route, $this->_urlDelimiter);
  105. $this->_defaults = (array) $defaults;
  106. $this->_requirements = (array) $reqs;
  107. if ($route != '') {
  108. foreach (explode($this->_urlDelimiter, $route) as $pos => $part) {
  109. if (substr($part, 0, 1) == $this->_urlVariable) {
  110. $name = substr($part, 1);
  111. $this->_parts[$pos] = (isset($reqs[$name]) ? $reqs[$name] : $this->_defaultRegex);
  112. $this->_variables[$pos] = $name;
  113. } else {
  114. $this->_parts[$pos] = $part;
  115. if ($part != '*') $this->_staticCount++;
  116. }
  117. }
  118. }
  119. }
  120. /**
  121. * Matches a user submitted path with parts defined by a map. Assigns and
  122. * returns an array of variables on a successful match.
  123. *
  124. * @param string $path Path used to match against this routing map
  125. * @return array|false An array of assigned values or a false on a mismatch
  126. */
  127. public function match($path)
  128. {
  129. $pathStaticCount = 0;
  130. $values = array();
  131. $path = trim($path, $this->_urlDelimiter);
  132. if ($path != '') {
  133. $path = explode($this->_urlDelimiter, $path);
  134. foreach ($path as $pos => $pathPart) {
  135. // Path is longer than a route, it's not a match
  136. if (!array_key_exists($pos, $this->_parts)) {
  137. return false;
  138. }
  139. // If it's a wildcard, get the rest of URL as wildcard data and stop matching
  140. if ($this->_parts[$pos] == '*') {
  141. $count = count($path);
  142. for($i = $pos; $i < $count; $i+=2) {
  143. $var = urldecode($path[$i]);
  144. if (!isset($this->_wildcardData[$var]) && !isset($this->_defaults[$var]) && !isset($values[$var])) {
  145. $this->_wildcardData[$var] = (isset($path[$i+1])) ? urldecode($path[$i+1]) : null;
  146. }
  147. }
  148. break;
  149. }
  150. $name = isset($this->_variables[$pos]) ? $this->_variables[$pos] : null;
  151. $pathPart = urldecode($pathPart);
  152. // If it's a static part, match directly
  153. if ($name === null && $this->_parts[$pos] != $pathPart) {
  154. return false;
  155. }
  156. // If it's a variable with requirement, match a regex. If not - everything matches
  157. if ($this->_parts[$pos] !== null && !preg_match($this->_regexDelimiter . '^' . $this->_parts[$pos] . '$' . $this->_regexDelimiter . 'iu', $pathPart)) {
  158. return false;
  159. }
  160. // If it's a variable store it's value for later
  161. if ($name !== null) {
  162. $values[$name] = $pathPart;
  163. } else {
  164. $pathStaticCount++;
  165. }
  166. }
  167. }
  168. // Check if all static mappings have been matched
  169. if ($this->_staticCount != $pathStaticCount) {
  170. return false;
  171. }
  172. $return = $values + $this->_wildcardData + $this->_defaults;
  173. // Check if all map variables have been initialized
  174. foreach ($this->_variables as $var) {
  175. if (!array_key_exists($var, $return)) {
  176. return false;
  177. }
  178. }
  179. $this->_values = $values;
  180. return $return;
  181. }
  182. /**
  183. * Assembles user submitted parameters forming a URL path defined by this route
  184. *
  185. * @param array $data An array of variable and value pairs used as parameters
  186. * @param boolean $reset Whether or not to set route defaults with those provided in $data
  187. * @return string Route path with user submitted parameters
  188. */
  189. public function assemble($data = array(), $reset = false, $encode = false)
  190. {
  191. $url = array();
  192. $flag = false;
  193. foreach ($this->_parts as $key => $part) {
  194. $name = isset($this->_variables[$key]) ? $this->_variables[$key] : null;
  195. $useDefault = false;
  196. if (isset($name) && array_key_exists($name, $data) && $data[$name] === null) {
  197. $useDefault = true;
  198. }
  199. if (isset($name)) {
  200. if (isset($data[$name]) && !$useDefault) {
  201. $url[$key] = $data[$name];
  202. unset($data[$name]);
  203. } elseif (!$reset && !$useDefault && isset($this->_values[$name])) {
  204. $url[$key] = $this->_values[$name];
  205. } elseif (!$reset && !$useDefault && isset($this->_wildcardData[$name])) {
  206. $url[$key] = $this->_wildcardData[$name];
  207. } elseif (isset($this->_defaults[$name])) {
  208. $url[$key] = $this->_defaults[$name];
  209. } else {
  210. #require_once 'Zend/Controller/Router/Exception.php';
  211. throw new Zend_Controller_Router_Exception($name . ' is not specified');
  212. }
  213. } elseif ($part != '*') {
  214. $url[$key] = $part;
  215. } else {
  216. if (!$reset) $data += $this->_wildcardData;
  217. foreach ($data as $var => $value) {
  218. if ($value !== null) {
  219. $url[$key++] = $var;
  220. $url[$key++] = $value;
  221. $flag = true;
  222. }
  223. }
  224. }
  225. }
  226. $return = '';
  227. foreach (array_reverse($url, true) as $key => $value) {
  228. if ($flag || !isset($this->_variables[$key]) || $value !== $this->getDefault($this->_variables[$key])) {
  229. if ($encode) $value = urlencode($value);
  230. $return = $this->_urlDelimiter . $value . $return;
  231. $flag = true;
  232. }
  233. }
  234. return trim($return, $this->_urlDelimiter);
  235. }
  236. /**
  237. * Return a single parameter of route's defaults
  238. *
  239. * @param string $name Array key of the parameter
  240. * @return string Previously set default
  241. */
  242. public function getDefault($name) {
  243. if (isset($this->_defaults[$name])) {
  244. return $this->_defaults[$name];
  245. }
  246. return null;
  247. }
  248. /**
  249. * Return an array of defaults
  250. *
  251. * @return array Route defaults
  252. */
  253. public function getDefaults() {
  254. return $this->_defaults;
  255. }
  256. }