PageRenderTime 57ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Controller/Router/Route/Hostname.php

http://digitalus-site-manager.googlecode.com/
PHP | 276 lines | 134 code | 39 blank | 103 comment | 36 complexity | df559b1e6e9862ec70d8573f6da0b0e5 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  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 9581 2008-06-01 14:08:03Z martel $
  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. * Hostname 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_Hostname extends Zend_Controller_Router_Route_Abstract
  33. {
  34. protected $_hostVariable = ':';
  35. protected $_regexDelimiter = '#';
  36. protected $_defaultRegex = null;
  37. /**
  38. * Holds names of all route's pattern variable names. Array index holds a position in host.
  39. * @var array
  40. */
  41. protected $_variables = array();
  42. /**
  43. * Holds Route patterns for all host parts. In case of a variable it stores it's regex
  44. * requirement or null. In case of a static part, it holds only it's direct value.
  45. * @var array
  46. */
  47. protected $_parts = array();
  48. /**
  49. * Holds user submitted default values for route's variables. Name and value pairs.
  50. * @var array
  51. */
  52. protected $_defaults = array();
  53. /**
  54. * Holds user submitted regular expression patterns for route's variables' values.
  55. * Name and value pairs.
  56. * @var array
  57. */
  58. protected $_requirements = array();
  59. /**
  60. * Associative array filled on match() that holds matched path values
  61. * for given variable names.
  62. * @var array
  63. */
  64. protected $_values = array();
  65. /**
  66. * Helper var that holds a count of route pattern's static parts
  67. * for validation
  68. * @var int
  69. */
  70. private $_staticCount = 0;
  71. /**
  72. * Instantiates route based on passed Zend_Config structure
  73. *
  74. * @param Zend_Config $config Configuration object
  75. */
  76. public static function getInstance(Zend_Config $config)
  77. {
  78. $reqs = ($config->reqs instanceof Zend_Config) ? $config->reqs->toArray() : array();
  79. $defs = ($config->defaults instanceof Zend_Config) ? $config->defaults->toArray() : array();
  80. return new self($config->route, $defs, $reqs);
  81. }
  82. /**
  83. * Prepares the route for mapping by splitting (exploding) it
  84. * to a corresponding atomic parts. These parts are assigned
  85. * a position which is later used for matching and preparing values.
  86. *
  87. * @param string $route Map used to match with later submitted hostname
  88. * @param array $defaults Defaults for map variables with keys as variable names
  89. * @param array $reqs Regular expression requirements for variables (keys as variable names)
  90. */
  91. public function __construct($route, $defaults = array(), $reqs = array())
  92. {
  93. $route = trim($route, '.');
  94. $this->_defaults = (array) $defaults;
  95. $this->_requirements = (array) $reqs;
  96. if ($route != '') {
  97. foreach (explode('.', $route) as $pos => $part) {
  98. if (substr($part, 0, 1) == $this->_hostVariable) {
  99. $name = substr($part, 1);
  100. $this->_parts[$pos] = (isset($reqs[$name]) ? $reqs[$name] : $this->_defaultRegex);
  101. $this->_variables[$pos] = $name;
  102. } else {
  103. $this->_parts[$pos] = $part;
  104. $this->_staticCount++;
  105. }
  106. }
  107. }
  108. }
  109. /**
  110. * Matches a user submitted path with parts defined by a map. Assigns and
  111. * returns an array of variables on a successful match.
  112. *
  113. * @param Zend_Controller_Request_Http $request Request to get the host from
  114. * @return array|false An array of assigned values or a false on a mismatch
  115. */
  116. public function match($request)
  117. {
  118. // Get the host and remove unnecessary port information
  119. $host = $request->getHttpHost();
  120. if (preg_match('#:\d+$#', $host, $result) === 1) {
  121. $host = substr($host, 0, -strlen($result[0]));
  122. }
  123. $hostStaticCount = 0;
  124. $values = array();
  125. $host = trim($host, '.');
  126. if ($host != '') {
  127. $host = explode('.', $host);
  128. foreach ($host as $pos => $hostPart) {
  129. // Host is longer than a route, it's not a match
  130. if (!array_key_exists($pos, $this->_parts)) {
  131. return false;
  132. }
  133. $name = isset($this->_variables[$pos]) ? $this->_variables[$pos] : null;
  134. $hostPart = urldecode($hostPart);
  135. // If it's a static part, match directly
  136. if ($name === null && $this->_parts[$pos] != $hostPart) {
  137. return false;
  138. }
  139. // If it's a variable with requirement, match a regex. If not - everything matches
  140. if ($this->_parts[$pos] !== null && !preg_match($this->_regexDelimiter . '^' . $this->_parts[$pos] . '$' . $this->_regexDelimiter . 'iu', $hostPart)) {
  141. return false;
  142. }
  143. // If it's a variable store it's value for later
  144. if ($name !== null) {
  145. $values[$name] = $hostPart;
  146. } else {
  147. $hostStaticCount++;
  148. }
  149. }
  150. }
  151. // Check if all static mappings have been matched
  152. if ($this->_staticCount != $hostStaticCount) {
  153. return false;
  154. }
  155. $return = $values + $this->_defaults;
  156. // Check if all map variables have been initialized
  157. foreach ($this->_variables as $var) {
  158. if (!array_key_exists($var, $return)) {
  159. return false;
  160. }
  161. }
  162. $this->_values = $values;
  163. return $return;
  164. }
  165. /**
  166. * Assembles user submitted parameters forming a hostname defined by this route
  167. *
  168. * @param array $data An array of variable and value pairs used as parameters
  169. * @param boolean $reset Whether or not to set route defaults with those provided in $data
  170. * @return string Route path with user submitted parameters
  171. */
  172. public function assemble($data = array(), $reset = false, $encode = false)
  173. {
  174. $host = array();
  175. $flag = false;
  176. foreach ($this->_parts as $key => $part) {
  177. $name = isset($this->_variables[$key]) ? $this->_variables[$key] : null;
  178. $useDefault = false;
  179. if (isset($name) && array_key_exists($name, $data) && $data[$name] === null) {
  180. $useDefault = true;
  181. }
  182. if (isset($name)) {
  183. if (isset($data[$name]) && !$useDefault) {
  184. $host[$key] = $data[$name];
  185. unset($data[$name]);
  186. } elseif (!$reset && !$useDefault && isset($this->_values[$name])) {
  187. $host[$key] = $this->_values[$name];
  188. } elseif (isset($this->_defaults[$name])) {
  189. $host[$key] = $this->_defaults[$name];
  190. } else {
  191. require_once 'Zend/Controller/Router/Exception.php';
  192. throw new Zend_Controller_Router_Exception($name . ' is not specified');
  193. }
  194. } else {
  195. $host[$key] = $part;
  196. }
  197. }
  198. $return = '';
  199. foreach (array_reverse($host, true) as $key => $value) {
  200. if ($flag || !isset($this->_variables[$key]) || $value !== $this->getDefault($this->_variables[$key])) {
  201. if ($encode) $value = urlencode($value);
  202. $return = '.' . $value . $return;
  203. $flag = true;
  204. }
  205. }
  206. $url = trim($return, '.');
  207. $request = $this->getRequest();
  208. if ($request instanceof Zend_Controller_Request_Http) {
  209. $scheme = $request->getScheme();
  210. } else {
  211. $scheme = 'http';
  212. }
  213. $hostname = implode('.', $host);
  214. $url = $scheme . '://' . $hostname;
  215. return $url;
  216. }
  217. /**
  218. * Return a single parameter of route's defaults
  219. *
  220. * @param string $name Array key of the parameter
  221. * @return string Previously set default
  222. */
  223. public function getDefault($name) {
  224. if (isset($this->_defaults[$name])) {
  225. return $this->_defaults[$name];
  226. }
  227. return null;
  228. }
  229. /**
  230. * Return an array of defaults
  231. *
  232. * @return array Route defaults
  233. */
  234. public function getDefaults() {
  235. return $this->_defaults;
  236. }
  237. }