From 65e220723681fea4da2b17789772bf0abcfa1276 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sun, 4 Jun 2017 22:50:39 -0400 Subject: [PATCH] Deprecate RouteCollection::extensions() Another combined get/set method. Refs #9978 --- src/Routing/RouteBuilder.php | 2 +- src/Routing/RouteCollection.php | 35 ++++++++++++++++--- src/Routing/Router.php | 2 +- .../TestCase/Routing/RouteCollectionTest.php | 22 +++++++++++- 4 files changed, 53 insertions(+), 8 deletions(-) diff --git a/src/Routing/RouteBuilder.php b/src/Routing/RouteBuilder.php index 09527308274..bbda06c99c0 100644 --- a/src/Routing/RouteBuilder.php +++ b/src/Routing/RouteBuilder.php @@ -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: diff --git a/src/Routing/RouteCollection.php b/src/Routing/RouteCollection.php index d9d72f8a853..3ae02b0e889 100644 --- a/src/Routing/RouteCollection.php +++ b/src/Routing/RouteCollection.php @@ -122,7 +122,7 @@ public function add(Route $route, array $options = []) $extensions = $route->getExtensions(); if (count($extensions) > 0) { - $this->extensions($extensions); + $this->setExtensions($extensions); } } @@ -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. * diff --git a/src/Routing/Router.php b/src/Routing/Router.php index 4e6296b6107..c8dbb8bbc3c 100644 --- a/src/Routing/Router.php +++ b/src/Routing/Router.php @@ -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) { diff --git a/tests/TestCase/Routing/RouteCollectionTest.php b/tests/TestCase/Routing/RouteCollectionTest.php index 85f6c654f26..204657677a4 100644 --- a/tests/TestCase/Routing/RouteCollectionTest.php +++ b/tests/TestCase/Routing/RouteCollectionTest.php @@ -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() { @@ -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. *