Преглед изворни кода

Create dedicated services to build the routing information

Thomas Rabaix пре 13 година
родитељ
комит
249cb2ade9

+ 26 - 16
Admin/Admin.php

@@ -31,6 +31,7 @@ use Sonata\AdminBundle\Builder\FormContractorInterface;
 use Sonata\AdminBundle\Builder\ListBuilderInterface;
 use Sonata\AdminBundle\Builder\DatagridBuilderInterface;
 use Sonata\AdminBundle\Builder\ShowBuilderInterface;
+use Sonata\AdminBundle\Builder\RouteBuilderInterface;
 
 use Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface;
 use Sonata\AdminBundle\Route\RouteCollection;
@@ -298,6 +299,11 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
      */
     protected $datagridBuilder;
 
+    /**
+     * @var \Sonata\AdminBundle\Builder\RouteBuilderInterface
+     */
+    protected $routeBuilder;
+
     /**
      * The datagrid instance
      *
@@ -821,32 +827,20 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
 
         $this->loaded['routes'] = true;
 
-        $collection = new RouteCollection(
+        $this->routes = new RouteCollection(
             $this->getBaseCodeRoute(),
             $this->getBaseRouteName(),
             $this->getBaseRoutePattern(),
             $this->getBaseControllerName()
         );
 
-        $collection->add('list');
-        $collection->add('create');
-        $collection->add('batch');
-        $collection->add('edit', $this->getRouterIdParameter().'/edit');
-        $collection->add('delete', $this->getRouterIdParameter().'/delete');
-        $collection->add('show', $this->getRouterIdParameter().'/show');
+        $this->routeBuilder->build($this, $this->routes);
 
-        // add children urls
-        foreach ($this->getChildren() as $children) {
-            $collection->addCollection($children->getRoutes());
-        }
-
-        $this->configureRoutes($collection);
+        $this->configureRoutes($this->routes);
 
         foreach($this->extensions as $extension) {
-            $extension->configureRoutes($this, $collection);
+            $extension->configureRoutes($this, $this->routes);
         }
-
-        $this->routes = $collection;
     }
 
     /**
@@ -2174,4 +2168,20 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
     {
         return $this->menuFactory;
     }
+
+    /**
+     * @param \Sonata\AdminBundle\Builder\RouteBuilderInterface $routeBuilder
+     */
+    public function setRouteBuilder(RouteBuilderInterface $routeBuilder)
+    {
+        $this->routeBuilder = $routeBuilder;
+    }
+
+    /**
+     * @return \Sonata\AdminBundle\Builder\RouteBuilderInterface
+     */
+    public function getRouteBuilder()
+    {
+        return $this->routeBuilder;
+    }
 }

+ 8 - 0
Admin/AdminInterface.php

@@ -156,4 +156,12 @@ interface AdminInterface
      * @return string the translated string
      */
     function trans($id, array $parameters = array(), $domain = null, $locale = null);
+
+    /**
+     * Return the parameter name used to represente the id in the url
+     *
+     * @abstract
+     * @return string
+     */
+    function getRouterIdParameter();
 }

+ 26 - 0
Builder/RouteBuilderInterface.php

@@ -0,0 +1,26 @@
+<?php
+
+/*
+ * This file is part of the Sonata package.
+ *
+ * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Sonata\AdminBundle\Builder;
+
+use Sonata\AdminBundle\Admin\AdminInterface;
+use Sonata\AdminBundle\Route\RouteCollection;
+
+interface RouteBuilderInterface
+{
+
+    /**
+     * @abstract
+     * @param \Sonata\AdminBundle\Admin\AdminInterface $admin
+     * @param \Sonata\AdminBundle\Route\RouteCollection $collection
+     */
+    function build(AdminInterface $admin, RouteCollection $collection);
+}

+ 8 - 4
Controller/CRUDController.php

@@ -219,13 +219,15 @@ class CRUDController extends Controller
      * @param  $id
      * @return \Symfony\Component\HttpFoundation\Response
      */
