Browse Source

[FrameworkBundle] added ControllerInterface

A Controller must now implements ControllerInterface.

The BaseController can be used as the base class for Controllers.
The Controller class adds some proxy methods and an array access to the Container.
Fabien Potencier 14 years ago
parent
commit
9c07e46d91

+ 34 - 0
src/Symfony/Bundle/FrameworkBundle/Controller/BaseController.php

@@ -0,0 +1,34 @@
+<?php
+
+namespace Symfony\Bundle\FrameworkBundle\Controller;
+
+use Symfony\Component\DependencyInjection\ContainerInterface;
+
+/*
+ * This file is part of the Symfony framework.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+/**
+ * FrameworkBundle Controller gives you convenient access to all commonly needed services.
+ *
+ * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
+ */
+class BaseController implements ControllerInterface
+{
+    protected $container;
+
+    /**
+     * Sets the Container associated with this Controller.
+     *
+     * @param ContainerInterface $container A ContainerInterface instance
+     */
+    public function setContainer(ContainerInterface $container)
+    {
+        $this->container = $container;
+    }
+}

+ 2 - 15
src/Symfony/Bundle/FrameworkBundle/Controller.php

@@ -1,8 +1,7 @@
 <?php
 
-namespace Symfony\Bundle\FrameworkBundle;
+namespace Symfony\Bundle\FrameworkBundle\Controller;
 
-use Symfony\Component\DependencyInjection\ContainerInterface;
 use Symfony\Component\HttpFoundation\Response;
 
 /*
@@ -19,20 +18,8 @@ use Symfony\Component\HttpFoundation\Response;
  *
  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
  */
-class Controller implements \ArrayAccess
+class Controller extends BaseController implements \ArrayAccess
 {
-    protected $container;
-
-    /**
-     * Constructor.
-     *
-     * @param ContainerInterface $container A ContainerInterface instance
-     */
-    function __construct(ContainerInterface $container)
-    {
-        $this->container = $container;
-    }
-
     /**
      * Creates a Response instance.
      *

+ 29 - 0
src/Symfony/Bundle/FrameworkBundle/Controller/ControllerInterface.php

@@ -0,0 +1,29 @@
+<?php
+
+namespace Symfony\Bundle\FrameworkBundle\Controller;
+
+use Symfony\Component\DependencyInjection\ContainerInterface;
+
+/*
+ * This file is part of the Symfony framework.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+/**
+ * FrameworkBundle ControllerInterface is a simple interface for controllers.
+ *
+ * @author Fabien Potencier <fabien.potencier@symfony-project.com>
+ */
+interface ControllerInterface
+{
+    /**
+     * Sets the Container associated with this Controller.
+     *
+     * @param ContainerInterface $container A ContainerInterface instance
+     */
+    function setContainer(ContainerInterface $container);
+}

+ 7 - 1
src/Symfony/Bundle/FrameworkBundle/Controller/ControllerResolver.php

@@ -9,6 +9,7 @@ use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\EventDispatcher\Event;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 use Symfony\Bundle\FrameworkBundle\Controller\ControllerNameConverter;
+use Symfony\Bundle\FrameworkBundle\Controller\ControllerInterface;
 
 /*
  * This file is part of the Symfony framework.
@@ -66,7 +67,12 @@ class ControllerResolver extends BaseControllerResolver
             throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
         }
 
-        return array(new $class($this->container), $method);
+        $controller = new $class();
+        if ($controller instanceof ControllerInterface) {
+            $controller->setContainer($this->container);
+        }
+
+        return array($controller, $method);
     }
 
     /**

+ 1 - 1
src/Symfony/Bundle/FrameworkBundle/Controller/DefaultController.php

@@ -2,7 +2,7 @@
 
 namespace Symfony\Bundle\FrameworkBundle\Controller;
 
-use Symfony\Bundle\FrameworkBundle\Controller;
+use Symfony\Bundle\FrameworkBundle\Controller\Controller;
 use Symfony\Component\HttpFoundation\Response;
 
 /*

+ 1 - 1
src/Symfony/Bundle/FrameworkBundle/Controller/ExceptionController.php

@@ -2,7 +2,7 @@
 
 namespace Symfony\Bundle\FrameworkBundle\Controller;
 
-use Symfony\Bundle\FrameworkBundle\Controller;
+use Symfony\Bundle\FrameworkBundle\Controller\Controller;
 use Symfony\Bundle\FrameworkBundle\Debug\ExceptionManager;
 
 /*

+ 1 - 1
src/Symfony/Bundle/FrameworkBundle/Controller/InternalController.php

@@ -2,7 +2,7 @@
 
 namespace Symfony\Bundle\FrameworkBundle\Controller;
 
-use Symfony\Bundle\FrameworkBundle\Controller;
+use Symfony\Bundle\FrameworkBundle\Controller\Controller;
 use Symfony\Component\HttpFoundation\Response;
 
 /*

+ 1 - 1
src/Symfony/Bundle/FrameworkBundle/Controller/RedirectController.php

@@ -2,7 +2,7 @@
 
 namespace Symfony\Bundle\FrameworkBundle\Controller;
 
-use Symfony\Bundle\FrameworkBundle\Controller;
+use Symfony\Bundle\FrameworkBundle\Controller\Controller;
 use Symfony\Component\HttpFoundation\Response;
 
 /*

+ 1 - 1
src/Symfony/Bundle/FrameworkBundle/Controller/TemplateController.php

@@ -2,7 +2,7 @@
 
 namespace Symfony\Bundle\FrameworkBundle\Controller;
 
-use Symfony\Bundle\FrameworkBundle\Controller;
+use Symfony\Bundle\FrameworkBundle\Controller\Controller;
 use Symfony\Component\HttpFoundation\Response;
 
 /*

+ 3 - 1
src/Symfony/Bundle/FrameworkBundle/DependencyInjection/WebExtension.php

@@ -122,7 +122,9 @@ class WebExtension extends Extension
 
             'Symfony\\Component\\EventDispatcher\\Event',
 
-            'Symfony\\Bundle\\FrameworkBundle\\Controller',
+            'Symfony\\Bundle\\FrameworkBundle\\Controller\\ControllerInterface',
+            'Symfony\\Bundle\\FrameworkBundle\\Controller\\BaseController',
+            'Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller',
         ));
     }
 

+ 1 - 1
src/Symfony/Bundle/FrameworkBundle/Resources/skeleton/bundle/Controller/DefaultController.php

@@ -2,7 +2,7 @@
 
 namespace {{ namespace }}\{{ bundle }}\Controller;
 
-use Symfony\Bundle\FrameworkBundle\Controller;
+use Symfony\Bundle\FrameworkBundle\Controller\Controller;
 
 class DefaultController extends Controller
 {