فهرست منبع

support controller as a service in RouteCollection

dbu 13 سال پیش
والد
کامیت
d7af08adfd
3فایلهای تغییر یافته به همراه43 افزوده شده و 4 حذف شده
  1. 13 1
      Resources/doc/reference/architecture.rst
  2. 6 0
      Route/RouteCollection.php
  3. 24 3
      Tests/Route/RouteCollectionTest.php

+ 13 - 1
Resources/doc/reference/architecture.rst

@@ -112,7 +112,14 @@ reduce overhead.
 Declaring a new Admin class
 ---------------------------
 
-Once you have created an admin class, you must declare the class to use it. Like
+Once you have created an admin class, you need to make the framework aware of
+it. To do that, you need to add a tag with the name ``sonata.admin`` to the
+service. Parameters for that tag are:
+* ``manager_type``: Label of the document manager to inject;
+* ``group``: A label to allow grouping on the dashboard;
+* ``label``: Label to use for the name of the entity this manager handles;
+
+Examples:
 
 .. code-block:: xml
 
@@ -138,4 +145,9 @@ Or if you're using a YML configuration file,
           arguments: [null, Sonata\NewsBundle\Entity\Post, SonataNewsBundle:PostAdmin]
 
 
+You can extend ``Sonata\AdminBundle\Admin\Admin`` to minimize the amount of
+code to write. This base admin uses the routing services to build routes.
+Note that you can use both the Bundle:Controller format or a service name to
+specify what controller to load.
+
 .. _`Django Project Website`: http://www.djangoproject.com/

+ 6 - 0
Route/RouteCollection.php

@@ -137,6 +137,12 @@ class RouteCollection
             $action = substr($action, $pos + 1);
         }
 
+        // if this is a service rather than just a controller name, the suffix
+        // Action is not automatically appended to the method name
+        if (strpos($this->baseControllerName, ':') === false) {
+            $action .= 'Action';
+        }
+
         return lcfirst(str_replace(' ', '', ucwords(strtr($action, '_-', '  '))));
     }
 

+ 24 - 3
Tests/Route/RouteCollectionTest.php

@@ -27,12 +27,20 @@ class RouteCollectionTest extends \PHPUnit_Framework_TestCase
 
     public function testActionify()
     {
-        $routeCollection = new RouteCollection('base.Code.Route', 'baseRouteName', 'baseRoutePattern', 'baseControllerName');
+        $routeCollection = new RouteCollection('base.Code.Route', 'baseRouteName', 'baseRoutePattern', 'BundleName:ControllerName');
 
         $this->assertEquals('fooBar', $routeCollection->actionify('Foo bar'));
         $this->assertEquals('bar', $routeCollection->actionify('Foo.bar'));
     }
 
+    public function testActionifyService()
+    {
+        $routeCollection = new RouteCollection('base.Code.Route', 'baseRouteName', 'baseRoutePattern', 'baseControllerService');
+
+        $this->assertEquals('fooBarAction', $routeCollection->actionify('Foo bar'));
+        $this->assertEquals('barAction', $routeCollection->actionify('Foo.bar'));
+    }
+
     public function testCode()
     {
         $routeCollection = new RouteCollection('base.Code.Route', 'baseRouteName', 'baseRoutePattern', 'baseControllerName');
@@ -78,13 +86,26 @@ class RouteCollectionTest extends \PHPUnit_Framework_TestCase
 
     public function testRoute()
     {
-        $routeCollection = new RouteCollection('baseCodeRoute', 'baseRouteName', 'baseRoutePattern', 'baseControllerName');
+        $routeCollection = new RouteCollection('baseCodeRoute', 'baseRouteName', 'baseRoutePattern', 'BundleName:ControllerName');
+
+        $routeCollection->add('view');
+
+        $route = $routeCollection->get('view');
+
+        $this->assertEquals('BundleName:ControllerName:view', $route->getDefault('_controller'));
+        $this->assertEquals('baseCodeRoute', $route->getDefault('_sonata_admin'));
+        $this->assertEquals('baseRouteName_view', $route->getDefault('_sonata_name'));
+    }
+
+    public function testRouteControllerService()
+    {
+        $routeCollection = new RouteCollection('baseCodeRoute', 'baseRouteName', 'baseRoutePattern', 'baseControllerServiceName');
 
         $routeCollection->add('view');
 
         $route = $routeCollection->get('view');
 
-        $this->assertEquals('baseControllerName:view', $route->getDefault('_controller'));
+        $this->assertEquals('baseControllerServiceName:viewAction', $route->getDefault('_controller'));
         $this->assertEquals('baseCodeRoute', $route->getDefault('_sonata_admin'));
         $this->assertEquals('baseRouteName_view', $route->getDefault('_sonata_name'));
     }