Skip to content

Commit

Permalink
Remove Router::parseExtensions().
Browse files Browse the repository at this point in the history
Router::extensions() can now be used to add/set extensions.
  • Loading branch information
ADmad committed Sep 7, 2014
1 parent de7cbdc commit 40acf40
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 51 deletions.
6 changes: 3 additions & 3 deletions src/Controller/Component/RequestHandlerComponent.php
Expand Up @@ -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;

Expand Down Expand Up @@ -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'])) {
Expand Down Expand Up @@ -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
Expand Down
40 changes: 14 additions & 26 deletions src/Routing/Router.php
Expand Up @@ -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
Expand All @@ -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);
}

/**
Expand Down
30 changes: 9 additions & 21 deletions tests/TestCase/Routing/RouterTest.php
Expand Up @@ -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';
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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();
Expand All @@ -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'));
}

/**
Expand All @@ -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());
}

Expand All @@ -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');
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_app/config/routes.php
Expand Up @@ -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'));
Expand Down

0 comments on commit 40acf40

Please sign in to comment.