Skip to content

Commit

Permalink
switch from addConfigs() to dedicated addDefaults, addRequirements, a…
Browse files Browse the repository at this point in the history
…ddOptions
  • Loading branch information
Tobion committed Dec 7, 2012
1 parent b6d1e22 commit 8c1aa53
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 25 deletions.
12 changes: 7 additions & 5 deletions src/Symfony/Component/Routing/CHANGELOG.md
Expand Up @@ -37,13 +37,15 @@ CHANGELOG


* [DEPRECATION] The methods `RouteCollection::getParent()` and `RouteCollection::getRoot()` * [DEPRECATION] The methods `RouteCollection::getParent()` and `RouteCollection::getRoot()`
have been deprecated and will be removed in Symfony 2.3. have been deprecated and will be removed in Symfony 2.3.
* [BC BREAK] Misusing the `RouteCollection::addPrefix` method to add requirements or options * [BC BREAK] Misusing the `RouteCollection::addPrefix` method to add defaults, requirements
without adding a prefix is not supported anymore. There is now a dedicated method named or options without adding a prefix is not supported anymore. So if you called `addPrefix`
`setConfigs()` for this. So if you used something like `addPrefix('', array(...), array(...))` with an empty prefix or `/` only (both have no relevance), like
you need to call `setConfigs(array(...), array(...))` instead. `addPrefix('', $defaultsArray, $requirementsArray, $optionsArray)`
you need to use the new dedicated methods `addDefaults($defaultsArray)`,
`addRequirements($requirementsArray)` or `addOptions($optionsArray)` instead.
* [DEPRECATION] The `$options` parameter to `RouteCollection::addPrefix()` has been deprecated * [DEPRECATION] The `$options` parameter to `RouteCollection::addPrefix()` has been deprecated
because adding options has nothing to do with adding a path prefix. If you want to add options because adding options has nothing to do with adding a path prefix. If you want to add options
to all child routes of a RouteCollection, you can use `setConfigs()`. to all child routes of a RouteCollection, you can use `addOptions()`.
* [DEPRECATION] `RouteCollection::addCollection(RouteCollection $collection)` should now only be * [DEPRECATION] `RouteCollection::addCollection(RouteCollection $collection)` should now only be
used with a single parameter. The other params `$prefix`, `$default`, `$requirements` and `$options` used with a single parameter. The other params `$prefix`, `$default`, `$requirements` and `$options`
will still work, but have been deprecated. The `addPrefix` method should be used for this will still work, but have been deprecated. The `addPrefix` method should be used for this
Expand Down
7 changes: 5 additions & 2 deletions src/Symfony/Component/Routing/Loader/XmlFileLoader.php
Expand Up @@ -107,12 +107,15 @@ protected function parseNode(RouteCollection $collection, \DOMElement $node, $pa
$this->setCurrentDir(dirname($path)); $this->setCurrentDir(dirname($path));


$subCollection = $this->import($resource, ('' !== $type ? $type : null), false, $file); $subCollection = $this->import($resource, ('' !== $type ? $type : null), false, $file);
/* @var RouteCollection $subCollection */ /* @var $subCollection RouteCollection */
$subCollection->addPrefix($prefix); $subCollection->addPrefix($prefix);
if (null !== $hostnamePattern) { if (null !== $hostnamePattern) {
$subCollection->setHostnamePattern($hostnamePattern); $subCollection->setHostnamePattern($hostnamePattern);
} }
$subCollection->addConfigs($defaults, $requirements, $options); $subCollection->addDefaults($defaults);
$subCollection->addRequirements($requirements);
$subCollection->addOptions($options);

$collection->addCollection($subCollection); $collection->addCollection($subCollection);
break; break;
default: default:
Expand Down
7 changes: 5 additions & 2 deletions src/Symfony/Component/Routing/Loader/YamlFileLoader.php
Expand Up @@ -75,12 +75,15 @@ public function load($file, $type = null)
$this->setCurrentDir(dirname($path)); $this->setCurrentDir(dirname($path));


$subCollection = $this->import($config['resource'], $type, false, $file); $subCollection = $this->import($config['resource'], $type, false, $file);
/* @var RouteCollection $subCollection */ /* @var $subCollection RouteCollection */
$subCollection->addPrefix($prefix); $subCollection->addPrefix($prefix);
if (null !== $hostnamePattern) { if (null !== $hostnamePattern) {
$subCollection->setHostnamePattern($hostnamePattern); $subCollection->setHostnamePattern($hostnamePattern);
} }
$subCollection->addConfigs($defaults, $requirements, $options); $subCollection->addDefaults($defaults);
$subCollection->addRequirements($requirements);
$subCollection->addOptions($options);

$collection->addCollection($subCollection); $collection->addCollection($subCollection);
} else { } else {
$this->parseRoute($collection, $name, $config, $path); $this->parseRoute($collection, $name, $config, $path);
Expand Down
63 changes: 47 additions & 16 deletions src/Symfony/Component/Routing/RouteCollection.php
Expand Up @@ -184,13 +184,14 @@ public function addCollection(RouteCollection $collection)
$numargs = func_num_args(); $numargs = func_num_args();
if ($numargs > 1) { if ($numargs > 1) {
$collection->addPrefix($this->getPrefix() . func_get_arg(1)); $collection->addPrefix($this->getPrefix() . func_get_arg(1));

if ($numargs > 2) { if ($numargs > 2) {
$defaults = func_get_arg(2); $collection->addDefaults(func_get_arg(2));
$requirements = $numargs > 3 ? func_get_arg(3) : array(); if ($numargs > 3) {
$options = $numargs > 4 ? func_get_arg(4) : array(); $collection->addRequirements(func_get_arg(3));

if ($numargs > 4) {
$collection->addConfigs($defaults, $requirements, $options); $collection->addOptions(func_get_arg(4));
}
}
} }
} else { } else {
// the sub-collection must have the prefix of the parent (current instance) prepended because it does not // the sub-collection must have the prefix of the parent (current instance) prepended because it does not
Expand Down Expand Up @@ -266,20 +267,50 @@ public function setHostnamePattern($pattern, array $defaults = array(), array $r
} }


/** /**
* Adds defaults, requirements and options to all routes. * Adds defaults to all routes.
* *
* Any existing config under the same name in a route will be overridden. * An existing default value under the same name in a route will be overridden.
* *
* @param array $defaults An array of default values * @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()) public function addDefaults(array $defaults)
{ {
foreach ($this->routes as $route) { if ($defaults) {
$route->addDefaults($defaults); foreach ($this->routes as $route) {
$route->addRequirements($requirements); $route->addDefaults($defaults);
$route->addOptions($options); }
}
}

/**
* Adds requirements to all routes.
*
* An existing requirement under the same name in a route will be overridden.
*
* @param array $requirements An array of requirements
*/
public function addRequirements(array $requirements)
{
if ($requirements) {
foreach ($this->routes as $route) {
$route->addRequirements($requirements);
}
}
}

/**
* Adds options to all routes.
*
* An existing option value under the same name in a route will be overridden.
*
* @param array $options An array of options
*/
public function addOptions(array $options)
{
if ($options) {
foreach ($this->routes as $route) {
$route->addOptions($options);
}
} }
} }


Expand Down

0 comments on commit 8c1aa53

Please sign in to comment.