Permalink
Browse files

Merge pull request #404 from tigrang/router-defaultRouteClass-fix

Router::defaultRouteClass() now returns currently set route class if no param is passed. Also added Router::_validateRouteClass() for route class validation.
  • Loading branch information...
2 parents ef568b1 + f5b62eb commit f11389fd1010b2950b92f57cf410a92225f31cc4 @ADmad ADmad committed Jan 15, 2012
Showing with 73 additions and 13 deletions.
  1. +29 −13 lib/Cake/Routing/Router.php
  2. +44 −0 lib/Cake/Test/Case/Routing/RouterTest.php
@@ -160,14 +160,33 @@ class Router {
protected static $_routeClass = 'CakeRoute';
/**
- * Set the default rotue class to use
+ * Set the default route class to use or return the current one
*
- * @param sting $routeClass to set as default
- * @return void
+ * @param string $routeClass to set as default
+ * @return mixed void|string
+ * @throws RouterException
*/
- public static function defaultRouteClass($routeClass) {
- self::$_routeClass = $routeClass;
- }
+ public static function defaultRouteClass($routeClass = null) {
+ if (is_null($routeClass)) {
+ return self::$_routeClass;
+ }
+
+ self::$_routeClass = self::_validateRouteClass($routeClass);
+ }
+
+/**
+ * Validates that the passed route class exists and is a subclass of CakeRoute
+ *
+ * @param $routeClass
+ * @return string
+ * @throws RouterException
+ */
+ protected static function _validateRouteClass($routeClass) {
+ if (!class_exists($routeClass) || !is_subclass_of($routeClass, 'CakeRoute')) {
+ throw new RouterException(__d('cake_dev', 'Route classes must extend CakeRoute'));
+ }
+ return $routeClass;
+ }
/**
* Sets the Routing prefixes.
@@ -278,14 +297,11 @@ public static function connect($route, $defaults = array(), $options = array())
}
$routeClass = self::$_routeClass;
if (isset($options['routeClass'])) {
- $routeClass = $options['routeClass'];
- if (!is_subclass_of($routeClass, 'CakeRoute')) {
- throw new RouterException(__d('cake_dev', 'Route classes must extend CakeRoute'));
- }
+ $routeClass = self::_validateRouteClass($options['routeClass']);
unset($options['routeClass']);
- if ($routeClass == 'RedirectRoute' && isset($defaults['redirect'])) {
- $defaults = $defaults['redirect'];
- }
+ }
+ if ($routeClass == 'RedirectRoute' && isset($defaults['redirect'])) {
+ $defaults = $defaults['redirect'];
}
self::$routes[] = new $routeClass($route, $defaults, $options);
return self::$routes;
@@ -2519,4 +2519,48 @@ public function testDefaultRouteClass() {
$this->assertInstanceOf('TestDefaultRouteClass', $result[0]);
}
+/**
+ * Test getting the default route class
+ *
+ * @return void
+ */
+ public function testDefaultRouteClassGetter() {
+ $routeClass = 'TestDefaultRouteClass';
+ Router::defaultRouteClass($routeClass);
+
+ $this->assertEqual($routeClass, Router::defaultRouteClass());
+ $this->assertEqual($routeClass, Router::defaultRouteClass(null));
+ }
+
+/**
+ * Test that route classes must extend CakeRoute
+ *
+ * @expectedException RouterException
+ * @return void
+ */
+ public function testDefaultRouteException() {
+ Router::defaultRouteClass('');
+ Router::connect('/:controller', array());
+ }
+
+/**
+ * Test that route classes must extend CakeRoute
+ *
+ * @expectedException RouterException
+ * @return void
+ */
+ public function testSettingInvalidDefaultRouteException() {
+ Router::defaultRouteClass('Object');
+ }
+
+/**
+ * Test that class must exist
+ *
+ * @expectedException RouterException
+ * @return void
+ */
+ public function testSettingNonExistentDefaultRouteException() {
+ Router::defaultRouteClass('NonExistentClass');
+ }
+
}

0 comments on commit f11389f

Please sign in to comment.