Browse Source

Add Router::getRouteBuilder()

This will be useful for the routing() hook to easily get at a route
builder. It also makes existing methods a bit shorter which is nice.
Mark Story 9 years ago
parent
commit
575e4d76bb
2 changed files with 43 additions and 9 deletions
  1. 23 9
      src/Routing/Router.php
  2. 20 0
      tests/TestCase/Routing/RouterTest.php

+ 23 - 9
src/Routing/Router.php

@@ -911,6 +911,26 @@ class Router
     }
 
     /**
+     * Create a RouteBuilder for the provided path.
+     *
+     * @param string $path The path to set the builder to.
+     * @param array $options The options for the builder
+     * @return \Cake\Routing\RouteBuilder
+     */
+    public static function getRouteBuilder($path, array $options = [])
+    {
+        $defaults = [
+            'routeClass' => static::defaultRouteClass(),
+            'extensions' => static::$_defaultExtensions,
+        ];
+        $options += $defaults;
+        return new RouteBuilder(static::$_collection, $path, [], [
+            'routeClass' => $options['routeClass'],
+            'extensions' => $options['extensions'],
+        ]);
+    }
+
+    /**
      * Create a routing scope.
      *
      * Routing scopes allow you to keep your routes DRY and avoid repeating
@@ -954,18 +974,12 @@ class Router
      */
     public static function scope($path, $params = [], $callback = null)
     {
-        $options = [
-            'routeClass' => static::defaultRouteClass(),
-            'extensions' => static::$_defaultExtensions,
-        ];
+        $options = [];
         if (is_array($params)) {
-            $options = $params + $options;
+            $options = $params;
             unset($params['routeClass'], $params['extensions']);
         }
-        $builder = new RouteBuilder(static::$_collection, '/', [], [
-            'routeClass' => $options['routeClass'],
-            'extensions' => $options['extensions'],
-        ]);
+        $builder = static::getRouteBuilder('/', $options);
         $builder->scope($path, $params, $callback);
     }
 

+ 20 - 0
tests/TestCase/Routing/RouterTest.php

@@ -19,6 +19,7 @@ use Cake\Core\Plugin;
 use Cake\Http\MiddlewareQueue;
 use Cake\Http\ServerRequest;
 use Cake\Http\ServerRequestFactory;
+use Cake\Routing\RouteBuilder;
 use Cake\Routing\RouteCollection;
 use Cake\Routing\Router;
 use Cake\Routing\Route\Route;
@@ -3353,6 +3354,25 @@ class RouterTest extends TestCase
     }
 
     /**
+     * Test getting a route builder instance.
+     *
+     * @return void
+     */
+    public function testGetRouteBuilder()
+    {
+        $builder = Router::getRouteBuilder('/api');
+        $this->assertInstanceOf(RouteBuilder::class, $builder);
+        $this->assertSame('/api', $builder->path());
+
+        $builder = Router::getRouteBuilder('/', [
+            'routeClass' => 'InflectedRoute',
+            'extensions' => ['json']
+        ]);
+        $this->assertInstanceOf(RouteBuilder::class, $builder);
+        $this->assertSame(['json'], $builder->extensions());
+    }
+
+    /**
      * Connect some fallback routes for testing router behavior.
      *
      * @return void