Skip to content

Commit

Permalink
Deprecate RouteCollection::extensions()
Browse files Browse the repository at this point in the history
Another combined get/set method.

Refs #9978
  • Loading branch information
markstory committed Jun 5, 2017
1 parent b342625 commit 65e2207
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Routing/RouteBuilder.php
Expand Up @@ -574,7 +574,7 @@ protected function _methodRoute($method, $template, $target, $name)
* reverse routing lookups. If undefined a name will be generated for each
* connected route.
* - `_ext` is an array of filename extensions that will be parsed out of the url if present.
* See {@link \Cake\Routing\RouteCollection::extensions()}.
* See {@link \Cake\Routing\RouteCollection::setExtensions()}.
* - `_method` Only match requests with specific HTTP verbs.
*
* Example of using the `_method` condition:
Expand Down
35 changes: 30 additions & 5 deletions src/Routing/RouteCollection.php
Expand Up @@ -122,7 +122,7 @@ public function add(Route $route, array $options = [])

$extensions = $route->getExtensions();
if (count($extensions) > 0) {
$this->extensions($extensions);
$this->setExtensions($extensions);
}
}

Expand Down Expand Up @@ -362,24 +362,49 @@ public function named()
* @param bool $merge Whether to merge with or override existing extensions.
* Defaults to `true`.
* @return array The valid extensions.
* @deprecated 3.5.0 Use getExtensions()/setExtensions() instead.
*/
public function extensions($extensions = null, $merge = true)
{
if ($extensions === null) {
return $this->_extensions;
if ($extensions !== null) {
$this->setExtensions((array)$extensions, $merge);
}

$extensions = (array)$extensions;
return $this->getExtensions();
}

/**
* Get the extensions that can be handled.
*
* @return array The valid extensions.
*/
public function getExtensions()
{
return $this->_extensions;
}

/**
* Set the extensions that the route collection can handle.
*
* @param array $extensions The list of extensions to set.
* @param bool $merge Whether to merge with or override existing extensions.
* Defaults to `true`.
* @return $this
*/
public function setExtensions(array $extensions, $merge = true)
{
if ($merge) {
$extensions = array_unique(array_merge(
$this->_extensions,
$extensions
));
}
$this->_extensions = $extensions;

return $this->_extensions = $extensions;
return $this;
}


/**
* Register a middleware with the RouteCollection.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Routing/Router.php
Expand Up @@ -844,7 +844,7 @@ public static function extensions($extensions = null, $merge = true)
static::_loadRoutes();
}

return array_unique(array_merge(static::$_defaultExtensions, $collection->extensions()));
return array_unique(array_merge(static::$_defaultExtensions, $collection->getExtensions()));
}
$extensions = (array)$extensions;
if ($merge) {
Expand Down
22 changes: 21 additions & 1 deletion tests/TestCase/Routing/RouteCollectionTest.php
Expand Up @@ -593,9 +593,10 @@ public function testAddingDuplicateNamedRoutes()
}

/**
* Test basic get/set of extensions.
* Test get/set combined method.
*
* @return void
* @deprecated 3.5.0
*/
public function testExtensions()
{
Expand All @@ -611,6 +612,25 @@ public function testExtensions()
$this->assertEquals(['csv'], $this->collection->extensions());
}

/**
* Test basic setExtension and its getter.
*
* @return void
*/
public function testSetExtensions()
{
$this->assertEquals([], $this->collection->getExtensions());

$this->collection->setExtensions(['json']);
$this->assertEquals(['json'], $this->collection->getExtensions());

$this->collection->setExtensions(['rss', 'xml']);
$this->assertEquals(['json', 'rss', 'xml'], $this->collection->getExtensions());

$this->collection->setExtensions(['csv'], false);
$this->assertEquals(['csv'], $this->collection->getExtensions());
}

/**
* String methods are not acceptable.
*
Expand Down

0 comments on commit 65e2207

Please sign in to comment.