Skip to content

Commit

Permalink
Removed Router::parseExtensions().
Browse files Browse the repository at this point in the history
Just use Router::setExtensions() instead. setExtensions() now also takes
a single extension as string for first param.
  • Loading branch information
ADmad committed Apr 28, 2014
1 parent 61759ad commit 48df5ba
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 55 deletions.
47 changes: 18 additions & 29 deletions src/Routing/Router.php
Expand Up @@ -944,39 +944,28 @@ public static function normalize($url = '/') {
}

/**
* Instructs the router to parse out file extensions from the URL. For example,
* http://example.com/posts.rss would yield an file extension of "rss".
* The file extension itself is made available in the controller as
* `$this->params['_ext']`, and is used by the RequestHandler component to
* automatically switch to alternate layouts and templates, and load helpers
* corresponding to the given content, i.e. RssHelper. Switching layouts and helpers
* requires that the chosen extension has a defined mime type in `Cake\Network\Response`
*
* A list of valid extension can be passed to this method, i.e. Router::parseExtensions('rss', 'xml');
* If no parameters are given, anything after the first . (dot) after the last / in the URL will be
* parsed, excluding querystring parameters (i.e. ?q=...).
*
* @return void
* @see RequestHandler::startup()
*/
public static function parseExtensions() {
if (func_num_args() > 0) {
static::setExtensions(func_get_args(), false);
}
}

