Переглянути джерело

remove the request dependency when retrieving the admmin through the DIC, the request is now set by the CRUDController

Thomas 14 роки тому
батько
коміт
035d2440f8

+ 21 - 15
Admin/Admin.php

@@ -747,11 +747,11 @@ abstract class Admin implements AdminInterface
      *
      * @throws RuntimeException
      * @param  $name
-     * @param array $params
+     * @param array $parameters
      *
      * @return return a complete url
      */
-    public function generateUrl($name, array $params = array())
+    public function generateUrl($name, array $parameters = array())
     {
 
         if (!$this->isChild()) {
@@ -767,25 +767,25 @@ abstract class Admin implements AdminInterface
 
             // twig template does not accept variable hash key ... so cannot use admin.idparameter ...
             // switch value
-            if (isset($params['id'])) {
-                $params[$this->getIdParameter()] = $params['id'];
-                unset($params['id']);
+            if (isset($parameters['id'])) {
+                $parameters[$this->getIdParameter()] = $parameters['id'];
+                unset($parameters['id']);
             }
 
-            $params[$this->getParent()->getIdParameter()] = $this->request->get($this->getParent()->getIdParameter());
+            $parameters[$this->getParent()->getIdParameter()] = $this->request->get($this->getParent()->getIdParameter());
         }
 
         // if the admin is linked to a FieldDescription (ie, embedded widget)
         if ($this->hasParentFieldDescription()) {
-            $params['uniqid']  = $this->getUniqid();
-            $params['code']    = $this->getCode();
-            $params['pcode']   = $this->getParentFieldDescription()->getAdmin()->getCode();
-            $params['puniqid'] = $this->getParentFieldDescription()->getAdmin()->getUniqid();
+            $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') {
-            $params['uniqid'] = $this->getUniqid();
-            $params['code']   = $this->getCode();
+            $parameters['uniqid'] = $this->getUniqid();
+            $parameters['code']   = $this->getCode();
         }
         
         $url = $this->getUrl($name);
@@ -794,7 +794,7 @@ abstract class Admin implements AdminInterface
             throw new \RuntimeException(sprintf('unable to find the url `%s`', $name));
         }
 
-        return $this->router->generate($url['name'], $params);
+        return $this->router->generate($url['name'], $parameters);
     }
 
     /**
@@ -868,6 +868,7 @@ abstract class Admin implements AdminInterface
     /**
      * attach an admin instance to the given FieldDescription
      *
+     * @param FieldDescription $fieldDescription
      */
     public function attachAdminClass(FieldDescription $fieldDescription)
     {
@@ -884,7 +885,7 @@ abstract class Admin implements AdminInterface
     /**
      * return the target object
      *
-     * @param  $id
+     * @param integer $id
      * @return
      */
     public function getObject($id)
@@ -925,7 +926,8 @@ abstract class Admin implements AdminInterface
     /**
      * return a form depend on the given $object
      *
-     * @param  $object
+     * @param object $object
+     * @param array $options the form options
      * @return Symfony\Component\Form\Form
      */
     public function getForm($object, array $options = array())
@@ -1618,6 +1620,10 @@ abstract class Admin implements AdminInterface
     public function setRequest(Request $request)
     {
         $this->request = $request;
+
+        if ($request->get('uniqid')) {
+            $this->setUniqid($request->get('uniqid'));
+        }
     }
 
     public function getRequest()

+ 8 - 6
Controller/CRUDController.php

@@ -74,6 +74,12 @@ class CRUDController extends Controller
         $this->configure();
     }
 
+    /**
+     * Contextualize the admin class depends on the current request
+     *
+     * @throws \RuntimeException
+     * @return void
+     */
     public function configure()
     {
         $adminCode = $this->container->get('request')->get('_sonata_admin');
@@ -84,9 +90,7 @@ class CRUDController extends Controller
             throw new \RuntimeException(sprintf('Unable to find the admin class related to the current controller (%s)', get_class($this)));
         }
 
-        if ($this->container->get('request')->get('uniqid')) {
-            $this->admin->setUniqid($this->container->get('request')->get('uniqid'));
-        }
+        $this->admin->setRequest($this->container->get('request'));
 
         if ($this->admin->isChild()) {
             $this->admin->setCurrentChild(true);
@@ -170,9 +174,6 @@ class CRUDController extends Controller
      */
     public function editAction($id)
     {
-
-        $id = $this->get('request')->get($this->admin->getIdParameter());
-
         if ($id instanceof Form) {
             $object = $id->getData();
             $form   = $id;
@@ -180,6 +181,7 @@ class CRUDController extends Controller
             // todo : refactor the Form Creation
             $this->admin->getForm($object);
         } else {
+            $id = $this->get('request')->get($this->admin->getIdParameter());
             $object = $this->admin->getObject($id);
 
             if (!$object) {

+ 0 - 4
DependencyInjection/AddDependencyCallsPass.php

@@ -103,10 +103,6 @@ class AddDependencyCallsPass implements CompilerPassInterface
             $definition->addMethodCall('setDatagridBuilder', array(new Reference(sprintf('sonata_admin.builder.%s_datagrid', $manager_type))));
         }
 
-        if (!$definition->hasMethodCall('setRequest')) {
-            $definition->addMethodCall('setRequest', array(new Reference('request')));
-        }
-
         if (!$definition->hasMethodCall('setTranslator')) {
             $definition->addMethodCall('setTranslator', array(new Reference('translator')));
         }

+ 1 - 1
DependencyInjection/SonataAdminExtension.php

@@ -15,7 +15,7 @@ use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
 use Symfony\Component\DependencyInjection\ContainerBuilder;
 use Symfony\Component\DependencyInjection\Reference;
 use Symfony\Component\DependencyInjection\Definition;
-use Symfony\Component\DependencyInjection\Extension\Extension;
+use Symfony\Component\HttpKernel\DependencyInjection\Extension;
 
 use Symfony\Component\Config\FileLocator;
 use Symfony\Component\Config\Resource\FileResource;

+ 16 - 0
Form/FormMapper.php

@@ -114,4 +114,20 @@ class FormMapper
         $this->admin->removeFormFieldDescription($key);
         $this->form->remove($key);
     }
+
+    /**
+     * @return \Symfony\Component\Form\Form
+     */
+    public function getForm()
+    {
+        return $this->form;
+    }
+
+    /**
+     * @return \Sonata\AdminBundle\Admin\Admin
+     */
+    public function getAdmin()
+    {
+        return $this->admin;
+    }
 }