PageRenderTime 54ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/zendframework/zend-mvc/src/Router/Http/Hostname.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 330 lines | 178 code | 54 blank | 98 comment | 29 complexity | 0a8f95e27decbb13bbb17b905fe65f37 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Mvc\Router\Http;
  10. use Traversable;
  11. use Zend\Mvc\Router\Exception;
  12. use Zend\Stdlib\ArrayUtils;
  13. use Zend\Stdlib\RequestInterface as Request;
  14. /**
  15. * Hostname route.
  16. */
  17. class Hostname implements RouteInterface
  18. {
  19. /**
  20. * Parts of the route.
  21. *
  22. * @var array
  23. */
  24. protected $parts;
  25. /**
  26. * Regex used for matching the route.
  27. *
  28. * @var string
  29. */
  30. protected $regex;
  31. /**
  32. * Map from regex groups to parameter names.
  33. *
  34. * @var array
  35. */
  36. protected $paramMap = array();
  37. /**
  38. * Default values.
  39. *
  40. * @var array
  41. */
  42. protected $defaults;
  43. /**
  44. * List of assembled parameters.
  45. *
  46. * @var array
  47. */
  48. protected $assembledParams = array();
  49. /**
  50. * Create a new hostname route.
  51. *
  52. * @param string $route
  53. * @param array $constraints
  54. * @param array $defaults
  55. */
  56. public function __construct($route, array $constraints = array(), array $defaults = array())
  57. {
  58. $this->defaults = $defaults;
  59. $this->parts = $this->parseRouteDefinition($route);
  60. $this->regex = $this->buildRegex($this->parts, $constraints);
  61. }
  62. /**
  63. * factory(): defined by RouteInterface interface.
  64. *
  65. * @see \Zend\Mvc\Router\RouteInterface::factory()
  66. * @param array|Traversable $options
  67. * @return Hostname
  68. * @throws Exception\InvalidArgumentException
  69. */
  70. public static function factory($options = array())
  71. {
  72. if ($options instanceof Traversable) {
  73. $options = ArrayUtils::iteratorToArray($options);
  74. } elseif (!is_array($options)) {
  75. throw new Exception\InvalidArgumentException(__METHOD__ . ' expects an array or Traversable set of options');
  76. }
  77. if (!isset($options['route'])) {
  78. throw new Exception\InvalidArgumentException('Missing "route" in options array');
  79. }
  80. if (!isset($options['constraints'])) {
  81. $options['constraints'] = array();
  82. }
  83. if (!isset($options['defaults'])) {
  84. $options['defaults'] = array();
  85. }
  86. return new static($options['route'], $options['constraints'], $options['defaults']);
  87. }
  88. /**
  89. * Parse a route definition.
  90. *
  91. * @param string $def
  92. * @return array
  93. * @throws Exception\RuntimeException
  94. */
  95. protected function parseRouteDefinition($def)
  96. {
  97. $currentPos = 0;
  98. $length = strlen($def);
  99. $parts = array();
  100. $levelParts = array(&$parts);
  101. $level = 0;
  102. while ($currentPos < $length) {
  103. if (!preg_match('(\G(?P<literal>[a-z0-9-.]*)(?P<token>[:{\[\]]|$))', $def, $matches, 0, $currentPos)) {
  104. throw new Exception\RuntimeException('Matched hostname literal contains a disallowed character');
  105. }
  106. $currentPos += strlen($matches[0]);
  107. if (!empty($matches['literal'])) {
  108. $levelParts[$level][] = array('literal', $matches['literal']);
  109. }
  110. if ($matches['token'] === ':') {
  111. if (!preg_match('(\G(?P<name>[^:.{\[\]]+)(?:{(?P<delimiters>[^}]+)})?:?)', $def, $matches, 0, $currentPos)) {
  112. throw new Exception\RuntimeException('Found empty parameter name');
  113. }
  114. $levelParts[$level][] = array('parameter', $matches['name'], isset($matches['delimiters']) ? $matches['delimiters'] : null);
  115. $currentPos += strlen($matches[0]);
  116. } elseif ($matches['token'] === '[') {
  117. $levelParts[$level][] = array('optional', array());
  118. $levelParts[$level + 1] = &$levelParts[$level][count($levelParts[$level]) - 1][1];
  119. $level++;
  120. } elseif ($matches['token'] === ']') {
  121. unset($levelParts[$level]);
  122. $level--;
  123. if ($level < 0) {
  124. throw new Exception\RuntimeException('Found closing bracket without matching opening bracket');
  125. }
  126. } else {
  127. break;
  128. }
  129. }
  130. if ($level > 0) {
  131. throw new Exception\RuntimeException('Found unbalanced brackets');
  132. }
  133. return $parts;
  134. }
  135. /**
  136. * Build the matching regex from parsed parts.
  137. *
  138. * @param array $parts
  139. * @param array $constraints
  140. * @param int $groupIndex
  141. * @return string
  142. * @throws Exception\RuntimeException
  143. */
  144. protected function buildRegex(array $parts, array $constraints, &$groupIndex = 1)
  145. {
  146. $regex = '';
  147. foreach ($parts as $part) {
  148. switch ($part[0]) {
  149. case 'literal':
  150. $regex .= preg_quote($part[1]);
  151. break;
  152. case 'parameter':
  153. $groupName = '?P<param' . $groupIndex . '>';
  154. if (isset($constraints[$part[1]])) {
  155. $regex .= '(' . $groupName . $constraints[$part[1]] . ')';
  156. } elseif ($part[2] === null) {
  157. $regex .= '(' . $groupName . '[^.]+)';
  158. } else {
  159. $regex .= '(' . $groupName . '[^' . $part[2] . ']+)';
  160. }
  161. $this->paramMap['param' . $groupIndex++] = $part[1];
  162. break;
  163. case 'optional':
  164. $regex .= '(?:' . $this->buildRegex($part[1], $constraints, $groupIndex) . ')?';
  165. break;
  166. }
  167. }
  168. return $regex;
  169. }
  170. /**
  171. * Build host.
  172. *
  173. * @param array $parts
  174. * @param array $mergedParams
  175. * @param bool $isOptional
  176. * @return string
  177. * @throws Exception\RuntimeException
  178. * @throws Exception\InvalidArgumentException
  179. */
  180. protected function buildHost(array $parts, array $mergedParams, $isOptional)
  181. {
  182. $host = '';
  183. $skip = true;
  184. $skippable = false;
  185. foreach ($parts as $part) {
  186. switch ($part[0]) {
  187. case 'literal':
  188. $host .= $part[1];
  189. break;
  190. case 'parameter':
  191. $skippable = true;
  192. if (!isset($mergedParams[$part[1]])) {
  193. if (!$isOptional) {
  194. throw new Exception\InvalidArgumentException(sprintf('Missing parameter "%s"', $part[1]));
  195. }
  196. return '';
  197. } elseif (!$isOptional || !isset($this->defaults[$part[1]]) || $this->defaults[$part[1]] !== $mergedParams[$part[1]]) {
  198. $skip = false;
  199. }
  200. $host .= $mergedParams[$part[1]];
  201. $this->assembledParams[] = $part[1];
  202. break;
  203. case 'optional':
  204. $skippable = true;
  205. $optionalPart = $this->buildHost($part[1], $mergedParams, true);
  206. if ($optionalPart !== '') {
  207. $host .= $optionalPart;
  208. $skip = false;
  209. }
  210. break;
  211. }
  212. }
  213. if ($isOptional && $skippable && $skip) {
  214. return '';
  215. }
  216. return $host;
  217. }
  218. /**
  219. * match(): defined by RouteInterface interface.
  220. *
  221. * @see \Zend\Mvc\Router\RouteInterface::match()
  222. * @param Request $request
  223. * @return RouteMatch|null
  224. */
  225. public function match(Request $request)
  226. {
  227. if (!method_exists($request, 'getUri')) {
  228. return;
  229. }
  230. $uri = $request->getUri();
  231. $host = $uri->getHost();
  232. $result = preg_match('(^' . $this->regex . '$)', $host, $matches);
  233. if (!$result) {
  234. return;
  235. }
  236. $params = array();
  237. foreach ($this->paramMap as $index => $name) {
  238. if (isset($matches[$index]) && $matches[$index] !== '') {
  239. $params[$name] = $matches[$index];
  240. }
  241. }
  242. return new RouteMatch(array_merge($this->defaults, $params));
  243. }
  244. /**
  245. * assemble(): Defined by RouteInterface interface.
  246. *
  247. * @see \Zend\Mvc\Router\RouteInterface::assemble()
  248. * @param array $params
  249. * @param array $options
  250. * @return mixed
  251. */
  252. public function assemble(array $params = array(), array $options = array())
  253. {
  254. $this->assembledParams = array();
  255. if (isset($options['uri'])) {
  256. $host = $this->buildHost(
  257. $this->parts,
  258. array_merge($this->defaults, $params),
  259. false
  260. );
  261. $options['uri']->setHost($host);
  262. }
  263. // A hostname does not contribute to the path, thus nothing is returned.
  264. return '';
  265. }
  266. /**
  267. * getAssembledParams(): defined by RouteInterface interface.
  268. *
  269. * @see RouteInterface::getAssembledParams
  270. * @return array
  271. */
  272. public function getAssembledParams()
  273. {
  274. return $this->assembledParams;
  275. }
  276. }