[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.
This commit is contained in:
Fabien Potencier 2010-08-22 22:35:44 +02:00
parent fe78d5f0f0
commit 9c07e46d91
11 changed files with 81 additions and 23 deletions

View File

@ -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;
}
}

View File

@ -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.
*

View File

@ -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);
}

View File

@ -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);
}
/**

View File

@ -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;
/*

View File

@ -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;
/*

View File

@ -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;
/*

View File

@ -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;
/*

View File

@ -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;
/*

View File

@ -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',
));
}

View File

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