[Routing] added hostname matching support to Route and RouteCollection

This commit is contained in:
Arnaud Le Blanc 2012-04-14 19:13:54 +02:00
parent 23feb37a98
commit add3658001
2 changed files with 44 additions and 2 deletions

View File

@ -25,6 +25,7 @@ class Route implements \Serializable
private $requirements;
private $options;
private $compiled;
private $hostnamePattern;
private static $compilers = array();
@ -39,15 +40,17 @@ class Route implements \Serializable
* @param array $defaults An array of default parameter values
* @param array $requirements An array of requirements for parameters (regexes)
* @param array $options An array of options
* @param string $hostname The hostname pattern to match
*
* @api
*/
public function __construct($pattern, array $defaults = array(), array $requirements = array(), array $options = array())
public function __construct($pattern, array $defaults = array(), array $requirements = array(), array $options = array(), $hostnamePattern = null)
{
$this->setPattern($pattern);
$this->setDefaults($defaults);
$this->setRequirements($requirements);
$this->setOptions($options);
$this->setHostnamePattern($hostnamePattern);
}
public function __clone()
@ -103,6 +106,18 @@ class Route implements \Serializable
return $this;
}
public function getHostnamePattern()
{
return $this->hostnamePattern;
}
public function setHostnamePattern($pattern)
{
$this->hostnamePattern = $pattern;
return $this;
}
/**
* Returns the options.
*

View File

@ -30,6 +30,7 @@ class RouteCollection implements \IteratorAggregate, \Countable
private $resources;
private $prefix;
private $parent;
private $hostnamePattern;
/**
* Constructor.
@ -41,6 +42,7 @@ class RouteCollection implements \IteratorAggregate, \Countable
$this->routes = array();
$this->resources = array();
$this->prefix = '';
$this->hostnamePattern = null;
}
public function __clone()
@ -188,12 +190,13 @@ class RouteCollection implements \IteratorAggregate, \Countable
* @param array $defaults An array of default values
* @param array $requirements An array of requirements
* @param array $options An array of options
* @param string $hostnamePattern Hostname pattern
*
* @throws \InvalidArgumentException When the RouteCollection already exists in the tree
*
* @api
*/
public function addCollection(RouteCollection $collection, $prefix = '', $defaults = array(), $requirements = array(), $options = array())
public function addCollection(RouteCollection $collection, $prefix = '', $defaults = array(), $requirements = array(), $options = array(), $hostnamePattern = null)
{
// prevent infinite loops by recursive referencing
$root = $this->getRoot();
@ -208,6 +211,12 @@ class RouteCollection implements \IteratorAggregate, \Countable
// the sub-collection must have the prefix of the parent (current instance) prepended because it does not
// necessarily already have it applied (depending on the order RouteCollections are added to each other)
$collection->addPrefix($this->getPrefix() . $prefix, $defaults, $requirements, $options);
// Allow child collection to have a different pattern
if (!$collection->getHostnamePattern()) {
$collection->setHostnamePattern($hostnamePattern);
}
$this->routes[] = $collection;
}
@ -341,4 +350,22 @@ class RouteCollection implements \IteratorAggregate, \Countable
return false;
}
public function getHostnamePattern()
{
return $this->hostnamePattern;
}
public function setHostnamePattern($pattern)
{
$this->hostnamePattern = $pattern;
foreach ($this->routes as $name => $route) {
// Allow individual routes to have a different pattern
if (!$route->getHostnamePattern()) {
$route->setHostnamePattern($pattern);
}
}
}
}