Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

adding setExtensions() to allow adding more extensions as parseExtens…

…ions would override existing valid extensions
  • Loading branch information...
commit acbb2bebc97e6d68b8d76cc5953a781cadbf94b0 1 parent ab1f336
@ceeram ceeram authored
View
25 lib/Cake/Routing/Router.php
@@ -1102,13 +1102,14 @@ public static function stripPlugin($base, $plugin = null) {
public static function parseExtensions() {
self::$_parseExtensions = true;
if (func_num_args() > 0) {
- self::$_validExtensions = func_get_args();
+ self::setExtensions(func_get_args(), false);
}
}
/**
- * Get the list of extensions that can be parsed by Router. To add more
- * extensions use Router::parseExtensions()
+ * Get the list of extensions that can be parsed by Router.
+ * To initially set extensions use `Router::parseExtensions()`
+ * To add more see `setExtensions()`
*
* @return array Array of extensions Router is configured to parse.
*/
@@ -1116,6 +1117,24 @@ public static function extensions() {
return self::$_validExtensions;
}
+/**
+ * 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 boolean $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)) {
+ return self::$_validExtensions;
+ }
+ if (!$merge) {
+ return self::$_validExtensions = $extensions;
+ }
+ return self::$_validExtensions = array_merge(self::$_validExtensions, $extensions);
+ }
+
}
//Save the initial state
View
37 lib/Cake/Test/Case/Routing/RouterTest.php
@@ -1235,15 +1235,46 @@ public function testPrefixRoutingAndPlugins() {
}
/**
- * testExtensionParsingSetting method
+ * testParseExtensions method
*
* @return void
*/
- public function testExtensionParsingSetting() {
+ public function testParseExtensions() {
$this->assertEquals(array(), Router::extensions());
Router::parseExtensions('rss');
- $this->assertEquals(Router::extensions(), array('rss'));
+ $this->assertEquals(array('rss'), Router::extensions());
+ }
+
+/**
+ * testSetExtensions method
+ *
+ * @return void
+ */
+ public function testSetExtensions() {
+ Router::setExtensions(array('rss'));
+ $this->assertEquals(array('rss'), Router::extensions());
+
+ require CAKE . 'Config' . DS . 'routes.php';
+ $result = Router::parse('/posts.rss');
+ $this->assertFalse(isset($result['ext']));
+
+ Router::parseExtensions();
+ $result = Router::parse('/posts.rss');
+ $this->assertEquals('rss', $result['ext']);
+
+ $result = Router::parse('/posts.xml');
+ $this->assertFalse(isset($result['ext']));
+
+ Router::setExtensions(array('xml'));
+ $result = Router::extensions();
+ $this->assertEquals(array('rss', 'xml'), $result);
+
+ $result = Router::parse('/posts.xml');
+ $this->assertEquals('xml', $result['ext']);
+
+ $result = Router::setExtensions(array('pdf'), false);
+ $this->assertEquals(array('pdf'), $result);
}
/**
Please sign in to comment.
Something went wrong with that request. Please try again.