Permalink
Browse files

clean up addPrefix that was misused to add requirements, defaults and…

… options without adding a prefix at all.

so RouteCollection::addPrefix('', array('default' => 'value')) was used in addCollection() to add defaults to child routes, but not adding a prefix. this was just a hack. instead RouteCollection now offers a dedicated method to add such configs.
  • Loading branch information...
Tobion committed Nov 15, 2012
1 parent 0c6e145 commit baf6a9c640fd8a658beed90cd27b53e62b80d895
Showing with 23 additions and 8 deletions.
  1. +23 −8 src/Symfony/Component/Routing/RouteCollection.php
@@ -188,6 +188,7 @@ public function addCollection(RouteCollection $collection, $prefix = '', $defaul
// 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);
+ $collection->addConfigs($defaults, $requirements, $options);
if ('' !== $hostnamePattern) {
$collection->setHostnamePattern($hostnamePattern);
@@ -204,7 +205,7 @@ public function addCollection(RouteCollection $collection, $prefix = '', $defaul
}
/**
- * Adds a prefix to all routes in the current set.
+ * Adds a prefix to the path of all child routes.
*
* @param string $prefix An optional prefix to add before each pattern of the route collection
* @param array $defaults An array of default values
@@ -217,19 +218,15 @@ public function addPrefix($prefix, $defaults = array(), $requirements = array(),
{
$prefix = trim(trim($prefix), '/');
- if ('' === $prefix && empty($defaults) && empty($requirements) && empty($options)) {
+ if ('' === $prefix) {
return;
}
// a prefix must start with a single slash and must not end with a slash
- if ('' !== $prefix) {
- $this->prefix = '/' . $prefix . $this->prefix;
- }
+ $this->prefix = '/' . $prefix . $this->prefix;
foreach ($this->routes as $route) {
- if ('' !== $prefix) {
- $route->setPattern('/' . $prefix . $route->getPattern());
- }
+ $route->setPattern('/' . $prefix . $route->getPattern());
$route->addDefaults($defaults);
$route->addRequirements($requirements);
$route->addOptions($options);
@@ -258,6 +255,24 @@ public function setHostnamePattern($pattern)
}
}
+ /**
+ * Adds defaults, requirements and options to all routes.
+ *
+ * Any existing config under the same name in a route will be overridden.
+ *
+ * @param array $defaults An array of default values
+ * @param array $requirements An array of requirements
+ * @param array $options An array of options
+ */
+ public function addConfigs(array $defaults = array(), array $requirements = array(), array $options = array())
+ {
+ foreach ($this->routes as $route) {
+ $route->addDefaults($defaults);
+ $route->addRequirements($requirements);
+ $route->addOptions($options);
+ }
+ }
+
/**
* Returns an array of resources loaded to build this collection.
*

0 comments on commit baf6a9c

Please sign in to comment.