3 * Zend Framework (http://framework.zend.com/)
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
10 namespace Zend\Mvc\Router\Http
;
13 use Zend\Mvc\Router\Exception
;
14 use Zend\Stdlib\ArrayUtils
;
15 use Zend\Stdlib\RequestInterface
as Request
;
20 class Hostname
implements RouteInterface
30 * Regex used for matching the route.
37 * Map from regex groups to parameter names.
41 protected $paramMap = array();
51 * List of assembled parameters.
55 protected $assembledParams = array();
58 * Create a new hostname route.
60 * @param string $route
61 * @param array $constraints
62 * @param array $defaults
64 public function __construct($route, array $constraints = array(), array $defaults = array())
66 $this->defaults
= $defaults;
67 $this->parts
= $this->parseRouteDefinition($route);
68 $this->regex
= $this->buildRegex($this->parts
, $constraints);
72 * factory(): defined by RouteInterface interface.
74 * @see \Zend\Mvc\Router\RouteInterface::factory()
75 * @param array|Traversable $options
77 * @throws Exception\InvalidArgumentException
79 public static function factory($options = array())
81 if ($options instanceof Traversable
) {
82 $options = ArrayUtils
::iteratorToArray($options);
83 } elseif (!is_array($options)) {
84 throw new Exception\
InvalidArgumentException(__METHOD__
. ' expects an array or Traversable set of options');
87 if (!isset($options['route'])) {
88 throw new Exception\
InvalidArgumentException('Missing "route" in options array');
91 if (!isset($options['constraints'])) {
92 $options['constraints'] = array();
95 if (!isset($options['defaults'])) {
96 $options['defaults'] = array();
99 return new static($options['route'], $options['constraints'], $options['defaults']);
103 * Parse a route definition.
107 * @throws Exception\RuntimeException
109 protected function parseRouteDefinition($def)
112 $length = strlen($def);
114 $levelParts = array(&$parts);
117 while ($currentPos < $length) {
118 if (!preg_match('(\G(?P<literal>[a-z0-9-.]*)(?P<token>[:{\[\]]|$))', $def, $matches, 0, $currentPos)) {
119 throw new Exception\
RuntimeException('Matched hostname literal contains a disallowed character');
122 $currentPos +
= strlen($matches[0]);
124 if (!empty($matches['literal'])) {
125 $levelParts[$level][] = array('literal', $matches['literal']);
128 if ($matches['token'] === ':') {
129 if (!preg_match('(\G(?P<name>[^:.{\[\]]+)(?:{(?P<delimiters>[^}]+)})?:?)', $def, $matches, 0, $currentPos)) {
130 throw new Exception\
RuntimeException('Found empty parameter name');
133 $levelParts[$level][] = array('parameter', $matches['name'], isset($matches['delimiters']) ?
$matches['delimiters'] : null);
135 $currentPos +
= strlen($matches[0]);
136 } elseif ($matches['token'] === '[') {
137 $levelParts[$level][] = array('optional', array());
138 $levelParts[$level +
1] = &$levelParts[$level][count($levelParts[$level]) - 1][1];
141 } elseif ($matches['token'] === ']') {
142 unset($levelParts[$level]);
146 throw new Exception\
RuntimeException('Found closing bracket without matching opening bracket');
154 throw new Exception\
RuntimeException('Found unbalanced brackets');
161 * Build the matching regex from parsed parts.
163 * @param array $parts
164 * @param array $constraints
165 * @param int $groupIndex
167 * @throws Exception\RuntimeException
169 protected function buildRegex(array $parts, array $constraints, &$groupIndex = 1)
173 foreach ($parts as $part) {
176 $regex .= preg_quote($part[1]);
180 $groupName = '?P<param' . $groupIndex . '>';
182 if (isset($constraints[$part[1]])) {
183 $regex .= '(' . $groupName . $constraints[$part[1]] . ')';
184 } elseif ($part[2] === null) {
185 $regex .= '(' . $groupName . '[^.]+)';
187 $regex .= '(' . $groupName . '[^' . $part[2] . ']+)';
190 $this->paramMap
['param' . $groupIndex++
] = $part[1];
194 $regex .= '(?:' . $this->buildRegex($part[1], $constraints, $groupIndex) . ')?';
205 * @param array $parts
206 * @param array $mergedParams
207 * @param bool $isOptional
209 * @throws Exception\RuntimeException
210 * @throws Exception\InvalidArgumentException
212 protected function buildHost(array $parts, array $mergedParams, $isOptional)
218 foreach ($parts as $part) {
227 if (!isset($mergedParams[$part[1]])) {
229 throw new Exception\
InvalidArgumentException(sprintf('Missing parameter "%s"', $part[1]));
233 } elseif (!$isOptional ||
!isset($this->defaults
[$part[1]]) ||
$this->defaults
[$part[1]] !== $mergedParams[$part[1]]) {
237 $host .= $mergedParams[$part[1]];
239 $this->assembledParams
[] = $part[1];
244 $optionalPart = $this->buildHost($part[1], $mergedParams, true);
246 if ($optionalPart !== '') {
247 $host .= $optionalPart;
254 if ($isOptional && $skippable && $skip) {
262 * match(): defined by RouteInterface interface.
264 * @see \Zend\Mvc\Router\RouteInterface::match()
265 * @param Request $request
266 * @return RouteMatch|null
268 public function match(Request
$request)
270 if (!method_exists($request, 'getUri')) {
274 $uri = $request->getUri();
275 $host = $uri->getHost();
277 $result = preg_match('(^' . $this->regex
. '$)', $host, $matches);
285 foreach ($this->paramMap
as $index => $name) {
286 if (isset($matches[$index]) && $matches[$index] !== '') {
287 $params[$name] = $matches[$index];
291 return new RouteMatch(array_merge($this->defaults
, $params));
295 * assemble(): Defined by RouteInterface interface.
297 * @see \Zend\Mvc\Router\RouteInterface::assemble()
298 * @param array $params
299 * @param array $options
302 public function assemble(array $params = array(), array $options = array())
304 $this->assembledParams
= array();
306 if (isset($options['uri'])) {
307 $host = $this->buildHost(
309 array_merge($this->defaults
, $params),
313 $options['uri']->setHost($host);
316 // A hostname does not contribute to the path, thus nothing is returned.
321 * getAssembledParams(): defined by RouteInterface interface.
323 * @see RouteInterface::getAssembledParams
326 public function getAssembledParams()
328 return $this->assembledParams
;