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

Skip to content

Commit add3658

Browse files
committed
[Routing] added hostname matching support to Route and RouteCollection
1 parent 23feb37 commit add3658

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

src/Symfony/Component/Routing/Route.php

+16-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class Route implements \Serializable
2525
private $requirements;
2626
private $options;
2727
private $compiled;
28+
private $hostnamePattern;
2829

2930
private static $compilers = array();
3031

@@ -39,15 +40,17 @@ class Route implements \Serializable
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()
@@ -103,6 +106,18 @@ public function setPattern($pattern)
103106
return $this;
104107
}
105108

109+
public function getHostnamePattern()
110+
{
111+
return $this->hostnamePattern;
112+
}
113+
114+
public function setHostnamePattern($pattern)
115+
{
116+
$this->hostnamePattern = $pattern;
117< 10000 code class="diff-text syntax-highlighted-line addition">+
118+
return $this;
119+
}
120+
106121
/**
107122
* Returns the options.
108123
*

src/Symfony/Component/Routing/RouteCollection.php

+28-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class RouteCollection implements \IteratorAggregate, \Countable
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()
@@ -188,12 +190,13 @@ public function remove($name)
188190
* @param array $defaults An array of default values
189191
* @param array $requirements An array of requirements
190192
* @param array $options An array of options
193+
* @param string $hostnamePattern Hostname pattern
191194
*
192195
* @throws \InvalidArgumentException When the RouteCollection already exists in the tree
193196
*
194197
* @api
195198
*/
196-
public function addCollection(RouteCollection $collection, $prefix = '', $defaults = array(), $requirements = array(), $options = array())
199+ 8000
public function addCollection(RouteCollection $collection, $prefix = '', $defaults = array(), $requirements = array(), $options = array(), $hostnamePattern = null)
197200
{
198201
// prevent infinite loops by recursive referencing
199202
$root = $this->getRoot();
@@ -208,6 +211,12 @@ public function addCollection(RouteCollection $collection, $prefix = '', $defaul
208211
// the sub-collection must have the prefix of the parent (current instance) prepended because it does not
209212
// necessarily already have it applied (depending on the order RouteCollections are added to each other)
210213
$collection->addPrefix($this->getPrefix() . $prefix, $defaults, $requirements, $options);
214+
215+
// Allow child collection to have a different pattern
216+
if (!$collection->getHostnamePattern()) {
217+
$collection->setHostnamePattern($hostnamePattern);
218+
}
219+
211220
$this->routes[] = $collection;
212221
}
213222

@@ -341,4 +350,22 @@ private function hasCollection(RouteCollection $collection)
341350

342351
return false;
343352
}
353+
354+
public function getHostnamePattern()
355+
{
356+
return $this->hostnamePattern;
357+
}
358+
359+
public function setHostnamePattern($pattern)
360+
{
361+
$this->hostnamePattern = $pattern;
362+
363+
foreach ($this->routes as $name => $route) {
364+
// Allow individual routes to have a different pattern
365+
if (!$route->getHostnamePattern()) {
366+
$route->setHostnamePattern($pattern);
367+
}
368+
}
369+
}
370+
344371
}

0 commit comments

Comments
 (0)
0