8000 [Routing] added hostname matching support to Route and RouteCollection · alexpott/symfony@43e9d75 · GitHub
[go: up one dir, main page]

Skip to content

Commit 43e9d75

Browse files
committed
[Routing] added hostname matching support to Route and RouteCollection
1 parent 50c2974 commit 43e9d75

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

src/Symfony/Component/Routing/Route.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class Route
2525
private $requirements;
2626
private $options;
2727
private $compiled;
28+
private $hostnamePattern;
2829

2930
static private $compilers = array();
3031

@@ -39,15 +40,17 @@ class Route
3940
* @param array $defaults An array of default parameter values
4041
* @param array $requirements An array of requirements for parameters (regexes)
4142
* @param array $options An array of options
43+
* @param string $hostname The hostname pattern to match
4244
*
4345
* @api
4446
*/
45-
public function __construct($pattern, array $defaults = array(), array $requirements = array(), array $options = array())
47+
public function __construct($pattern, array $defaults = array(), array $requirements = array(), array $options = array(), $hostnamePattern = null)
4648
{
4749
$this->setPattern($pattern);
4850
$this->setDefaults($defaults);
4951
$this->setRequirements($requirements);
5052
$this->setOptions($options);
53+
$this->setHostnamePattern($hostnamePattern);
5154
}
5255

5356
public function __clone()
@@ -88,6 +91,18 @@ public function setPattern($pattern)
8891
return $this;
8992
}
9093

94+
public function getHostnamePattern()
95+
{
96+
return $this->hostnamePattern;
97+
}
98+
99+
public function setHostnamePattern($pattern)
100+
{
101+
$this->hostnamePattern = $pattern;
102+
103+
return $this;
104+
}
105+
91106
/**
92107
* Returns the options.
93108
*

src/Symfony/Component/Routing/RouteCollection.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class RouteCollection implements \IteratorAggregate
3030
private $resources;
3131
private $prefix;
3232
private $parent;
33+
private $hostnamePattern;
3334

3435
/**
3536
* Constructor.
@@ -41,6 +42,7 @@ public function __construct()
4142
$this->routes = array();
4243
$this->resources = array();
4344
$this->prefix = '';
45+
$this->hostnamePattern = null;
4446
}
4547

4648
public function __clone()
@@ -173,12 +175,13 @@ public function remove($name)
173175
* @param array $defaults An array of default values
174176
* @param array $requirements An array of requirements
175177
* @param array $options An array of options
178+
* @param string $hostnamePattern Hostname pattern
176179
*
177180
* @throws \InvalidArgumentException When the RouteCollection already exists in the tree
178181
*
179182
* @api
180183
*/
181-
public function addCollection(RouteCollection $collection, $prefix = '', $defaults = array(), $requirements = array(), $options = array())
184+
public function addCollection(RouteCollection $collection, $prefix = '', $defaults = array(), $requirements = array(), $options = array(), $hostnamePattern = null)
182185
{
183186
// prevent infinite loops by recursive referencing
184187
$root = $this->getRoot();
@@ -193,6 +196,12 @@ public function addCollection(RouteCollection $collection, $prefix = '', $defaul
193196
// the sub-collection must have the prefix of the parent (current instance) prepended because it does not
194197
// necessarily already have it applied (depending on the order RouteCollections are added to each other)
195198
$collection->addPrefix($this->getPrefix() . $prefix, $defaults, $requirements, $options);
199+
200+
// Allow child collection to have a different pattern
201+
if (!$collection->getHostnamePattern()) {
202+
$collection->setHostnamePattern($hostnamePattern);
203+
}
204+
196205
$this->routes[] = $collection;
197206
}
198207

@@ -326,4 +335,22 @@ private function hasCollection(RouteCollection $collection)
326335

327336
return false;
328337
}
338+
339+
public function getHostnamePattern()
340+
{
341+
return $this->hostnamePattern;
342+
}
343+
344+
public function setHostnamePattern($pattern)
345+
{
346+
$this->hostnamePattern = $pattern;
347+
348+
foreach ($this->routes as $name => $route) {
349+
// Allow individual routes to have a different pattern
350+
if (!$route->getHostnamePattern()) {
351+
$route->setHostnamePattern($pattern);
352+
}
353+
}
354+
}
355+
329356
}

0 commit comments

Comments
 (0)
0