PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/s5-members/library/Am/Mvc/Router/Route.php

https://bitbucket.org/awylie199/s5t
PHP | 378 lines | 218 code | 56 blank | 104 comment | 81 complexity | aa69d6ac058252ae252340660ce76c17 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, Apache-2.0, LGPL-3.0, MIT, BSD-3-Clause
  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-2014 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. class Am_Mvc_Router_Route extends Am_Mvc_Router_Route_Abstract
  23. {
  24. protected static $_defaultTranslator;
  25. protected $_translator;
  26. protected static $_defaultLocale;
  27. protected $_locale;
  28. protected $_isTranslated = false;
  29. protected $_translatable = array();
  30. protected $_urlVariable = ':';
  31. protected $_urlDelimiter = self::URI_DELIMITER;
  32. protected $_regexDelimiter = '#';
  33. protected $_defaultRegex = null;
  34. /**
  35. * Holds names of all route's pattern variable names. Array index holds a position in URL.
  36. * @var array
  37. */
  38. protected $_variables = array();
  39. /**
  40. * Holds Route patterns for all URL parts. In case of a variable it stores it's regex
  41. * requirement or null. In case of a static part, it holds only it's direct value.
  42. * In case of a wildcard, it stores an asterisk (*)
  43. * @var array
  44. */
  45. protected $_parts = array();
  46. /**
  47. * Holds user submitted default values for route's variables. Name and value pairs.
  48. * @var array
  49. */
  50. protected $_defaults = array();
  51. /**
  52. * Holds user submitted regular expression patterns for route's variables' values.
  53. * Name and value pairs.
  54. * @var array
  55. */
  56. protected $_requirements = array();
  57. /**
  58. * Associative array filled on match() that holds matched path values
  59. * for given variable names.
  60. * @var array
  61. */
  62. protected $_values = array();
  63. /**
  64. * Associative array filled on match() that holds wildcard variable
  65. * names and values.
  66. * @var array
  67. */
  68. protected $_wildcardData = array();
  69. /**
  70. * Helper var that holds a count of route pattern's static parts
  71. * for validation
  72. * @var int
  73. */
  74. protected $_staticCount = 0;
  75. public function getVersion() {
  76. return 1;
  77. }
  78. /**
  79. * Prepares the route for mapping by splitting (exploding) it
  80. * to a corresponding atomic parts. These parts are assigned
  81. * a position which is later used for matching and preparing values.
  82. *
  83. * @param string $route Map used to match with later submitted URL path
  84. * @param array $defaults Defaults for map variables with keys as variable names
  85. * @param array $reqs Regular expression requirements for variables (keys as variable names)
  86. * @param Zend_Translate $translator Translator to use for this instance
  87. */
  88. public function __construct($route, $defaults = array(), $reqs = array(), Zend_Translate $translator = null, $locale = null)
  89. {
  90. $route = trim($route, $this->_urlDelimiter);
  91. $this->_defaults = (array) $defaults;
  92. $this->_requirements = (array) $reqs;
  93. $this->_translator = $translator;
  94. $this->_locale = $locale;
  95. if ($route !== '') {
  96. foreach (explode($this->_urlDelimiter, $route) as $pos => $part) {
  97. if (substr($part, 0, 1) == $this->_urlVariable && substr($part, 1, 1) != $this->_urlVariable) {
  98. $name = substr($part, 1);
  99. if (substr($name, 0, 1) === '@' && substr($name, 1, 1) !== '@') {
  100. $name = substr($name, 1);
  101. $this->_translatable[] = $name;
  102. $this->_isTranslated = true;
  103. }
  104. $this->_parts[$pos] = (isset($reqs[$name]) ? $reqs[$name] : $this->_defaultRegex);
  105. $this->_variables[$pos] = $name;
  106. } else {
  107. if (substr($part, 0, 1) == $this->_urlVariable) {
  108. $part = substr($part, 1);
  109. }
  110. if (substr($part, 0, 1) === '@' && substr($part, 1, 1) !== '@') {
  111. $this->_isTranslated = true;
  112. }
  113. $this->_parts[$pos] = $part;
  114. if ($part !== '*') {
  115. $this->_staticCount++;
  116. }
  117. }
  118. }
  119. }
  120. }
  121. /**
  122. * Matches a user submitted path with parts defined by a map. Assigns and
  123. * returns an array of variables on a successful match.
  124. *
  125. * @param string $path Path used to match against this routing map
  126. * @return array|false An array of assigned values or a false on a mismatch
  127. */
  128. public function match($path, $partial = false)
  129. {
  130. $pathStaticCount = 0;
  131. $values = array();
  132. $matchedPath = '';
  133. if (!$partial) {
  134. $path = trim($path, $this->_urlDelimiter);
  135. }
  136. if ($path !== '') {
  137. $path = explode($this->_urlDelimiter, $path);
  138. foreach ($path as $pos => $pathPart) {
  139. // Path is longer than a route, it's not a match
  140. if (!array_key_exists($pos, $this->_parts)) {
  141. if ($partial) {
  142. break;
  143. } else {
  144. return false;
  145. }
  146. }
  147. $matchedPath .= $pathPart . $this->_urlDelimiter;
  148. // If it's a wildcard, get the rest of URL as wildcard data and stop matching
  149. if ($this->_parts[$pos] == '*') {
  150. $count = count($path);
  151. for($i = $pos; $i < $count; $i+=2) {
  152. $var = urldecode($path[$i]);
  153. if (!isset($this->_wildcardData[$var]) && !isset($this->_defaults[$var]) && !isset($values[$var])) {
  154. $this->_wildcardData[$var] = (isset($path[$i+1])) ? urldecode($path[$i+1]) : null;
  155. }
  156. }
  157. $matchedPath = implode($this->_urlDelimiter, $path);
  158. break;
  159. }
  160. $name = isset($this->_variables[$pos]) ? $this->_variables[$pos] : null;
  161. $pathPart = urldecode($pathPart);
  162. // Translate value if required
  163. $part = $this->_parts[$pos];
  164. if ($this->_isTranslated && (substr($part, 0, 1) === '@' && substr($part, 1, 1) !== '@' && $name === null) || $name !== null && in_array($name, $this->_translatable)) {
  165. if (substr($part, 0, 1) === '@') {
  166. $part = substr($part, 1);
  167. }
  168. if (($originalPathPart = array_search($pathPart, $translateMessages)) !== false) {
  169. $pathPart = $originalPathPart;
  170. }
  171. }
  172. if (substr($part, 0, 2) === '@@') {
  173. $part = substr($part, 1);
  174. }
  175. // If it's a static part, match directly
  176. if ($name === null && $part != $pathPart) {
  177. return false;
  178. }
  179. // If it's a variable with requirement, match a regex. If not - everything matches
  180. if ($part !== null && !preg_match($this->_regexDelimiter . '^' . $part . '$' . $this->_regexDelimiter . 'iu', $pathPart)) {
  181. return false;
  182. }
  183. // If it's a variable store it's value for later
  184. if ($name !== null) {
  185. $values[$name] = $pathPart;
  186. } else {
  187. $pathStaticCount++;
  188. }
  189. }
  190. }
  191. // Check if all static mappings have been matched
  192. if ($this->_staticCount != $pathStaticCount) {
  193. return false;
  194. }
  195. $return = $values + $this->_wildcardData + $this->_defaults;
  196. // Check if all map variables have been initialized
  197. foreach ($this->_variables as $var) {
  198. if (!array_key_exists($var, $return)) {
  199. return false;
  200. } elseif ($return[$var] == '' || $return[$var] === null) {
  201. // Empty variable? Replace with the default value.
  202. $return[$var] = $this->_defaults[$var];
  203. }
  204. }
  205. $this->setMatchedPath(rtrim($matchedPath, $this->_urlDelimiter));
  206. $this->_values = $values;
  207. return $return;
  208. }
  209. /**
  210. * Assembles user submitted parameters forming a URL path defined by this route
  211. *
  212. * @param array $data An array of variable and value pairs used as parameters
  213. * @param boolean $reset Whether or not to set route defaults with those provided in $data
  214. * @return string Route path with user submitted parameters
  215. */
  216. public function assemble($data = array(), $reset = false, $encode = false, $partial = false)
  217. {
  218. $url = array();
  219. $flag = false;
  220. foreach ($this->_parts as $key => $part) {
  221. $name = isset($this->_variables[$key]) ? $this->_variables[$key] : null;
  222. $useDefault = false;
  223. if (isset($name) && array_key_exists($name, $data) && $data[$name] === null) {
  224. $useDefault = true;
  225. }
  226. if (isset($name)) {
  227. if (isset($data[$name]) && !$useDefault) {
  228. $value = $data[$name];
  229. unset($data[$name]);
  230. } elseif (!$reset && !$useDefault && isset($this->_values[$name])) {
  231. $value = $this->_values[$name];
  232. } elseif (!$reset && !$useDefault && isset($this->_wildcardData[$name])) {
  233. $value = $this->_wildcardData[$name];
  234. } elseif (array_key_exists($name, $this->_defaults)) {
  235. $value = $this->_defaults[$name];
  236. } else {
  237. //--//require_once 'Zend/Controller/Router/Exception.php';
  238. throw new Am_Mvc_Router_Exception($name . ' is not specified');
  239. }
  240. if ($this->_isTranslated && in_array($name, $this->_translatable)) {
  241. $url[$key] = $translator->translate($value, $locale);
  242. } else {
  243. $url[$key] = $value;
  244. }
  245. } elseif ($part != '*') {
  246. if ($this->_isTranslated && substr($part, 0, 1) === '@') {
  247. if (substr($part, 1, 1) !== '@') {
  248. $url[$key] = $translator->translate(substr($part, 1), $locale);
  249. } else {
  250. $url[$key] = substr($part, 1);
  251. }
  252. } else {
  253. if (substr($part, 0, 2) === '@@') {
  254. $part = substr($part, 1);
  255. }
  256. $url[$key] = $part;
  257. }
  258. } else {
  259. if (!$reset) $data += $this->_wildcardData;
  260. $defaults = $this->getDefaults();
  261. foreach ($data as $var => $value) {
  262. if ($value !== null && (!isset($defaults[$var]) || $value != $defaults[$var])) {
  263. $url[$key++] = $var;
  264. $url[$key++] = $value;
  265. $flag = true;
  266. }
  267. }
  268. }
  269. }
  270. $return = '';
  271. foreach (array_reverse($url, true) as $key => $value) {
  272. $defaultValue = null;
  273. if (isset($this->_variables[$key])) {
  274. $defaultValue = $this->getDefault($this->_variables[$key]);
  275. if ($this->_isTranslated && $defaultValue !== null && isset($this->_translatable[$this->_variables[$key]])) {
  276. $defaultValue = $translator->translate($defaultValue, $locale);
  277. }
  278. }
  279. if ($flag || $value !== $defaultValue || $partial) {
  280. if ($encode) $value = urlencode($value);
  281. $return = $this->_urlDelimiter . $value . $return;
  282. $flag = true;
  283. }
  284. }
  285. return trim($return, $this->_urlDelimiter);
  286. }
  287. /**
  288. * Return a single parameter of route's defaults
  289. *
  290. * @param string $name Array key of the parameter
  291. * @return string Previously set default
  292. */
  293. public function getDefault($name) {
  294. if (isset($this->_defaults[$name])) {
  295. return $this->_defaults[$name];
  296. }
  297. return null;
  298. }
  299. /**
  300. * Return an array of defaults
  301. *
  302. * @return array Route defaults
  303. */
  304. public function getDefaults() {
  305. return $this->_defaults;
  306. }
  307. /**
  308. * Get all variables which are used by the route
  309. *
  310. * @return array
  311. */
  312. public function getVariables()
  313. {
  314. return $this->_variables;
  315. }
  316. public static function getInstance(Zend_Config $config)
  317. {
  318. $reqs = ($config->reqs instanceof Zend_Config) ? $config->reqs->toArray() : array();
  319. $defs = ($config->defaults instanceof Zend_Config) ? $config->defaults->toArray() : array();
  320. return new self($config->route, $defs, $reqs);
  321. }
  322. }