From 9c07e46d91b30236d89a98f738b7b7f3e2e5dfd1 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 22 Aug 2010 22:35:44 +0200 Subject: [PATCH] [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. --- .../Controller/BaseController.php | 34 +++++++++++++++++++ .../{ => Controller}/Controller.php | 17 ++-------- .../Controller/ControllerInterface.php | 29 ++++++++++++++++ .../Controller/ControllerResolver.php | 8 ++++- .../Controller/DefaultController.php | 2 +- .../Controller/ExceptionController.php | 2 +- .../Controller/InternalController.php | 2 +- .../Controller/RedirectController.php | 2 +- .../Controller/TemplateController.php | 2 +- .../DependencyInjection/WebExtension.php | 4 ++- .../bundle/Controller/DefaultController.php | 2 +- 11 files changed, 81 insertions(+), 23 deletions(-) create mode 100644 src/Symfony/Bundle/FrameworkBundle/Controller/BaseController.php rename src/Symfony/Bundle/FrameworkBundle/{ => Controller}/Controller.php (91%) create mode 100644 src/Symfony/Bundle/FrameworkBundle/Controller/ControllerInterface.php diff --git a/src/Symfony/Bundle/FrameworkBundle/Controller/BaseController.php b/src/Symfony/Bundle/FrameworkBundle/Controller/BaseController.php new file mode 100644 index 0000000000..c3f1dfd727 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Controller/BaseController.php @@ -0,0 +1,34 @@ + + * + * 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 + */ +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; + } +} diff --git a/src/Symfony/Bundle/FrameworkBundle/Controller.php b/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php similarity index 91% rename from src/Symfony/Bundle/FrameworkBundle/Controller.php rename to src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php index 9d882c9ddc..69d8eb9749 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Controller.php +++ b/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php @@ -1,8 +1,7 @@ */ -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. * diff --git a/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerInterface.php b/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerInterface.php new file mode 100644 index 0000000000..8395a30f67 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerInterface.php @@ -0,0 +1,29 @@ + + * + * 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 + */ +interface ControllerInterface +{ + /** + * Sets the Container associated with this Controller. + * + * @param ContainerInterface $container A ContainerInterface instance + */ + function setContainer(ContainerInterface $container); +} diff --git a/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerResolver.php b/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerResolver.php index ef3eae558e..287233ce42 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerResolver.php +++ b/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); } /** diff --git a/src/Symfony/Bundle/FrameworkBundle/Controller/DefaultController.php b/src/Symfony/Bundle/FrameworkBundle/Controller/DefaultController.php index 625145a2e6..d68861e40a 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Controller/DefaultController.php +++ b/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; /* diff --git a/src/Symfony/Bundle/FrameworkBundle/Controller/ExceptionController.php b/src/Symfony/Bundle/FrameworkBundle/Controller/ExceptionController.php index 48d620d6ff..19ffc082ad 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Controller/ExceptionController.php +++ b/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; /* diff --git a/src/Symfony/Bundle/FrameworkBundle/Controller/InternalController.php b/src/Symfony/Bundle/FrameworkBundle/Controller/InternalController.php index fd15e0e8e2..d18ba7bab7 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Controller/InternalController.php +++ b/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; /* diff --git a/src/Symfony/Bundle/FrameworkBundle/Controller/RedirectController.php b/src/Symfony/Bundle/FrameworkBundle/Controller/RedirectController.php index 2b34a46489..e3462da867 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Controller/RedirectController.php +++ b/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; /* diff --git a/src/Symfony/Bundle/FrameworkBundle/Controller/TemplateController.php b/src/Symfony/Bundle/FrameworkBundle/Controller/TemplateController.php index 8542dfe5e8..7f5755c265 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Controller/TemplateController.php +++ b/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; /* diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/WebExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/WebExtension.php index 6ade8de96f..7cbb089d8a 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/WebExtension.php +++ b/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', )); } diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/skeleton/bundle/Controller/DefaultController.php b/src/Symfony/Bundle/FrameworkBundle/Resources/skeleton/bundle/Controller/DefaultController.php index caaf043407..ad507ecfa6 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/skeleton/bundle/Controller/DefaultController.php +++ b/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 {