/**
* Set/add valid extensions.
* To have the extensions parsed you still need to call `Router::parseExtensions()`
*
* @param array $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
* Set/add valid extensions. Instructs the router to parse out file extensions
* from the URL. For example, http://example.com/posts.rss would yield an file
* extension of "rss". The file extension itself is made available in the
* controller as `$this->params['_ext']`, and is used by the RequestHandler
* component to automatically switch to alternate layouts and templates, and
* load helpers corresponding to the given content, i.e. RssHelper. Switching
* 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.
*
* @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
*/
public static function setExtensions($extensions, $merge = true) {
if (!is_array($extensions)) {
public static function setExtensions($extensions = null, $merge = true) {
if ($extensions === null) {
return static::$_validExtensions;
}
$extensions = (array)$extensions;
if ($merge) {
$extensions = array_merge(static::$_validExtensions, $extensions);
}
Expand Down
32 changes: 16 additions & 16 deletions tests/TestCase/Controller/Component/RequestHandlerComponentTest.php
Expand Up @@ -83,7 +83,7 @@ public function tearDown() {
if (!headers_sent()) {
header('Content-type: text/html'); //reset content type.
}
call_user_func_array('Cake\Routing\Router::parseExtensions', $this->_extensions);
call_user_func_array('Cake\Routing\Router::setExtensions', [$this->_extensions, false]);
}

/**
Expand Down Expand Up @@ -122,7 +122,7 @@ public function testInitializeCallback() {
public function testInitializeContentTypeSettingExt() {
$event = new Event('Controller.initialize', $this->Controller);
$_SERVER['HTTP_ACCEPT'] = 'application/json';
Router::parseExtensions('json');
Router::setExtensions('json', false);

$this->assertNull($this->RequestHandler->ext);

Expand All @@ -139,7 +139,7 @@ public function testInitializeContentTypeWithjQueryAccept() {
$_SERVER['HTTP_ACCEPT'] = 'application/json, application/javascript, */*; q=0.01';
$event = new Event('Controller.initialize', $this->Controller);
$this->assertNull($this->RequestHandler->ext);
Router::parseExtensions('json');
Router::setExtensions('json', false);

$this->RequestHandler->initialize($event);
$this->assertEquals('json', $this->RequestHandler->ext);
Expand All @@ -154,7 +154,7 @@ public function testInitializeContentTypeWithjQueryTextPlainAccept() {
$_SERVER['HTTP_ACCEPT'] = 'text/plain, */*; q=0.01';
$event = new Event('Controller.initialize', $this->Controller);
$this->assertNull($this->RequestHandler->ext);
Router::parseExtensions('csv');
Router::setExtensions('csv', false);

$this->RequestHandler->initialize($event);
$this->assertNull($this->RequestHandler->ext);
Expand All @@ -170,7 +170,7 @@ public function testInitializeContentTypeWithjQueryAcceptAndMultiplesExtensions(
$_SERVER['HTTP_ACCEPT'] = 'application/json, application/javascript, */*; q=0.01';
$event = new Event('Controller.initialize', $this->Controller);
$this->assertNull($this->RequestHandler->ext);
Router::parseExtensions('rss', 'json');
Router::setExtensions(['rss', 'json'], false);

$this->RequestHandler->initialize($event);
$this->assertEquals('json', $this->RequestHandler->ext);
Expand All @@ -185,7 +185,7 @@ public function testInitializeNoContentTypeWithSingleAccept() {
$_SERVER['HTTP_ACCEPT'] = 'application/json, text/html, */*; q=0.01';
$event = new Event('Controller.initialize', $this->Controller);
$this->assertNull($this->RequestHandler->ext);
Router::parseExtensions('json');
Router::setExtensions('json', false);

$this->RequestHandler->initialize($event);
$this->assertNull($this->RequestHandler->ext);
Expand All @@ -203,7 +203,7 @@ public function testInitializeNoContentTypeWithMultipleAcceptedTypes() {
$_SERVER['HTTP_ACCEPT'] = 'application/json, application/javascript, application/xml, */*; q=0.01';
$event = new Event('Controller.initialize', $this->Controller);
$this->assertNull($this->RequestHandler->ext);
Router::parseExtensions('xml', 'json');
Router::setExtensions(['xml', 'json'], false);

$this->RequestHandler->initialize($event);
$this->assertEquals('xml', $this->RequestHandler->ext);
Expand All @@ -224,7 +224,7 @@ public function testInitializeContentTypeWithMultipleAcceptedTypes() {
$_SERVER['HTTP_ACCEPT'] = 'text/csv;q=1.0, application/json;q=0.8, application/xml;q=0.7';
$event = new Event('Controller.initialize', $this->Controller);
$this->assertNull($this->RequestHandler->ext);
Router::parseExtensions('xml', 'json');
Router::setExtensions(['xml', 'json'], false);

$this->RequestHandler->initialize($event);
$this->assertEquals('json', $this->RequestHandler->ext);
Expand All @@ -239,7 +239,7 @@ public function testInitializeAmbiguousAndroidAccepts() {
$_SERVER['HTTP_ACCEPT'] = 'application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5';
$event = new Event('Controller.initialize', $this->Controller);
$this->assertNull($this->RequestHandler->ext);
Router::parseExtensions('html', 'xml');
Router::setExtensions(['html', 'xml'], false);

$this->RequestHandler->initialize($event);
$this->assertNull($this->RequestHandler->ext);
Expand All @@ -252,7 +252,7 @@ public function testInitializeAmbiguousAndroidAccepts() {
*/
public function testInititalizeFirefoxHeaderNotXml() {
$_SERVER['HTTP_ACCEPT'] = 'text/html,application/xhtml+xml,application/xml;image/png,image/jpeg,image/*;q=0.9,*/*;q=0.8';
Router::parseExtensions('xml', 'json');
Router::setExtensions(['xml', 'json'], false);

$event = new Event('Controller.initialize', $this->Controller);
$this->RequestHandler->initialize($event);
Expand All @@ -268,7 +268,7 @@ public function testInitializeContentTypeAndExtensionMismatch() {
$event = new Event('Controller.initialize', $this->Controller);
$this->assertNull($this->RequestHandler->ext);
$extensions = Router::extensions();
Router::parseExtensions('xml');
Router::setExtensions('xml', false);

$this->Controller->request = $this->getMock('Cake\Network\Request', ['accepts']);
$this->Controller->request->expects($this->any())
Expand All @@ -278,7 +278,7 @@ public function testInitializeContentTypeAndExtensionMismatch() {
$this->RequestHandler->initialize($event);
$this->assertNull($this->RequestHandler->ext);

call_user_func_array(array('Cake\Routing\Router', 'parseExtensions'), $extensions);
call_user_func_array(array('Cake\Routing\Router', 'setExtensions'), [$extensions, false]);
}

/**
Expand Down Expand Up @@ -353,7 +353,7 @@ public function testAutoAjaxLayout() {
* test custom JsonView class is loaded and correct.
*/
public function testJsonViewLoaded() {
Router::parseExtensions('json', 'xml', 'ajax');
Router::setExtensions(['json', 'xml', 'ajax'], false);
$this->Controller->request->params['_ext'] = 'json';
$event = new Event('Controller.startup', $this->Controller);
$this->RequestHandler->initialize($event);
Expand All @@ -368,7 +368,7 @@ public function testJsonViewLoaded() {
* test custom XmlView class is loaded and correct.
*/
public function testXmlViewLoaded() {
Router::parseExtensions('json', 'xml', 'ajax');
Router::setExtensions(['json', 'xml', 'ajax'], false);
$this->Controller->request->params['_ext'] = 'xml';
$event = new Event('Controller.startup', $this->Controller);
$this->RequestHandler->initialize($event);
Expand All @@ -383,7 +383,7 @@ public function testXmlViewLoaded() {
* test custom AjaxView class is loaded and correct.
*/
public function testAjaxViewLoaded() {
Router::parseExtensions('json', 'xml', 'ajax');
Router::setExtensions(['json', 'xml', 'ajax'], false);
$this->Controller->request->params['_ext'] = 'ajax';
$event = new Event('Controller.startup', $this->Controller);
$this->RequestHandler->initialize($event);
Expand All @@ -397,7 +397,7 @@ public function testAjaxViewLoaded() {
* test configured extension but no view class set.
*/
public function testNoViewClassExtension() {
Router::parseExtensions('json', 'xml', 'ajax', 'csv');
Router::setExtensions(['json', 'xml', 'ajax', 'csv'], false);
$this->Controller->request->params['_ext'] = 'csv';
$event = new Event('Controller.startup', $this->Controller);
$this->RequestHandler->initialize($event);
Expand Down
18 changes: 9 additions & 9 deletions tests/TestCase/Routing/RouterTest.php
Expand Up @@ -292,7 +292,7 @@ public function testMapResourcesWithExtension() {
$this->assertEquals(['posts'], $resources);

$_SERVER['REQUEST_METHOD'] = 'GET';
Router::parseExtensions('json', 'xml');
Router::setExtensions(['json', 'xml'], false);

$expected = array(
'plugin' => null,
Expand Down Expand Up @@ -839,7 +839,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');
Router::setExtensions('rss', false);

$request = new Request();
$request->addParams(array(
Expand Down Expand Up @@ -1630,7 +1630,7 @@ public function testPrefixRoutingAndPlugins() {
*/
public function testParseExtensions() {
Router::extensions();
Router::parseExtensions('rss');
Router::setExtensions('rss', false);
$this->assertContains('rss', Router::extensions());
}

Expand All @@ -1641,7 +1641,7 @@ public function testParseExtensions() {
*/
public function testSetExtensions() {
Router::extensions();
Router::setExtensions(array('rss'));
Router::setExtensions('rss', false);
$this->assertContains('rss', Router::extensions());

require CAKE . 'Config/routes.php';
Expand Down Expand Up @@ -1670,7 +1670,7 @@ public function testSetExtensions() {
* @return void
*/
public function testExtensionParsing() {
Router::parseExtensions('rss');
Router::setExtensions('rss', false);
require CAKE . 'Config/routes.php';

$result = Router::parse('/posts.rss');
Expand Down Expand Up @@ -1698,7 +1698,7 @@ public function testExtensionParsing() {
$this->assertEquals($expected, $result);

Router::reload();
Router::parseExtensions('rss', 'xml');
Router::setExtensions(['rss', 'xml'], false);
require CAKE . 'Config/routes.php';

$result = Router::parse('/posts.xml');
Expand Down Expand Up @@ -1740,7 +1740,7 @@ public function testExtensionParsing() {
$this->assertEquals($expected, $result);

Router::reload();
Router::parseExtensions('rss');
Router::setExtensions('rss', false);
Router::connect('/controller/action', array('controller' => 'controller', 'action' => 'action', '_ext' => 'rss'));
$result = Router::parse('/controller/action');
$expected = array(
Expand Down Expand Up @@ -2069,7 +2069,7 @@ public function testParsingWithTrailingPeriod() {
public function testParsingWithTrailingPeriodAndParseExtensions() {
Router::reload();
Router::connect('/:controller/:action/*');
Router::parseExtensions('json');
Router::setExtensions('json', false);

$result = Router::parse('/posts/view/something.');
$this->assertEquals('something.', $result['pass'][0], 'Period was chopped off %s');
Expand Down Expand Up @@ -2486,7 +2486,7 @@ public function testReverse() {
*/
public function testReverseWithExtension() {
Router::connect('/:controller/:action/*');
Router::parseExtensions('json');
Router::setExtensions('json', false);

$request = new Request('/posts/view/1.json');
$request->addParams(array(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_app/TestApp/Config/routes.php
Expand Up @@ -22,7 +22,7 @@

// Configure::write('Routing.prefixes', array());

Router::parseExtensions('json');
Router::setExtensions('json');
Router::connect('/some_alias', array('controller' => 'tests_apps', 'action' => 'some_method'));

Router::connect('/', ['controller' => 'pages', 'action' => 'display', 'home']);
Expand Down

0 comments on commit 48df5ba

Please sign in to comment.