From 40acf4071f898c18d12471497859cccb57f21459 Mon Sep 17 00:00:00 2001 From: ADmad Date: Sun, 7 Sep 2014 12:29:05 +0530 Subject: [PATCH] Remove Router::parseExtensions(). Router::extensions() can now be used to add/set extensions. --- .../Component/RequestHandlerComponent.php | 6 +-- src/Routing/Router.php | 40 +++++++------------ tests/TestCase/Routing/RouterTest.php | 30 +++++--------- tests/test_app/config/routes.php | 2 +- 4 files changed, 27 insertions(+), 51 deletions(-) diff --git a/src/Controller/Component/RequestHandlerComponent.php b/src/Controller/Component/RequestHandlerComponent.php index d0dd94902d3..b60eb026c4c 100644 --- a/src/Controller/Component/RequestHandlerComponent.php +++ b/src/Controller/Component/RequestHandlerComponent.php @@ -63,7 +63,7 @@ class RequestHandlerComponent extends Component { * Contains the file extension parsed out by the Router * * @var string - * @see Router::parseExtensions() + * @see Router::extensions() */ public $ext = null; @@ -149,7 +149,7 @@ public function implementedEvents() { * * @param Event $event The initialize event that was fired. * @return void - * @see Router::parseExtensions() + * @see Router::extensions() */ public function initialize(Event $event) { if (isset($this->request->params['_ext'])) { @@ -207,7 +207,7 @@ protected function _setExtension() { * The startup method of the RequestHandler enables several automatic behaviors * related to the detection of certain properties of the HTTP request, including: * - * - If Router::parseExtensions() is enabled, the layout and template type are + * - If Router::extensions() is enabled, the layout and template type are * switched based on the parsed extension or Accept-Type header. For example, if `controller/action.xml` * is requested, the view path becomes `app/View/Controller/xml/action.ctp`. Also if * `controller/action` is requested with `Accept-Type: application/xml` in the headers diff --git a/src/Routing/Router.php b/src/Routing/Router.php index 7732c9b1a65..03d57353eff 100644 --- a/src/Routing/Router.php +++ b/src/Routing/Router.php @@ -709,7 +709,7 @@ public static function normalize($url = '/') { } /** - * Set/add valid extensions. Instructs the router to parse out file extensions + * Get/Set valid extensions. Instructs the router to parse out file extensions * from the URL. For example, http://example.com/posts.rss would yield a file * extension of "rss". The file extension itself is made available in the * controller as `$this->params['_ext']`, and is used by the RequestHandler @@ -718,38 +718,26 @@ public static function normalize($url = '/') { * layouts and helpers requires that the chosen extension has a defined mime type * in `Cake\Network\Response`. * - * An array of valid extension can be passed to this method. If called without - * any parameters it will return current list of set extensions. + * A string or an array of valid extension can be passed to this method. + * If called without any parameters it will return current list of set extensions. * - * @param array|string $extensions List of extensions to be added as valid extension - * @param bool $merge Default true will merge extensions. Set to false to override - * current extensions - * @return array + * @param array|string $extensions List of extensions to be added. + * @param array $options Valid options: + * - `merge` - Default true will merge extensions. Set to false to override + * current extensions + * @return array Array of extensions Router is configured to parse. */ - public static function parseExtensions($extensions = null, $merge = true) { + public static function extensions($extensions = null, array $options = []) { $collection = static::$_collection; if ($extensions === null) { + if (!static::$initialized) { + static::_loadRoutes(); + } return $collection->extensions(); } - $extensions = (array)$extensions; - if ($merge) { - $extensions = array_merge($collection->extensions(), $extensions); - } - return $collection->extensions($extensions); - } -/** - * Get the list of extensions that can be parsed by Router. - * - * To add / update extensions use `Router::parseExtensions()` - * - * @return array Array of extensions Router is configured to parse. - */ - public static function extensions() { - if (!static::$initialized) { - static::_loadRoutes(); - } - return static::$_collection->extensions(); + $options += ['merge' => true]; + return $collection->extensions($extensions, $options); } /** diff --git a/tests/TestCase/Routing/RouterTest.php b/tests/TestCase/Routing/RouterTest.php index a12da76a0e6..7914844b85b 100644 --- a/tests/TestCase/Routing/RouterTest.php +++ b/tests/TestCase/Routing/RouterTest.php @@ -277,7 +277,7 @@ public function testMapResourcesWithPrefix() { * @return void */ public function testMapResourcesWithExtension() { - Router::parseExtensions(['json', 'xml'], false); + Router::extensions(['json', 'xml'], ['merge' => false]); Router::mapResources('Posts', ['_ext' => 'json']); $_SERVER['REQUEST_METHOD'] = 'GET'; @@ -811,7 +811,7 @@ public function testUrlGenerationWithPrefix() { Router::connect('/reset/*', array('admin' => true, 'controller' => 'users', 'action' => 'reset')); Router::connect('/tests', array('controller' => 'tests', 'action' => 'index')); Router::connect('/admin/:controller/:action/*', array('prefix' => 'admin')); - Router::parseExtensions('rss', false); + Router::extensions('rss', ['merge' => false]); $request = new Request(); $request->addParams(array( @@ -1524,24 +1524,13 @@ public function parseReverseSymmetryData() { ); } -/** - * testParseExtensions method - * - * @return void - */ - public function testParseExtensions() { - Router::extensions(); - Router::parseExtensions('rss', false); - $this->assertContains('rss', Router::extensions()); - } - /** * testSetExtensions method * * @return void */ public function testSetExtensions() { - Router::parseExtensions('rss', false); + Router::extensions('rss', ['merge' => false]); $this->assertContains('rss', Router::extensions()); $this->_connectDefaultRoutes(); @@ -1552,7 +1541,7 @@ public function testSetExtensions() { $result = Router::parse('/posts.xml'); $this->assertFalse(isset($result['_ext'])); - Router::parseExtensions(array('xml')); + Router::extensions(array('xml')); } /** @@ -1571,7 +1560,6 @@ public function testExtensionsWithScopedRoutes() { }); }); - // json is added by routes in test_app/config/routes.php $this->assertEquals(['rss', 'xml', 'json'], Router::extensions()); } @@ -1581,7 +1569,7 @@ public function testExtensionsWithScopedRoutes() { * @return void */ public function testExtensionParsing() { - Router::parseExtensions('rss', false); + Router::extensions('rss', ['merge' => false]); $this->_connectDefaultRoutes(); $result = Router::parse('/posts.rss'); @@ -1609,7 +1597,7 @@ public function testExtensionParsing() { $this->assertEquals($expected, $result); Router::reload(); - Router::parseExtensions(['rss', 'xml'], false); + Router::extensions(['rss', 'xml'], ['merge' => false]); $this->_connectDefaultRoutes(); $result = Router::parse('/posts.xml'); @@ -1651,7 +1639,7 @@ public function testExtensionParsing() { $this->assertEquals($expected, $result); Router::reload(); - Router::parseExtensions('rss', false); + Router::extensions('rss', ['merge' => false]); Router::connect('/controller/action', array('controller' => 'controller', 'action' => 'action', '_ext' => 'rss')); $result = Router::parse('/controller/action'); $expected = array( @@ -1980,7 +1968,7 @@ public function testParsingWithTrailingPeriod() { public function testParsingWithTrailingPeriodAndParseExtensions() { Router::reload(); Router::connect('/:controller/:action/*'); - Router::parseExtensions('json', false); + Router::extensions('json', ['merge' => false]); $result = Router::parse('/posts/view/something.'); $this->assertEquals('something.', $result['pass'][0], 'Period was chopped off %s'); @@ -2369,7 +2357,7 @@ public function testReverse() { */ public function testReverseWithExtension() { Router::connect('/:controller/:action/*'); - Router::parseExtensions('json', false); + Router::extensions('json', ['merge' => false]); $request = new Request('/posts/view/1.json'); $request->addParams(array( diff --git a/tests/test_app/config/routes.php b/tests/test_app/config/routes.php index a966f3a19e1..0863a91eb19 100644 --- a/tests/test_app/config/routes.php +++ b/tests/test_app/config/routes.php @@ -14,7 +14,7 @@ */ use Cake\Routing\Router; -Router::parseExtensions('json'); +Router::extensions('json'); Router::scope('/', function($routes) { $routes->connect('/', ['controller' => 'pages', 'action' => 'display', 'home']); $routes->connect('/some_alias', array('controller' => 'tests_apps', 'action' => 'some_method'));