Thomas Rabaix 13 лет назад
Родитель
Сommit
58ed91c359

+ 16 - 63
Admin/Admin.php

@@ -33,6 +33,7 @@ use Sonata\AdminBundle\Builder\ListBuilderInterface;
 use Sonata\AdminBundle\Builder\DatagridBuilderInterface;
 use Sonata\AdminBundle\Builder\ShowBuilderInterface;
 use Sonata\AdminBundle\Builder\RouteBuilderInterface;
+use Sonata\AdminBundle\Route\RouteGeneratorInterface;
 
 use Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface;
 use Sonata\AdminBundle\Route\RouteCollection;
@@ -325,9 +326,9 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
     /**
      * The router intance
      *
-     * @var \Symfony\Component\Routing\RouterInterface
+     * @var \Sonata\AdminBundle\Route\RouterGeneratorInterface
      */
-    protected $router;
+    protected $routeGenerator;
 
     /**
      * The generated breadcrumbs
@@ -908,7 +909,7 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
 
 
     /**
-     * generate the object url with the given $name
+     * Generates the object url with the given $name
      *
      * @param  string $name
      * @param  $object
@@ -916,15 +917,15 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
      *
      * @return return a complete url
      */
-    public function generateObjectUrl($name, $object, array $parameters = array())
+    public function generateObjectUrl($name, $object, array $parameters = array(), $absolute = false)
     {
         $parameters['id'] = $this->getNormalizedIdentifier($object);
 
-        return $this->generateUrl($name, $parameters);
+        return $this->generateUrl($name, $parameters, $absolute);
     }
 
     /**
-     * generate the url with the given $name
+     * Generates the url with the given $name
      *
      * @throws RuntimeException
      * @param string $name
@@ -932,57 +933,9 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
      *
      * @return return a complete url
      */
-    public function generateUrl($name, array $parameters = array())
+    public function generateUrl($name, array $parameters = array(), $absolute = false)
     {
-        if (!$this->isChild()) {
-            if (strpos($name, '.')) {
-                $name = $this->getCode().'|'.$name;
-            } else {
-                $name = $this->getCode().'.'.$name;
-            }
-        }
-        // if the admin is a child we automatically append the parent's id
-        else if ($this->isChild()) {
-            $name = $this->baseCodeRoute.'.'.$name;
-
-            // twig template does not accept variable hash key ... so cannot use admin.idparameter ...
-            // switch value
-            if (isset($parameters['id'])) {
-                $parameters[$this->getIdParameter()] = $parameters['id'];
-                unset($parameters['id']);
-            }
-
-            $parameters[$this->getParent()->getIdParameter()] = $this->request->get($this->getParent()->getIdParameter());
-        }
-
-        // if the admin is linked to a parent FieldDescription (ie, embedded widget)
-        if ($this->hasParentFieldDescription()) {
-            // merge link parameter if any provided by the parent field
-            $parameters = array_merge($parameters, $this->getParentFieldDescription()->getOption('link_parameters', array()));
-
-            $parameters['uniqid']  = $this->getUniqid();
-            $parameters['code']    = $this->getCode();
-            $parameters['pcode']   = $this->getParentFieldDescription()->getAdmin()->getCode();
-            $parameters['puniqid'] = $this->getParentFieldDescription()->getAdmin()->getUniqid();
-        }
-
-        if ($name == 'update' || substr($name, -7) == '|update') {
-            $parameters['uniqid'] = $this->getUniqid();
-            $parameters['code']   = $this->getCode();
-        }
-
-        // allows to define persistent parameters
-        if ($this->hasRequest()) {
-            $parameters = array_merge($this->getPersistentParameters(), $parameters);
-        }
-
-        $route = $this->getRoute($name);
-
-        if (!$route) {
-            throw new \RuntimeException(sprintf('unable to find the route `%s`', $name));
-        }
-
-        return $this->router->generate($route->getDefault('_sonata_name'), $parameters);
+        return $this->routeGenerator->generateUrl($this, $name, $parameters, $absolute);
     }
 
     /**
@@ -1781,7 +1734,7 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
 
         $child = $menu->addChild(
             $this->trans($this->getLabelTranslatorStrategy()->getLabel('dashboard', 'breadcrumb', 'link'), array(), 'SonataAdminBundle'),
-            array('uri' => $this->router->generate('sonata_admin_dashboard'))
+            array('uri' => $this->routeGenerator->generate('sonata_admin_dashboard'))
         );
 
         $child = $child->addChild(
@@ -2071,20 +2024,20 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
     }
 
     /**
-     * @param \Symfony\Component\Routing\RouterInterface $router
+     * @param \Sonata\AdminBundle\Route\RouteGeneratorInterface $routeGenerator
      * @return void
      */