-    public function editAction($id)
+    public function editAction($id = null)
     {
         if (false === $this->admin->isGranted('EDIT')) {
             throw new AccessDeniedException();
         }
 
-        $object = $this->admin->getObject($this->get('request')->get($this->admin->getIdParameter()));
+        $id = $this->get('request')->get($this->admin->getIdParameter());
+
+        $object = $this->admin->getObject($id);
 
         if (!$object) {
             throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
@@ -423,13 +425,15 @@ class CRUDController extends Controller
      *
      * @return \Symfony\Component\HttpFoundation\Response
      */
-    public function showAction($id)
+    public function showAction($id = null)
     {
         if (false === $this->admin->isGranted('SHOW')) {
             throw new AccessDeniedException();
         }
 
-        $object = $this->admin->getObject($this->get('request')->get($this->admin->getIdParameter()));
+        $id = $this->get('request')->get($this->admin->getIdParameter());
+
+        $object = $this->admin->getObject($id);
 
         if (!$object) {
             throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));

+ 4 - 0
DependencyInjection/Compiler/AddDependencyCallsCompilerPass.php

@@ -182,6 +182,10 @@ class AddDependencyCallsCompilerPass implements CompilerPassInterface
             }
         }
 
+        if (!$definition->hasMethodCall('setRouteBuilder')) {
+            $definition->addMethodCall('setRouteBuilder', array(new Reference('sonata.admin.route.path_info')));
+        }
+
         if (isset($service['label'])) {
             $label = $service['label'];
         } elseif (isset($attributes[0]['label'])) {

+ 1 - 0
DependencyInjection/SonataAdminExtension.php

@@ -54,6 +54,7 @@ class SonataAdminExtension extends Extension
         $loader->load('core.xml');
         $loader->load('form_types.xml');
         $loader->load('validator.xml');
+        $loader->load('route.xml');
 
         $configuration = new Configuration();
         $processor = new Processor();

+ 12 - 0
Resources/config/route.xml

@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+
+<container xmlns="http://symfony.com/schema/dic/services"
+           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+           xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
+
+    <services>
+        <service id="sonata.admin.route.path_info" class="Sonata\AdminBundle\Route\PathInfoBuilder" />
+
+        <service id="sonata.admin.route.query_string" class="Sonata\AdminBundle\Route\QueryStringBuilder" />
+    </services>
+</container>

+ 36 - 0
Route/PathInfoBuilder.php

@@ -0,0 +1,36 @@
+<?php
+/*
+ * This file is part of the Sonata package.
+ *
+ * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ *
+ */
+namespace Sonata\AdminBundle\Route;
+
+use Sonata\AdminBundle\Builder\RouteBuilderInterface;
+use Sonata\AdminBundle\Admin\AdminInterface;
+
+class PathInfoBuilder implements RouteBuilderInterface
+{
+    /**
+     * @param \Sonata\AdminBundle\Admin\AdminInterface $admin
+     * @param \Sonata\AdminBundle\Route\RouteCollection $collection
+     */
+    function build(AdminInterface $admin, RouteCollection $collection)
+    {
+        $collection->add('list');
+        $collection->add('create');
+        $collection->add('batch');
+        $collection->add('edit', $admin->getRouterIdParameter().'/edit');
+        $collection->add('delete', $admin->getRouterIdParameter().'/delete');
+        $collection->add('show', $admin->getRouterIdParameter().'/show');
+
+        // add children urls
+        foreach ($admin->getChildren() as $children) {
+            $collection->addCollection($children->getRoutes());
+        }
+    }
+}

+ 36 - 0
Route/QueryStringBuilder.php

@@ -0,0 +1,36 @@
+<?php
+/*
+ * This file is part of the Sonata package.
+ *
+ * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ *
+ */
+namespace Sonata\AdminBundle\Route;
+
+use Sonata\AdminBundle\Builder\RouteBuilderInterface;
+use Sonata\AdminBundle\Admin\AdminInterface;
+
+class QueryStringBuilder implements RouteBuilderInterface
+{
+    /**
+     * @param \Sonata\AdminBundle\Admin\AdminInterface $admin
+     * @param \Sonata\AdminBundle\Route\RouteCollection $collection
+     */
+    function build(AdminInterface $admin, RouteCollection $collection)
+    {
+        $collection->add('list');
+        $collection->add('create');
+        $collection->add('batch');
+        $collection->add('edit');
+        $collection->add('delete');
+        $collection->add('show');
+
+        // add children urls
+        foreach ($admin->getChildren() as $children) {
+            $collection->addCollection($children->getRoutes());
+        }
+    }
+}