From 73ab6875217d3d137f6829cb26ddf1ee190eea2f Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 23 Jan 2011 10:23:33 +0100 Subject: [PATCH] moved ControllerResolver methods to HttpKernel (makes more sense) --- .../Controller/ControllerResolver.php | 137 ----------------- .../Bundle/FrameworkBundle/HttpKernel.php | 144 +++++++++++++++++- .../Resources/config/services.xml | 2 +- .../Resources/config/templating.xml | 2 +- .../Templating/Helper/ActionsHelper.php | 14 +- .../FrameworkBundle/Tests/HttpKernelTest.php | 6 +- .../Extension/TemplatingExtension.php | 2 +- .../Resources/config/toolbar.xml | 2 +- .../Tests/WebDebugToolbarListenerTest.php | 2 +- .../WebDebugToolbarListener.php | 10 +- 10 files changed, 162 insertions(+), 159 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerResolver.php b/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerResolver.php index c98c7481f6ad..0613038f4ec1 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerResolver.php +++ b/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerResolver.php @@ -13,9 +13,6 @@ use Symfony\Component\HttpKernel\Log\LoggerInterface; use Symfony\Component\HttpKernel\Controller\ControllerResolver as BaseControllerResolver; -use Symfony\Component\HttpKernel\HttpKernelInterface; -use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\EventDispatcher\Event; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser; use Symfony\Component\DependencyInjection\ContainerAwareInterface; @@ -29,7 +26,6 @@ class ControllerResolver extends BaseControllerResolver { protected $container; protected $parser; - protected $esiSupport; /** * Constructor. @@ -83,137 +79,4 @@ protected function createController($controller) return array($controller, $method); } - - /** - * Forwards the request to another controller. - * - * @param string $controller The controller name (a string like BlogBundle:Post:index) - * @param array $attributes An array of request attributes - * @param array $query An array of request query parameters - * - * @return Response A Response instance - */ - public function forward($controller, array $attributes = array(), array $query = array()) - { - $attributes['_controller'] = $controller; - $subRequest = $this->container->get('request')->duplicate($query, null, $attributes); - - return $this->container->get('kernel')->handle($subRequest, HttpKernelInterface::SUB_REQUEST); - } - - /** - * Renders a Controller and returns the Response content. - * - * Note that this method generates an esi:include tag only when both the standalone - * option is set to true and the request has ESI capability (@see Symfony\Component\HttpKernel\Cache\ESI). - * - * Available options: - * - * * attributes: An array of request attributes (only when the first argument is a controller) - * * query: An array of request query parameters (only when the first argument is a controller) - * * ignore_errors: true to return an empty string in case of an error - * * alt: an alternative controller to execute in case of an error (can be a controller, a URI, or an array with the controller, the attributes, and the query arguments) - * * standalone: whether to generate an esi:include tag or not when ESI is supported - * * comment: a comment to add when returning an esi:include tag - * - * @param string $controller A controller name to execute (a string like BlogBundle:Post:index), or a relative URI - * @param array $options An array of options - * - * @return string The Response content - */ - public function render($controller, array $options = array()) - { - $options = array_merge(array( - 'attributes' => array(), - 'query' => array(), - 'ignore_errors' => !$this->container->getParameter('kernel.debug'), - 'alt' => array(), - 'standalone' => false, - 'comment' => '', - ), $options); - - if (!is_array($options['alt'])) { - $options['alt'] = array($options['alt']); - } - - if (null === $this->esiSupport) { - $this->esiSupport = $this->container->has('esi') && $this->container->get('esi')->hasSurrogateEsiCapability($this->container->get('request')); - } - - if ($this->esiSupport && $options['standalone']) { - $uri = $this->generateInternalUri($controller, $options['attributes'], $options['query']); - - $alt = ''; - if ($options['alt']) { - $alt = $this->generateInternalUri($options['alt'][0], isset($options['alt'][1]) ? $options['alt'][1] : array(), isset($options['alt'][2]) ? $options['alt'][2] : array()); - } - - return $this->container->get('esi')->renderIncludeTag($uri, $alt, $options['ignore_errors'], $options['comment']); - } - - $request = $this->container->get('request'); - - // controller or URI? - if (0 === strpos($controller, '/')) { - $subRequest = Request::create($controller, 'get', array(), $request->cookies->all(), array(), $request->server->all()); - $subRequest->setSession($request->getSession()); - } else { - $options['attributes']['_controller'] = $controller; - $options['attributes']['_format'] = $request->getRequestFormat(); - $subRequest = $request->duplicate($options['query'], null, $options['attributes']); - } - - try { - $response = $this->container->get('kernel')->handle($subRequest, HttpKernelInterface::SUB_REQUEST, true); - - if (200 != $response->getStatusCode()) { - throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $request->getUri(), $response->getStatusCode())); - } - - return $response->getContent(); - } catch (\Exception $e) { - if ($options['alt']) { - $alt = $options['alt']; - unset($options['alt']); - $options['attributes'] = isset($alt[1]) ? $alt[1] : array(); - $options['query'] = isset($alt[2]) ? $alt[2] : array(); - - return $this->render($alt[0], $options); - } - - if (!$options['ignore_errors']) { - throw $e; - } - } - } - - /** - * Generates an internal URI for a given controller. - * - * This method uses the "_internal" route, which should be available. - * - * @param string $controller A controller name to execute (a string like BlogBundle:Post:index), or a relative URI - * @param array $attributes An array of request attributes - * @param array $query An array of request query parameters - * - * @return string An internal URI - */ - public function generateInternalUri($controller, array $attributes = array(), array $query = array()) - { - if (0 === strpos($controller, '/')) { - return $controller; - } - - $uri = $this->container->get('router')->generate('_internal', array( - 'controller' => $controller, - 'path' => $attributes ? http_build_query($attributes) : 'none', - '_format' => $this->container->get('request')->getRequestFormat(), - ), true); - - if ($query) { - $uri = $uri.'?'.http_build_query($query); - } - - return $uri; - } } diff --git a/src/Symfony/Bundle/FrameworkBundle/HttpKernel.php b/src/Symfony/Bundle/FrameworkBundle/HttpKernel.php index 020546499d36..58e9cf008770 100644 --- a/src/Symfony/Bundle/FrameworkBundle/HttpKernel.php +++ b/src/Symfony/Bundle/FrameworkBundle/HttpKernel.php @@ -17,12 +17,17 @@ class HttpKernel extends BaseHttpKernel { protected $container; + protected $esiSupport; - public function __construct(ContainerInterface $container, BaseEventDispatcher $eventDispatcher, ControllerResolverInterface $controllerResolver) + public function __construct(ContainerInterface $container, ControllerResolverInterface $controllerResolver) { - parent::__construct($eventDispatcher, $controllerResolver); - $this->container = $container; + $this->resolver = $controllerResolver; + } + + public function setEventDispatcher(BaseEventDispatcher $dispatcher) + { + $this->dispatcher = $dispatcher; } public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true) @@ -42,4 +47,137 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ return $response; } + + /** + * Forwards the request to another controller. + * + * @param string $controller The controller name (a string like BlogBundle:Post:index) + * @param array $attributes An array of request attributes + * @param array $query An array of request query parameters + * + * @return Response A Response instance + */ + public function forward($controller, array $attributes = array(), array $query = array()) + { + $attributes['_controller'] = $controller; + $subRequest = $this->container->get('request')->duplicate($query, null, $attributes); + + return $this->handle($subRequest, HttpKernelInterface::SUB_REQUEST); + } + + /** + * Renders a Controller and returns the Response content. + * + * Note that this method generates an esi:include tag only when both the standalone + * option is set to true and the request has ESI capability (@see Symfony\Component\HttpKernel\Cache\ESI). + * + * Available options: + * + * * attributes: An array of request attributes (only when the first argument is a controller) + * * query: An array of request query parameters (only when the first argument is a controller) + * * ignore_errors: true to return an empty string in case of an error + * * alt: an alternative controller to execute in case of an error (can be a controller, a URI, or an array with the controller, the attributes, and the query arguments) + * * standalone: whether to generate an esi:include tag or not when ESI is supported + * * comment: a comment to add when returning an esi:include tag + * + * @param string $controller A controller name to execute (a string like BlogBundle:Post:index), or a relative URI + * @param array $options An array of options + * + * @return string The Response content + */ + public function render($controller, array $options = array()) + { + $options = array_merge(array( + 'attributes' => array(), + 'query' => array(), + 'ignore_errors' => !$this->container->getParameter('kernel.debug'), + 'alt' => array(), + 'standalone' => false, + 'comment' => '', + ), $options); + + if (!is_array($options['alt'])) { + $options['alt'] = array($options['alt']); + } + + if (null === $this->esiSupport) { + $this->esiSupport = $this->container->has('esi') && $this->container->get('esi')->hasSurrogateEsiCapability($this->container->get('request')); + } + + if ($this->esiSupport && $options['standalone']) { + $uri = $this->generateInternalUri($controller, $options['attributes'], $options['query']); + + $alt = ''; + if ($options['alt']) { + $alt = $this->generateInternalUri($options['alt'][0], isset($options['alt'][1]) ? $options['alt'][1] : array(), isset($options['alt'][2]) ? $options['alt'][2] : array()); + } + + return $this->container->get('esi')->renderIncludeTag($uri, $alt, $options['ignore_errors'], $options['comment']); + } + + $request = $this->container->get('request'); + + // controller or URI? + if (0 === strpos($controller, '/')) { + $subRequest = Request::create($controller, 'get', array(), $request->cookies->all(), array(), $request->server->all()); + $subRequest->setSession($request->getSession()); + } else { + $options['attributes']['_controller'] = $controller; + $options['attributes']['_format'] = $request->getRequestFormat(); + $subRequest = $request->duplicate($options['query'], null, $options['attributes']); + } + + try { + $response = $this->handle($subRequest, HttpKernelInterface::SUB_REQUEST, true); + + if (200 != $response->getStatusCode()) { + throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $request->getUri(), $response->getStatusCode())); + } + + return $response->getContent(); + } catch (\Exception $e) { + if ($options['alt']) { + $alt = $options['alt']; + unset($options['alt']); + $options['attributes'] = isset($alt[1]) ? $alt[1] : array(); + $options['query'] = isset($alt[2]) ? $alt[2] : array(); + + return $this->render($alt[0], $options); + } + + if (!$options['ignore_errors']) { + throw $e; + } + } + } + + /** + * Generates an internal URI for a given controller. + * + * This method uses the "_internal" route, which should be available. + * + * @param string $controller A controller name to execute (a string like BlogBundle:Post:index), or a relative URI + * @param array $attributes An array of request attributes + * @param array $query An array of request query parameters + * + * @return string An internal URI + */ + public function generateInternalUri($controller, array $attributes = array(), array $query = array()) + { + if (0 === strpos($controller, '/')) { + return $controller; + } + + $uri = $this->container->get('router')->generate('_internal', array( + 'controller' => $controller, + 'path' => $attributes ? http_build_query($attributes) : 'none', + '_format' => $this->container->get('request')->getRequestFormat(), + ), true); + + if ($query) { + $uri = $uri.'?'.http_build_query($query); + } + + return $uri; + } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/services.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/services.xml index 96665dca6af9..5115c4c6968e 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/services.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/services.xml @@ -23,8 +23,8 @@ - + diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/templating.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/templating.xml index fd86ef9800b3..bf72c34ddcb1 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/templating.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/templating.xml @@ -92,7 +92,7 @@ - + diff --git a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/ActionsHelper.php b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/ActionsHelper.php index bfe88b3e7d91..8cb48f508afc 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/ActionsHelper.php +++ b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/ActionsHelper.php @@ -12,7 +12,7 @@ namespace Symfony\Bundle\FrameworkBundle\Templating\Helper; use Symfony\Component\Templating\Helper\Helper; -use Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver; +use Symfony\Bundle\FrameworkBundle\HttpKernel; /** * ActionsHelper manages action inclusions. @@ -21,16 +21,16 @@ */ class ActionsHelper extends Helper { - protected $resolver; + protected $kernel; /** * Constructor. * - * @param Constructor $resolver A ControllerResolver instance + * @param HttpKernel $kernel A HttpKernel instance */ - public function __construct(ControllerResolver $resolver) + public function __construct(HttpKernel $kernel) { - $this->resolver = $resolver; + $this->kernel = $kernel; } /** @@ -53,7 +53,7 @@ public function output($controller, array $attributes = array(), array $options * @param array $attributes An array of request attributes * @param array $options An array of options * - * @see Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver::render() + * @see Symfony\Bundle\FrameworkBundle\HttpKernel::render() */ public function render($controller, array $attributes = array(), array $options = array()) { @@ -64,7 +64,7 @@ public function render($controller, array $attributes = array(), array $options $options['query'] = $options['query']; } - return $this->resolver->render($controller, $options); + return $this->kernel->render($controller, $options); } /** diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/HttpKernelTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/HttpKernelTest.php index d11366c01bad..e6d33ffac770 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/HttpKernelTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/HttpKernelTest.php @@ -38,7 +38,8 @@ public function testHandle($type) $dispatcher = new EventDispatcher(); $resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface'); - $kernel = new HttpKernel($container, $dispatcher, $resolver); + $kernel = new HttpKernel($container, $resolver); + $kernel->setEventDispatcher($dispatcher); $controller = function() use($expected) { @@ -86,7 +87,8 @@ public function testHandleRestoresThePreviousRequestOnException($type) $dispatcher = new EventDispatcher(); $resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface'); - $kernel = new HttpKernel($container, $dispatcher, $resolver); + $kernel = new HttpKernel($container, $resolver); + $kernel->setEventDispatcher($dispatcher); $controller = function() use ($expected) { diff --git a/src/Symfony/Bundle/TwigBundle/Extension/TemplatingExtension.php b/src/Symfony/Bundle/TwigBundle/Extension/TemplatingExtension.php index ec857a7d96fa..14a8bf2630f3 100644 --- a/src/Symfony/Bundle/TwigBundle/Extension/TemplatingExtension.php +++ b/src/Symfony/Bundle/TwigBundle/Extension/TemplatingExtension.php @@ -106,7 +106,7 @@ public function renderAction($controller, array $attributes = array(), array $op $options['query'] = $options['query']; } - return $this->container->get('controller_resolver')->render($controller, $options); + return $this->container->get('http_kernel')->render($controller, $options); } /** diff --git a/src/Symfony/Bundle/WebProfilerBundle/Resources/config/toolbar.xml b/src/Symfony/Bundle/WebProfilerBundle/Resources/config/toolbar.xml index 92bddcb8b0b6..e3c208fbaeb9 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Resources/config/toolbar.xml +++ b/src/Symfony/Bundle/WebProfilerBundle/Resources/config/toolbar.xml @@ -12,7 +12,7 @@ - + %debug.toolbar.intercept_redirects% diff --git a/src/Symfony/Bundle/WebProfilerBundle/Tests/WebDebugToolbarListenerTest.php b/src/Symfony/Bundle/WebProfilerBundle/Tests/WebDebugToolbarListenerTest.php index f9e52eecd163..cb2aeb5140ec 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Tests/WebDebugToolbarListenerTest.php +++ b/src/Symfony/Bundle/WebProfilerBundle/Tests/WebDebugToolbarListenerTest.php @@ -21,7 +21,7 @@ class WebDebugToolbarListenerTest extends \PHPUnit_Framework_TestCase */ public function testInjectToolbar($content, $expected) { - $resolver = $this->getMock('Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver', array(), array(), '', false); + $resolver = $this->getMock('Symfony\Bundle\FrameworkBundle\HttpKernel', array(), array(), '', false); $resolver->expects($this->any()) ->method('render') ->will($this->returnValue('WDT')); diff --git a/src/Symfony/Bundle/WebProfilerBundle/WebDebugToolbarListener.php b/src/Symfony/Bundle/WebProfilerBundle/WebDebugToolbarListener.php index d93d26754d41..84500f433df7 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/WebDebugToolbarListener.php +++ b/src/Symfony/Bundle/WebProfilerBundle/WebDebugToolbarListener.php @@ -15,8 +15,8 @@ use Symfony\Component\EventDispatcher\Event; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Bundle\FrameworkBundle\HttpKernel; use Symfony\Component\HttpKernel\HttpKernelInterface; -use Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver; /** * WebDebugToolbarListener injects the Web Debug Toolbar. @@ -25,12 +25,12 @@ */ class WebDebugToolbarListener { - protected $resolver; + protected $kernel; protected $interceptRedirects; - public function __construct(ControllerResolver $resolver, $interceptRedirects = false) + public function __construct(HttpKernel $kernel, $interceptRedirects = false) { - $this->resolver = $resolver; + $this->kernel = $kernel; $this->interceptRedirects = $interceptRedirects; } @@ -92,7 +92,7 @@ protected function injectToolbar(Request $request, Response $response) $substrFunction = 'substr'; } - $toolbar = "\n".str_replace("\n", '', $this->resolver->render('WebProfilerBundle:Profiler:toolbar'))."\n"; + $toolbar = "\n".str_replace("\n", '', $this->kernel->render('WebProfilerBundle:Profiler:toolbar'))."\n"; $content = $response->getContent(); if (false === $pos = $posrFunction($content, '')) {