-    public function setRouter(RouterInterface $router)
+    public function setRouteGenerator(RouteGeneratorInterface $routeGenerator)
     {
-        $this->router = $router;
+        $this->routeGenerator = $routeGenerator;
     }
 
     /**
-     * @return \Symfony\Component\Routing\RouterInterface
+     * @return \Sonata\AdminBundle\Route\RouteGeneratorInterface
      */
-    public function getRouter()
+    public function getRouteGenerator()
     {
-        return $this->router;
+        return $this->routeGenerator;
     }
 
     public function getCode()

+ 6 - 26
Admin/AdminInterface.php

@@ -20,6 +20,7 @@ use Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface;
 use Sonata\AdminBundle\Builder\RouteBuilderInterface;
 use Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface;
 use Sonata\AdminBundle\Validator\ErrorElement;
+use Sonata\AdminBundle\Route\RouteGeneratorInterface;
 
 use Knp\Menu\FactoryInterface as MenuFactoryInterface;
 
@@ -31,97 +32,84 @@ use Symfony\Component\HttpFoundation\Request;
 interface AdminInterface
 {
     /**
-     * @abstract
      * @param \Sonata\AdminBundle\Builder\FormContractorInterface $formContractor
      * @return void
      */
     function setFormContractor(FormContractorInterface $formContractor);
 
     /**
-     * @abstract
      * @param ListBuilderInterface $listBuilder
      * @return void
      */
     function setListBuilder(ListBuilderInterface $listBuilder);
 
     /**
-     * @abstract
      * @param \Sonata\AdminBundle\Builder\DatagridBuilderInterface $datagridBuilder
      * @return void
      */
     function setDatagridBuilder(DatagridBuilderInterface $datagridBuilder);
 
     /**
-     * @abstract
      * @param \Symfony\Component\Translation\TranslatorInterface $translator
      * @return void
      */
     function setTranslator(TranslatorInterface $translator);
 
     /**
-     * @abstract
      * @param \Symfony\Component\HttpFoundation\Request $request
      * @return void
      */
     function setRequest(Request $request);
 
     /**
-     * @abstract
      * @param Pool $pool
      * @return void
      */
     function setConfigurationPool(Pool $pool);
 
     /**
-     * @abstract
-     * @param \Symfony\Component\Routing\RouterInterface $router
+     * @param \Sonata\AdminBundle\Route\RouteGeneratorInterface $router
      * @return void
      */
-    function setRouter(RouterInterface $router);
+    function setRouteGenerator(RouteGeneratorInterface $routeGenerator);
 
     /**
      * Returns the class name managed
      *
-     * @abstract
      * @return void
      */
     function getClass();
 
     /**
-     * @abstract
      * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
      * @return void
      */
     function attachAdminClass(FieldDescriptionInterface $fieldDescription);
 
     /**
-     * @abstract
      * @return \Sonata\AdminBundle\Datagrid\DatagridInterface
      */
     function getDatagrid();
 
     /**
-     * @abstract
-     * @param string $name
+     * @param $name
      * @param array $parameters
+     * @param bool $absolute
      * @return void
      */
-    function generateUrl($name, array $parameters = array());
+    function generateUrl($name, array $parameters = array(), $absolute = false);
 
     /**
-     * @abstract
      * @return \Sonata\AdminBundle\ModelManagerInterface;
      */
     function getModelManager();
 
     /**
-     * @abstract
      * @return string the manager type of the admin
      */
     function getManagerType();
 
     /**
-     * @abstract
      * @return \Sonata\AdminBundle\Datagrid\ProxyQueryInterface
      */
     function createQuery($context = 'list');
@@ -133,14 +121,12 @@ interface AdminInterface
     function getFormBuilder();
 
     /**
-     * @abstract
      * @param string $name
      * @return \Sonata\AdminBundle\Admin\FieldDescriptionInterface
      */
     function getFormFieldDescription($name);
 
     /**
-     * @abstract
      * @return \Symfony\Component\HttpFoundation\Request
      */
     function getRequest();
@@ -152,13 +138,11 @@ interface AdminInterface
     function getCode();
 
     /**
-     * @abstract
      * @return array
      */
     function getSecurityInformation();
 
     /**
-     * @abstract
      * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $parentFieldDescription
      * @return void
      */
@@ -178,7 +162,6 @@ interface AdminInterface
     /**
      * Return the parameter name used to represente the id in the url
      *
-     * @abstract
      * @return string
      */
     function getRouterIdParameter();
@@ -236,7 +219,6 @@ interface AdminInterface
     function isGranted($name, $object = null);
 
     /**
-     * @abstract
      * @param $entity
      */
     function getNormalizedIdentifier($entity);
@@ -424,14 +406,12 @@ interface AdminInterface
     function validate(ErrorElement $errorElement, $object);
 
     /**
-     * @abstract
      * @param $context
      * @return boolean
      */
     function showIn($context);
 
     /**
-     * @abstract
      * @param $object
      * @return void
      */

+ 10 - 1
Admin/Pool.php

@@ -91,13 +91,22 @@ class Pool
      */
     public function getAdminByClass($class)
     {
-        if (!isset($this->adminClasses[$class])) {
+        if (!$this->hasAdminByClass($class)) {
             return null;
         }
 
         return $this->getInstance($this->adminClasses[$class]);
     }
 
+    /**
+     * @param $class
+     * @return bool
+     */
+    public function hasAdminByClass($class)
+    {
+        return isset($this->adminClasses[$class]);
+    }
+
     /**
      * Returns an admin class by its Admin code
      * ie : sonata.news.admin.post|sonata.news.admin.comment => return the child class of post

+ 1 - 1
DependencyInjection/Compiler/AddDependencyCallsCompilerPass.php

@@ -171,7 +171,7 @@ class AddDependencyCallsCompilerPass implements CompilerPassInterface
             'datagrid_builder'          => sprintf('sonata.admin.builder.%s_datagrid', $manager_type),
             'translator'                => 'translator',
             'configuration_pool'        => 'sonata.admin.pool',
-            'router'                    => 'router',
+            'route_generator'           => 'sonata.admin.route.default_generator',
             'validator'                 => 'validator',
             'security_handler'          => 'sonata.admin.security.handler',
             'menu_factory'              => 'knp_menu.factory',

+ 4 - 0
Resources/config/route.xml

@@ -12,5 +12,9 @@
         <service id="sonata.admin.route.query_string" class="Sonata\AdminBundle\Route\QueryStringBuilder">
             <argument type="service" id="sonata.admin.audit.manager" />
         </service>
+
+        <service id="sonata.admin.route.default_generator" class="Sonata\AdminBundle\Route\DefaultRouteGenerator">
+            <argument type="service" id="router" />
+        </service>
     </services>
 </container>

+ 99 - 0
Route/DefaultRouteGenerator.php

@@ -0,0 +1,99 @@
+<?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\Admin\AdminInterface;
+use Symfony\Component\Routing\RouterInterface;
+
+class DefaultRouteGenerator implements RouteGeneratorInterface
+{
+    private $router;
+
+    /**
+     * @param \Symfony\Component\Routing\RouterInterface $router
+     */
+    public function __construct(RouterInterface $router)
+    {
+        $this->router = $router;
+    }
+
+    /**
+     * @param $name
+     * @param array $parameters
+     * @param bool $absolute
+     * @return string
+     */
+    public function generate($name, array $parameters = array(), $absolute = false)
+    {
+        return $this->router->generate($name, $parameters, $absolute);
+    }
+
+    /**
+     * @throws \RuntimeException
+     * @param \Sonata\AdminBundle\Admin\AdminInterface $admin
+     * @param $name
+     * @param array $parameter
+     * @param bool $absolute
+     * @return string
+     */
+    public function generateUrl(AdminInterface $admin, $name, array $parameters = array(), $absolute = false)
+    {
+        if (!$admin->isChild()) {
+            if (strpos($name, '.')) {
+                $name = $admin->getCode().'|'.$name;
+            } else {
+                $name = $admin->getCode().'.'.$name;
+            }
+        }
+        // if the admin is a child we automatically append the parent's id
+        else if ($admin->isChild()) {
+            $name = $admin->getBaseCodeRoute().'.'.$name;
+
+            // twig template does not accept variable hash key ... so cannot use admin.idparameter ...
+            // switch value
+            if (isset($parameters['id'])) {
+                $parameters[$admin->getIdParameter()] = $parameters['id'];
+                unset($parameters['id']);
+            }
+
+            $parameters[$admin->getParent()->getIdParameter()] = $admin->getRequest()->get($admin->getParent()->getIdParameter());
+        }
+
+        // if the admin is linked to a parent FieldDescription (ie, embedded widget)
+        if ($admin->hasParentFieldDescription()) {
+            // merge link parameter if any provided by the parent field
+            $parameters = array_merge($parameters, $admin->getParentFieldDescription()->getOption('link_parameters', array()));
+
+            $parameters['uniqid']  = $admin->getUniqid();
+            $parameters['code']    = $admin->getCode();
+            $parameters['pcode']   = $admin->getParentFieldDescription()->getAdmin()->getCode();
+            $parameters['puniqid'] = $admin->getParentFieldDescription()->getAdmin()->getUniqid();
+        }
+
+        if ($name == 'update' || substr($name, -7) == '|update') {
+            $parameters['uniqid'] = $admin->getUniqid();
+            $parameters['code']   = $admin->getCode();
+        }
+
+        // allows to define persistent parameters
+        if ($admin->hasRequest()) {
+            $parameters = array_merge($admin->getPersistentParameters(), $parameters);
+        }
+
+        $route = $admin->getRoute($name);
+
+        if (!$route) {
+            throw new \RuntimeException(sprintf('unable to find the route `%s`', $name));
+        }
+
+        return $this->router->generate($route->getDefault('_sonata_name'), $parameters, $absolute);
+    }
+}

+ 33 - 0
Route/RouteGeneratorInterface.php

@@ -0,0 +1,33 @@
+<?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\Admin\AdminInterface;
+
+interface RouteGeneratorInterface
+{
+    /**
+     * @param \Sonata\AdminBundle\Admin\AdminInterface $admin
+     * @param $name
+     * @param array $parameters
+     * @param bool $absolute
+     * @return string
+     */
+    function generateUrl(AdminInterface $admin, $name, array $parameters = array(), $absolute = false);
+
+    /**
+     * @param $name
+     * @param array $parameters
+     * @param bool $absolute
+     * @return void
+     */
+    function generate($name, array $parameters = array(), $absolute = false);
+}

+ 0 - 1
Tests/Admin/BaseAdminTest.php

@@ -166,7 +166,6 @@ class BaseAdminTest extends \PHPUnit_Framework_TestCase
 
     public function testGetBaseRouteNameWithChildAdmin()
     {
-
         $pathInfo = new \Sonata\AdminBundle\Route\PathInfoBuilder($this->getMock('Sonata\AdminBundle\Model\AuditManagerInterface'));
         $postAdmin = new PostAdmin('sonata.post.admin.post', 'Application\Sonata\NewsBundle\Entity\Post', 'SonataNewsBundle:PostAdmin');
         $postAdmin->setRouteBuilder($pathInfo);

+ 2 - 0
Tests/Admin/PoolTest.php

@@ -77,12 +77,14 @@ class PoolTest extends \PHPUnit_Framework_TestCase
     public function testGetAdminForClassWhenAdminClassIsNotSet()
     {
         $this->pool->setAdminClasses(array('someclass' => 'sonata.user.admin.group1'));
+        $this->assertFalse($this->pool->hasAdminByClass('notexists'));
         $this->assertNull($this->pool->getAdminByClass('notexists'));
     }
 
     public function testGetAdminForClassWhenAdminClassIsSet()
     {
         $this->pool->setAdminClasses(array('someclass' => 'sonata.user.admin.group1'));
+        $this->assertTrue($this->pool->hasAdminByClass('someclass'));
         $this->assertEquals('adminUserClass', $this->pool->getAdminByClass('someclass'));
     }
 

+ 27 - 0
Tests/Route/DefaultRouteGeneratorTest.php

@@ -0,0 +1,27 @@
+<?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\Tests\Route;
+
+use Sonata\AdminBundle\Route\DefaultRouteGenerator;
+
+class DefaultRouteGeneratorTest extends \PHPUnit_Framework_TestCase
+{
+    public function testGenerate()
+    {
+        $router = $this->getMock('\Symfony\Component\Routing\RouterInterface');
+        $router->expects($this->once())->method('generate')->will($this->returnValue('/foo/bar'));
+
+        $generator = new DefaultRouteGenerator($router);
+
+        $this->assertEquals('/foo/bar', $generator->generate('foo_bar'));
+    }
+}

+ 1 - 1
Tests/tests/autoload.php.dist

@@ -1,7 +1,7 @@
 <?php
 
 // try to reuse lib defined in a current symfony2 project
-$autoload = __DIR__.'/../../../../../../app/bootstrap.php.cache';
+$autoload = __DIR__.'/../../../../../../app/autoload.php';
 if (is_file($autoload)) {
     include $autoload;
 } else {