From b9f0e17e673b1085d5a7c0678749e5966c3ee345 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 22 Jan 2013 09:06:13 +0100 Subject: [PATCH 1/5] [HttpKernel] made the Request required when using rendering strategies The previous code allowed to pass null as a Request but that does not really make sense as rendering a sub-request can only happen from a master request. This was done to ease testing but that was a mistake. --- .../Extension/HttpKernelExtensionTest.php | 21 ++++++++++++------- .../HttpKernel/HttpContentRenderer.php | 10 ++++++--- .../DefaultRenderingStrategy.php | 21 +++++++------------ .../EsiRenderingStrategy.php | 4 ++-- .../GeneratorAwareRenderingStrategy.php | 6 ++---- .../HIncludeRenderingStrategy.php | 2 +- .../RenderingStrategyInterface.php | 2 +- .../Tests/HttpContentRendererTest.php | 14 ++++++++++++- .../DefaultRenderingStrategyTest.php | 10 ++++----- .../EsiRenderingStrategyTest.php | 2 +- .../GeneratorAwareRenderingStrategyTest.php | 10 ++++----- .../HIncludeRenderingStrategyTest.php | 14 ++++++------- 12 files changed, 65 insertions(+), 51 deletions(-) diff --git a/src/Symfony/Bridge/Twig/Tests/Extension/HttpKernelExtensionTest.php b/src/Symfony/Bridge/Twig/Tests/Extension/HttpKernelExtensionTest.php index 489d36bfb2051..f9c3f31d537fe 100644 --- a/src/Symfony/Bridge/Twig/Tests/Extension/HttpKernelExtensionTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Extension/HttpKernelExtensionTest.php @@ -13,6 +13,7 @@ use Symfony\Bridge\Twig\Extension\HttpKernelExtension; use Symfony\Bridge\Twig\Tests\TestCase; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\HttpContentRenderer; @@ -29,13 +30,6 @@ protected function setUp() } } - public function testRenderWithoutMasterRequest() - { - $kernel = $this->getHttpContentRenderer($this->returnValue(new Response('foo'))); - - $this->assertEquals('foo', $this->renderTemplate($kernel)); - } - /** * @expectedException \Twig_Error_Runtime */ @@ -56,7 +50,18 @@ protected function getHttpContentRenderer($return) $strategy->expects($this->once())->method('getName')->will($this->returnValue('default')); $strategy->expects($this->once())->method('render')->will($return); - return new HttpContentRenderer(array($strategy)); + // simulate a master request + $event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent')->disableOriginalConstructor()->getMock(); + $event + ->expects($this->once()) + ->method('getRequest') + ->will($this->returnValue(Request::create('/'))) + ; + + $renderer = new HttpContentRenderer(array($strategy)); + $renderer->onKernelRequest($event); + + return $renderer; } protected function renderTemplate(HttpContentRenderer $renderer, $template = '{{ render("foo") }}') diff --git a/src/Symfony/Component/HttpKernel/HttpContentRenderer.php b/src/Symfony/Component/HttpKernel/HttpContentRenderer.php index 34b18e13d7490..337cb6d11c771 100644 --- a/src/Symfony/Component/HttpKernel/HttpContentRenderer.php +++ b/src/Symfony/Component/HttpKernel/HttpContentRenderer.php @@ -23,7 +23,13 @@ /** * Renders a URI using different strategies. * + * This class handles sub-requests. The response content from the sub-request + * is then embedded into a master request. The handling of the sub-request + * is managed by rendering strategies. + * * @author Fabien Potencier + * + * @see RenderingStrategyInterface */ class HttpContentRenderer implements EventSubscriberInterface { @@ -103,9 +109,7 @@ public function render($uri, $strategy = 'default', array $options = array()) throw new \InvalidArgumentException(sprintf('The "%s" rendering strategy does not exist.', $strategy)); } - $request = $this->requests ? $this->requests[0] : null; - - return $this->deliver($this->strategies[$strategy]->render($uri, $request, $options)); + return $this->deliver($this->strategies[$strategy]->render($uri, $this->requests[0], $options)); } /** diff --git a/src/Symfony/Component/HttpKernel/RenderingStrategy/DefaultRenderingStrategy.php b/src/Symfony/Component/HttpKernel/RenderingStrategy/DefaultRenderingStrategy.php index e1c737a05dc63..058c82fe62f9a 100644 --- a/src/Symfony/Component/HttpKernel/RenderingStrategy/DefaultRenderingStrategy.php +++ b/src/Symfony/Component/HttpKernel/RenderingStrategy/DefaultRenderingStrategy.php @@ -42,7 +42,7 @@ public function __construct(HttpKernelInterface $kernel) * * * alt: an alternative URI to render in case of an error */ - public function render($uri, Request $request = null, array $options = array()) + public function render($uri, Request $request, array $options = array()) { if ($uri instanceof ControllerReference) { $uri = $this->generateProxyUri($uri, $request); @@ -74,21 +74,16 @@ public function render($uri, Request $request = null, array $options = array()) } } - protected function createSubRequest($uri, Request $request = null) + protected function createSubRequest($uri, Request $request) { - if (null !== $request) { - $cookies = $request->cookies->all(); - $server = $request->server->all(); - - // the sub-request is internal - $server['REMOTE_ADDR'] = '127.0.0.1'; - } else { - $cookies = array(); - $server = array(); - } + $cookies = $request->cookies->all(); + $server = $request->server->all(); + + // the sub-request is internal + $server['REMOTE_ADDR'] = '127.0.0.1'; $subRequest = Request::create($uri, 'get', array(), $cookies, array(), $server); - if (null !== $request && $session = $request->getSession()) { + if ($session = $request->getSession()) { $subRequest->setSession($session); } diff --git a/src/Symfony/Component/HttpKernel/RenderingStrategy/EsiRenderingStrategy.php b/src/Symfony/Component/HttpKernel/RenderingStrategy/EsiRenderingStrategy.php index e1ecc8a8cb1d3..945a388ac7453 100644 --- a/src/Symfony/Component/HttpKernel/RenderingStrategy/EsiRenderingStrategy.php +++ b/src/Symfony/Component/HttpKernel/RenderingStrategy/EsiRenderingStrategy.php @@ -55,9 +55,9 @@ public function __construct(Esi $esi, RenderingStrategyInterface $defaultStrateg * * @see Symfony\Component\HttpKernel\HttpCache\ESI */ - public function render($uri, Request $request = null, array $options = array()) + public function render($uri, Request $request, array $options = array()) { - if (null === $request || !$this->esi->hasSurrogateEsiCapability($request)) { + if (!$this->esi->hasSurrogateEsiCapability($request)) { return $this->defaultStrategy->render($uri, $request, $options); } diff --git a/src/Symfony/Component/HttpKernel/RenderingStrategy/GeneratorAwareRenderingStrategy.php b/src/Symfony/Component/HttpKernel/RenderingStrategy/GeneratorAwareRenderingStrategy.php index a5ba272f81e9b..57e0d55407c70 100644 --- a/src/Symfony/Component/HttpKernel/RenderingStrategy/GeneratorAwareRenderingStrategy.php +++ b/src/Symfony/Component/HttpKernel/RenderingStrategy/GeneratorAwareRenderingStrategy.php @@ -50,7 +50,7 @@ public function setUrlGenerator(UrlGeneratorInterface $generator) * @throws \LogicException when the _proxy route is not available * @throws \LogicException when there is no registered route generator */ - protected function generateProxyUri(ControllerReference $reference, Request $request = null) + protected function generateProxyUri(ControllerReference $reference, Request $request) { if (null === $this->generator) { throw new \LogicException('Unable to generate a proxy URL as there is no registered route generator.'); @@ -59,10 +59,8 @@ protected function generateProxyUri(ControllerReference $reference, Request $req if (isset($reference->attributes['_format'])) { $format = $reference->attributes['_format']; unset($reference->attributes['_format']); - } elseif (null !== $request) { - $format = $request->getRequestFormat(); } else { - $format = 'html'; + $format = $request->getRequestFormat(); } try { diff --git a/src/Symfony/Component/HttpKernel/RenderingStrategy/HIncludeRenderingStrategy.php b/src/Symfony/Component/HttpKernel/RenderingStrategy/HIncludeRenderingStrategy.php index cdcb6acee9109..fa13179180ec2 100644 --- a/src/Symfony/Component/HttpKernel/RenderingStrategy/HIncludeRenderingStrategy.php +++ b/src/Symfony/Component/HttpKernel/RenderingStrategy/HIncludeRenderingStrategy.php @@ -53,7 +53,7 @@ public function __construct($templating = null, UriSigner $signer = null, $globa * * * default: The default content (it can be a template name or the content) */ - public function render($uri, Request $request = null, array $options = array()) + public function render($uri, Request $request, array $options = array()) { if ($uri instanceof ControllerReference) { if (null === $this->signer) { diff --git a/src/Symfony/Component/HttpKernel/RenderingStrategy/RenderingStrategyInterface.php b/src/Symfony/Component/HttpKernel/RenderingStrategy/RenderingStrategyInterface.php index fd795445e0d43..b4f8b8e50b405 100644 --- a/src/Symfony/Component/HttpKernel/RenderingStrategy/RenderingStrategyInterface.php +++ b/src/Symfony/Component/HttpKernel/RenderingStrategy/RenderingStrategyInterface.php @@ -32,7 +32,7 @@ interface RenderingStrategyInterface * * @return Response A Response instance */ - public function render($uri, Request $request = null, array $options = array()); + public function render($uri, Request $request, array $options = array()); /** * Gets the name of the strategy. diff --git a/src/Symfony/Component/HttpKernel/Tests/HttpContentRendererTest.php b/src/Symfony/Component/HttpKernel/Tests/HttpContentRendererTest.php index f97bf51451ede..1051819697a40 100644 --- a/src/Symfony/Component/HttpKernel/Tests/HttpContentRendererTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/HttpContentRendererTest.php @@ -12,6 +12,7 @@ namespace Symfony\Component\HttpKernel\Tests; use Symfony\Component\HttpKernel\HttpContentRenderer; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; class HttpContentRendererTest extends \PHPUnit_Framework_TestCase @@ -34,6 +35,8 @@ public function testRenderWhenStrategyDoesNotExist() public function testRender() { + $request = Request::create('/'); + $strategy = $this->getMock('Symfony\Component\HttpKernel\RenderingStrategy\RenderingStrategyInterface'); $strategy ->expects($this->any()) @@ -43,13 +46,22 @@ public function testRender() $strategy ->expects($this->any()) ->method('render') - ->with('/', null, array('foo' => 'foo', 'ignore_errors' => true)) + ->with('/', $request, array('foo' => 'foo', 'ignore_errors' => true)) ->will($this->returnValue(new Response('foo'))) ; $renderer = new HttpContentRenderer(); $renderer->addStrategy($strategy); + // simulate a master request + $event = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseEvent')->disableOriginalConstructor()->getMock(); + $event + ->expects($this->once()) + ->method('getRequest') + ->will($this->returnValue(Request::create('/'))) + ; + $renderer->onKernelRequest($event); + $this->assertEquals('foo', $renderer->render('/', 'foo', array('foo' => 'foo'))); } diff --git a/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/DefaultRenderingStrategyTest.php b/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/DefaultRenderingStrategyTest.php index 8a208f48e389f..2a6d9ab7b3363 100644 --- a/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/DefaultRenderingStrategyTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/DefaultRenderingStrategyTest.php @@ -35,7 +35,7 @@ public function testRender() { $strategy = new DefaultRenderingStrategy($this->getKernel($this->returnValue(new Response('foo')))); - $this->assertEquals('foo', $strategy->render('/')->getContent()); + $this->assertEquals('foo', $strategy->render('/', Request::create('/'))->getContent()); } public function testRenderWithControllerReference() @@ -43,7 +43,7 @@ public function testRenderWithControllerReference() $strategy = new DefaultRenderingStrategy($this->getKernel($this->returnValue(new Response('foo')))); $strategy->setUrlGenerator($this->getUrlGenerator()); - $this->assertEquals('foo', $strategy->render(new ControllerReference('main_controller', array(), array()))->getContent()); + $this->assertEquals('foo', $strategy->render(new ControllerReference('main_controller', array(), array()), Request::create('/'))->getContent()); } /** @@ -53,14 +53,14 @@ public function testRenderExceptionNoIgnoreErrors() { $strategy = new DefaultRenderingStrategy($this->getKernel($this->throwException(new \RuntimeException('foo')))); - $this->assertEquals('foo', $strategy->render('/')->getContent()); + $this->assertEquals('foo', $strategy->render('/', Request::create('/'))->getContent()); } public function testRenderExceptionIgnoreErrors() { $strategy = new DefaultRenderingStrategy($this->getKernel($this->throwException(new \RuntimeException('foo')))); - $this->assertEmpty($strategy->render('/', null, array('ignore_errors' => true))->getContent()); + $this->assertEmpty($strategy->render('/', Request::create('/'), array('ignore_errors' => true))->getContent()); } public function testRenderExceptionIgnoreErrorsWithAlt() @@ -70,7 +70,7 @@ public function testRenderExceptionIgnoreErrorsWithAlt() $this->returnValue(new Response('bar')) ))); - $this->assertEquals('bar', $strategy->render('/', null, array('ignore_errors' => true, 'alt' => '/foo'))->getContent()); + $this->assertEquals('bar', $strategy->render('/', Request::create('/'), array('ignore_errors' => true, 'alt' => '/foo'))->getContent()); } private function getKernel($returnValue) diff --git a/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/EsiRenderingStrategyTest.php b/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/EsiRenderingStrategyTest.php index cea8aeee5d40a..90241a7616a71 100644 --- a/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/EsiRenderingStrategyTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/EsiRenderingStrategyTest.php @@ -32,7 +32,7 @@ protected function setUp() public function testRenderFallbackToDefaultStrategyIfNoRequest() { $strategy = new EsiRenderingStrategy(new Esi(), $this->getDefaultStrategy(true)); - $strategy->render('/'); + $strategy->render('/', Request::create('/')); } public function testRenderFallbackToDefaultStrategyIfEsiNotSupported() diff --git a/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/GeneratorAwareRenderingStrategyTest.php b/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/GeneratorAwareRenderingStrategyTest.php index 387ab3e2a0f20..8d7a0b54b1a5f 100644 --- a/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/GeneratorAwareRenderingStrategyTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/GeneratorAwareRenderingStrategyTest.php @@ -32,7 +32,7 @@ protected function setUp() public function testGenerateProxyUriWithNoGenerator() { $strategy = new Strategy(); - $strategy->doGenerateProxyUri(new ControllerReference('controller', array(), array())); + $strategy->doGenerateProxyUri(new ControllerReference('controller', array(), array()), Request::create('/')); } /** @@ -49,7 +49,7 @@ public function testGenerateProxyUriWhenRouteNotFound() $strategy = new Strategy(); $strategy->setUrlGenerator($generator); - $strategy->doGenerateProxyUri(new ControllerReference('controller', array(), array())); + $strategy->doGenerateProxyUri(new ControllerReference('controller', array(), array()), Request::create('/')); } /** @@ -57,7 +57,7 @@ public function testGenerateProxyUriWhenRouteNotFound() */ public function testGenerateProxyUri($uri, $controller) { - $this->assertEquals($uri, $this->getStrategy()->doGenerateProxyUri($controller)); + $this->assertEquals($uri, $this->getStrategy()->doGenerateProxyUri($controller, Request::create('/'))); } public function getGeneratorProxyUriData() @@ -91,10 +91,10 @@ private function getStrategy() class Strategy extends GeneratorAwareRenderingStrategy { - public function render($uri, Request $request = null, array $options = array()) {} + public function render($uri, Request $request, array $options = array()) {} public function getName() {} - public function doGenerateProxyUri(ControllerReference $reference, Request $request = null) + public function doGenerateProxyUri(ControllerReference $reference, Request $request) { return parent::generateProxyUri($reference, $request); } diff --git a/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/HIncludeRenderingStrategyTest.php b/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/HIncludeRenderingStrategyTest.php index 8e81b3be2d415..c78f2d153b5d9 100644 --- a/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/HIncludeRenderingStrategyTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/HIncludeRenderingStrategyTest.php @@ -35,37 +35,37 @@ protected function setUp() public function testRenderExceptionWhenControllerAndNoSigner() { $strategy = new HIncludeRenderingStrategy(); - $strategy->render(new ControllerReference('main_controller', array(), array())); + $strategy->render(new ControllerReference('main_controller', array(), array()), Request::create('/')); } public function testRenderWithControllerAndSigner() { $strategy = new HIncludeRenderingStrategy(null, new UriSigner('foo')); $strategy->setUrlGenerator($this->getUrlGenerator()); - $this->assertEquals('', $strategy->render(new ControllerReference('main_controller', array(), array()))->getContent()); + $this->assertEquals('', $strategy->render(new ControllerReference('main_controller', array(), array()), Request::create('/'))->getContent()); } public function testRenderWithUri() { $strategy = new HIncludeRenderingStrategy(); - $this->assertEquals('', $strategy->render('/foo')->getContent()); + $this->assertEquals('', $strategy->render('/foo', Request::create('/'))->getContent()); $strategy = new HIncludeRenderingStrategy(null, new UriSigner('foo')); - $this->assertEquals('', $strategy->render('/foo')->getContent()); + $this->assertEquals('', $strategy->render('/foo', Request::create('/'))->getContent()); } public function testRenderWhithDefault() { // only default $strategy = new HIncludeRenderingStrategy(); - $this->assertEquals('default', $strategy->render('/foo', null, array('default' => 'default'))->getContent()); + $this->assertEquals('default', $strategy->render('/foo', Request::create('/'), array('default' => 'default'))->getContent()); // only global default $strategy = new HIncludeRenderingStrategy(null, null, 'global_default'); - $this->assertEquals('global_default', $strategy->render('/foo', null, array())->getContent()); + $this->assertEquals('global_default', $strategy->render('/foo', Request::create('/'), array())->getContent()); // global default and default $strategy = new HIncludeRenderingStrategy(null, null, 'global_default'); - $this->assertEquals('default', $strategy->render('/foo', null, array('default' => 'default'))->getContent()); + $this->assertEquals('default', $strategy->render('/foo', Request::create('/'), array('default' => 'default'))->getContent()); } } From ad8289369128fc1e72f9f8f11b738679ed35a6de Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 22 Jan 2013 09:30:21 +0100 Subject: [PATCH 2/5] removed the need for a proxy route for rendering strategies --- .../Resources/config/content_generator.xml | 2 - .../FrameworkBundle/Resources/config/esi.xml | 1 - .../Resources/config/routing/proxy.xml | 10 -- .../EventListener/RouterProxyListener.php | 4 +- .../DefaultRenderingStrategy.php | 2 +- .../EsiRenderingStrategy.php | 2 +- .../GeneratorAwareRenderingStrategy.php | 82 -------------- .../HIncludeRenderingStrategy.php | 2 +- .../ProxyAwareRenderingStrategy.php | 44 ++++++++ .../EventListener/RouterProxyListenerTest.php | 38 ++----- .../AbstractRenderingStrategyTest.php | 29 ----- .../DefaultRenderingStrategyTest.php | 3 +- .../EsiRenderingStrategyTest.php | 9 +- .../GeneratorAwareRenderingStrategyTest.php | 101 ------------------ .../HIncludeRenderingStrategyTest.php | 10 +- .../ProxyAwareRenderingStrategyTest.php | 63 +++++++++++ 16 files changed, 128 insertions(+), 274 deletions(-) delete mode 100644 src/Symfony/Bundle/FrameworkBundle/Resources/config/routing/proxy.xml delete mode 100644 src/Symfony/Component/HttpKernel/RenderingStrategy/GeneratorAwareRenderingStrategy.php create mode 100644 src/Symfony/Component/HttpKernel/RenderingStrategy/ProxyAwareRenderingStrategy.php delete mode 100644 src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/AbstractRenderingStrategyTest.php delete mode 100644 src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/GeneratorAwareRenderingStrategyTest.php create mode 100644 src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/ProxyAwareRenderingStrategyTest.php diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/content_generator.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/content_generator.xml index 332cf0de38804..5bc6f2e3606d5 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/content_generator.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/content_generator.xml @@ -22,7 +22,6 @@ - @@ -30,7 +29,6 @@ %http_content_renderer.strategy.hinclude.global_template% - diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/esi.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/esi.xml index 0c4a271863a8d..8b390da4f1e25 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/esi.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/esi.xml @@ -22,7 +22,6 @@ - diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/routing/proxy.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/routing/proxy.xml deleted file mode 100644 index ab792c29cc1e2..0000000000000 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/routing/proxy.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - .+ - - diff --git a/src/Symfony/Component/HttpKernel/EventListener/RouterProxyListener.php b/src/Symfony/Component/HttpKernel/EventListener/RouterProxyListener.php index b88350c20fa32..572d0a145654e 100644 --- a/src/Symfony/Component/HttpKernel/EventListener/RouterProxyListener.php +++ b/src/Symfony/Component/HttpKernel/EventListener/RouterProxyListener.php @@ -47,7 +47,7 @@ public function onKernelRequest(GetResponseEvent $event) { $request = $event->getRequest(); - if ('_proxy' !== $request->attributes->get('_route')) { + if ('/_proxy' !== rawurldecode($request->getPathInfo())) { return; } @@ -92,7 +92,7 @@ protected function getLocalIpAddresses() public static function getSubscribedEvents() { return array( - KernelEvents::REQUEST => array(array('onKernelRequest', 16)), + KernelEvents::REQUEST => array(array('onKernelRequest', 48)), ); } } diff --git a/src/Symfony/Component/HttpKernel/RenderingStrategy/DefaultRenderingStrategy.php b/src/Symfony/Component/HttpKernel/RenderingStrategy/DefaultRenderingStrategy.php index 058c82fe62f9a..5a3427e05e3c8 100644 --- a/src/Symfony/Component/HttpKernel/RenderingStrategy/DefaultRenderingStrategy.php +++ b/src/Symfony/Component/HttpKernel/RenderingStrategy/DefaultRenderingStrategy.php @@ -21,7 +21,7 @@ * * @author Fabien Potencier */ -class DefaultRenderingStrategy extends GeneratorAwareRenderingStrategy +class DefaultRenderingStrategy extends ProxyAwareRenderingStrategy { private $kernel; diff --git a/src/Symfony/Component/HttpKernel/RenderingStrategy/EsiRenderingStrategy.php b/src/Symfony/Component/HttpKernel/RenderingStrategy/EsiRenderingStrategy.php index 945a388ac7453..195066d004945 100644 --- a/src/Symfony/Component/HttpKernel/RenderingStrategy/EsiRenderingStrategy.php +++ b/src/Symfony/Component/HttpKernel/RenderingStrategy/EsiRenderingStrategy.php @@ -21,7 +21,7 @@ * * @author Fabien Potencier */ -class EsiRenderingStrategy extends GeneratorAwareRenderingStrategy +class EsiRenderingStrategy extends ProxyAwareRenderingStrategy { private $esi; private $defaultStrategy; diff --git a/src/Symfony/Component/HttpKernel/RenderingStrategy/GeneratorAwareRenderingStrategy.php b/src/Symfony/Component/HttpKernel/RenderingStrategy/GeneratorAwareRenderingStrategy.php deleted file mode 100644 index 57e0d55407c70..0000000000000 --- a/src/Symfony/Component/HttpKernel/RenderingStrategy/GeneratorAwareRenderingStrategy.php +++ /dev/null @@ -1,82 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\HttpKernel\RenderingStrategy; - -use Symfony\Component\HttpKernel\Controller\ControllerReference; -use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\Routing\Generator\UrlGeneratorInterface; -use Symfony\Component\Routing\Exception\RouteNotFoundException; - -/** - * Adds the possibility to generate a proxy URI for a given Controller. - * - * @author Fabien Potencier - */ -abstract class GeneratorAwareRenderingStrategy implements RenderingStrategyInterface -{ - protected $generator; - - /** - * Sets a URL generator to use for proxy URIs generation. - * - * @param UrlGeneratorInterface $generator An UrlGeneratorInterface instance - */ - public function setUrlGenerator(UrlGeneratorInterface $generator) - { - $this->generator = $generator; - } - - /** - * Generates a proxy URI for a given controller. - * - * This method only works when using the Symfony Routing component and - * if a "_proxy" route is defined with a {_controller} and {_format} - * placeholders. - * - * @param ControllerReference $reference A ControllerReference instance - * @param Request $request A Request instance - * - * @return string A proxy URI - * - * @throws \LogicException when the _proxy route is not available - * @throws \LogicException when there is no registered route generator - */ - protected function generateProxyUri(ControllerReference $reference, Request $request) - { - if (null === $this->generator) { - throw new \LogicException('Unable to generate a proxy URL as there is no registered route generator.'); - } - - if (isset($reference->attributes['_format'])) { - $format = $reference->attributes['_format']; - unset($reference->attributes['_format']); - } else { - $format = $request->getRequestFormat(); - } - - try { - $uri = $this->generator->generate('_proxy', array('_controller' => $reference->controller, '_format' => $format), UrlGeneratorInterface::ABSOLUTE_URL); - } catch (RouteNotFoundException $e) { - throw new \LogicException('Unable to generate a proxy URL as the "_proxy" route is not registered.', 0, $e); - } - - if ($path = http_build_query($reference->attributes, '', '&')) { - $reference->query['path'] = $path; - } - - if ($qs = http_build_query($reference->query, '', '&')) { - $uri .= '?'.$qs; - } - - return $uri; - } -} diff --git a/src/Symfony/Component/HttpKernel/RenderingStrategy/HIncludeRenderingStrategy.php b/src/Symfony/Component/HttpKernel/RenderingStrategy/HIncludeRenderingStrategy.php index fa13179180ec2..07a695635b17a 100644 --- a/src/Symfony/Component/HttpKernel/RenderingStrategy/HIncludeRenderingStrategy.php +++ b/src/Symfony/Component/HttpKernel/RenderingStrategy/HIncludeRenderingStrategy.php @@ -22,7 +22,7 @@ * * @author Fabien Potencier */ -class HIncludeRenderingStrategy extends GeneratorAwareRenderingStrategy +class HIncludeRenderingStrategy extends ProxyAwareRenderingStrategy { private $templating; private $globalDefaultTemplate; diff --git a/src/Symfony/Component/HttpKernel/RenderingStrategy/ProxyAwareRenderingStrategy.php b/src/Symfony/Component/HttpKernel/RenderingStrategy/ProxyAwareRenderingStrategy.php new file mode 100644 index 0000000000000..1067c30d13c4f --- /dev/null +++ b/src/Symfony/Component/HttpKernel/RenderingStrategy/ProxyAwareRenderingStrategy.php @@ -0,0 +1,44 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpKernel\RenderingStrategy; + +use Symfony\Component\HttpKernel\Controller\ControllerReference; +use Symfony\Component\HttpFoundation\Request; + +/** + * Adds the possibility to generate a proxy URI for a given Controller. + * + * @author Fabien Potencier + */ +abstract class ProxyAwareRenderingStrategy implements RenderingStrategyInterface +{ + /** + * Generates a proxy URI for a given controller. + * + * @param ControllerReference $reference A ControllerReference instance + * @param Request $request A Request instance + * + * @return string A proxy URI + */ + protected function generateProxyUri(ControllerReference $reference, Request $request) + { + if (!isset($reference->attributes['_format'])) { + $reference->attributes['_format'] = $request->getRequestFormat(); + } + + $reference->attributes['_controller'] = $reference->controller; + + $reference->query['path'] = http_build_query($reference->attributes, '', '&'); + + return $request->getUriForPath('/_proxy?'.http_build_query($reference->query, '', '&')); + } +} diff --git a/src/Symfony/Component/HttpKernel/Tests/EventListener/RouterProxyListenerTest.php b/src/Symfony/Component/HttpKernel/Tests/EventListener/RouterProxyListenerTest.php index b0e091d50414a..352e0ab1e0790 100644 --- a/src/Symfony/Component/HttpKernel/Tests/EventListener/RouterProxyListenerTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/EventListener/RouterProxyListenerTest.php @@ -28,10 +28,10 @@ protected function setUp() public function testOnlyTriggeredOnProxyRoute() { - $request = Request::create('http://example.com/foo?path=foo%3D=bar'); + $request = Request::create('http://example.com/foo?path=foo%3Dbar%26_controller%3Dfoo'); $listener = new RouterProxyListener(new UriSigner('foo')); - $event = $this->createGetResponseEvent($request, 'foobar'); + $event = $this->createGetResponseEvent($request); $expected = $request->attributes->all(); @@ -46,7 +46,7 @@ public function testOnlyTriggeredOnProxyRoute() */ public function testAccessDeniedWithNonSafeMethods() { - $request = Request::create('http://example.com/foo', 'POST'); + $request = Request::create('http://example.com/_proxy', 'POST'); $listener = new RouterProxyListener(new UriSigner('foo')); $event = $this->createGetResponseEvent($request); @@ -59,7 +59,7 @@ public function testAccessDeniedWithNonSafeMethods() */ public function testAccessDeniedWithNonLocalIps() { - $request = Request::create('http://example.com/foo', 'GET', array(), array(), array(), array('REMOTE_ADDR' => '10.0.0.1')); + $request = Request::create('http://example.com/_proxy', 'GET', array(), array(), array(), array('REMOTE_ADDR' => '10.0.0.1')); $listener = new RouterProxyListener(new UriSigner('foo')); $event = $this->createGetResponseEvent($request); @@ -72,7 +72,7 @@ public function testAccessDeniedWithNonLocalIps() */ public function testAccessDeniedWithWrongSignature() { - $request = Request::create('http://example.com/foo', 'GET', array(), array(), array(), array('REMOTE_ADDR' => '10.0.0.1')); + $request = Request::create('http://example.com/_proxy', 'GET', array(), array(), array(), array('REMOTE_ADDR' => '10.0.0.1')); $listener = new RouterProxyListener(new UriSigner('foo')); $event = $this->createGetResponseEvent($request); @@ -80,40 +80,22 @@ public function testAccessDeniedWithWrongSignature() $listener->onKernelRequest($event); } - public function testWithSignatureAndNoPath() + public function testWithSignature() { $signer = new UriSigner('foo'); - $request = Request::create($signer->sign('http://example.com/foo'), 'GET', array(), array(), array(), array('REMOTE_ADDR' => '10.0.0.1')); + $request = Request::create($signer->sign('http://example.com/_proxy?path=foo%3Dbar%26_controller%3Dfoo'), 'GET', array(), array(), array(), array('REMOTE_ADDR' => '10.0.0.1')); $listener = new RouterProxyListener($signer); $event = $this->createGetResponseEvent($request); $listener->onKernelRequest($event); - $this->assertEquals(array('foo' => 'foo'), $request->attributes->get('_route_params')); + $this->assertEquals(array('foo' => 'bar', '_controller' => 'foo'), $request->attributes->get('_route_params')); $this->assertFalse($request->query->has('path')); } - public function testWithSignatureAndPath() + private function createGetResponseEvent(Request $request) { - $signer = new UriSigner('foo'); - $request = Request::create($signer->sign('http://example.com/foo?path=bar%3Dbar'), 'GET', array(), array(), array(), array('REMOTE_ADDR' => '10.0.0.1')); - - $listener = new RouterProxyListener($signer); - $event = $this->createGetResponseEvent($request); - - $listener->onKernelRequest($event); - - $this->assertEquals(array('foo' => 'foo', 'bar' => 'bar'), $request->attributes->get('_route_params')); - $this->assertFalse($request->query->has('path')); - } - - private function createGetResponseEvent(Request $request, $route = '_proxy') - { - $kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'); - $request->attributes->set('_route', $route); - $request->attributes->set('_route_params', array('foo' => 'foo')); - - return new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST); + return new GetResponseEvent($this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'), $request, HttpKernelInterface::MASTER_REQUEST); } } diff --git a/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/AbstractRenderingStrategyTest.php b/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/AbstractRenderingStrategyTest.php deleted file mode 100644 index ae3a07f2cce10..0000000000000 --- a/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/AbstractRenderingStrategyTest.php +++ /dev/null @@ -1,29 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\HttpKernel\Tests\RenderingStrategy; - -abstract class AbstractRenderingStrategyTest extends \PHPUnit_Framework_TestCase -{ - protected function getUrlGenerator() - { - $generator = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface'); - $generator - ->expects($this->any()) - ->method('generate') - ->will($this->returnCallback(function ($name, $parameters, $referenceType) { - return '/'.$parameters['_controller'].'.'.$parameters['_format']; - })) - ; - - return $generator; - } -} diff --git a/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/DefaultRenderingStrategyTest.php b/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/DefaultRenderingStrategyTest.php index 2a6d9ab7b3363..5d1d4c92d6e32 100644 --- a/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/DefaultRenderingStrategyTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/DefaultRenderingStrategyTest.php @@ -18,7 +18,7 @@ use Symfony\Component\HttpFoundation\Response; use Symfony\Component\EventDispatcher\EventDispatcher; -class DefaultRenderingStrategyTest extends AbstractRenderingStrategyTest +class DefaultRenderingStrategyTest extends \PHPUnit_Framework_TestCase { protected function setUp() { @@ -41,7 +41,6 @@ public function testRender() public function testRenderWithControllerReference() { $strategy = new DefaultRenderingStrategy($this->getKernel($this->returnValue(new Response('foo')))); - $strategy->setUrlGenerator($this->getUrlGenerator()); $this->assertEquals('foo', $strategy->render(new ControllerReference('main_controller', array(), array()), Request::create('/'))->getContent()); } diff --git a/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/EsiRenderingStrategyTest.php b/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/EsiRenderingStrategyTest.php index 90241a7616a71..bace5d6f98fb5 100644 --- a/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/EsiRenderingStrategyTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/EsiRenderingStrategyTest.php @@ -16,17 +16,13 @@ use Symfony\Component\HttpKernel\HttpCache\Esi; use Symfony\Component\HttpFoundation\Request; -class EsiRenderingStrategyTest extends AbstractRenderingStrategyTest +class EsiRenderingStrategyTest extends \PHPUnit_Framework_TestCase { protected function setUp() { if (!class_exists('Symfony\Component\HttpFoundation\Request')) { $this->markTestSkipped('The "HttpFoundation" component is not available'); } - - if (!interface_exists('Symfony\Component\Routing\Generator\UrlGeneratorInterface')) { - $this->markTestSkipped('The "Routing" component is not available'); - } } public function testRenderFallbackToDefaultStrategyIfNoRequest() @@ -44,7 +40,6 @@ public function testRenderFallbackToDefaultStrategyIfEsiNotSupported() public function testRender() { $strategy = new EsiRenderingStrategy(new Esi(), $this->getDefaultStrategy()); - $strategy->setUrlGenerator($this->getUrlGenerator()); $request = Request::create('/'); $request->headers->set('Surrogate-Capability', 'ESI/1.0'); @@ -52,7 +47,7 @@ public function testRender() $this->assertEquals('', $strategy->render('/', $request)->getContent()); $this->assertEquals("\n", $strategy->render('/', $request, array('comment' => 'This is a comment'))->getContent()); $this->assertEquals('', $strategy->render('/', $request, array('alt' => 'foo'))->getContent()); - $this->assertEquals('', $strategy->render(new ControllerReference('main_controller', array(), array()), $request, array('alt' => new ControllerReference('alt_controller', array(), array())))->getContent()); + $this->assertEquals('', $strategy->render(new ControllerReference('main_controller', array(), array()), $request, array('alt' => new ControllerReference('alt_controller', array(), array())))->getContent()); } private function getDefaultStrategy($called = false) diff --git a/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/GeneratorAwareRenderingStrategyTest.php b/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/GeneratorAwareRenderingStrategyTest.php deleted file mode 100644 index 8d7a0b54b1a5f..0000000000000 --- a/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/GeneratorAwareRenderingStrategyTest.php +++ /dev/null @@ -1,101 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\HttpKernel\Tests\RenderingStrategy; - -use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpKernel\Controller\ControllerReference; -use Symfony\Component\HttpKernel\RenderingStrategy\GeneratorAwareRenderingStrategy; -use Symfony\Component\Routing\Generator\UrlGeneratorInterface; -use Symfony\Component\Routing\Exception\RouteNotFoundException; - -class GeneratorAwareRenderingStrategyTest extends AbstractRenderingStrategyTest -{ - protected function setUp() - { - if (!interface_exists('Symfony\Component\Routing\Generator\UrlGeneratorInterface')) { - $this->markTestSkipped('The "Routing" component is not available'); - } - } - - /** - * @expectedException \LogicException - */ - public function testGenerateProxyUriWithNoGenerator() - { - $strategy = new Strategy(); - $strategy->doGenerateProxyUri(new ControllerReference('controller', array(), array()), Request::create('/')); - } - - /** - * @expectedException \LogicException - */ - public function testGenerateProxyUriWhenRouteNotFound() - { - $generator = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface'); - $generator - ->expects($this->once()) - ->method('generate') - ->will($this->throwException(new RouteNotFoundException())) - ; - - $strategy = new Strategy(); - $strategy->setUrlGenerator($generator); - $strategy->doGenerateProxyUri(new ControllerReference('controller', array(), array()), Request::create('/')); - } - - /** - * @dataProvider getGeneratorProxyUriData - */ - public function testGenerateProxyUri($uri, $controller) - { - $this->assertEquals($uri, $this->getStrategy()->doGenerateProxyUri($controller, Request::create('/'))); - } - - public function getGeneratorProxyUriData() - { - return array( - array('/controller.html', new ControllerReference('controller', array(), array())), - array('/controller.xml', new ControllerReference('controller', array('_format' => 'xml'), array())), - array('/controller.json?path=foo%3Dfoo', new ControllerReference('controller', array('foo' => 'foo', '_format' => 'json'), array())), - array('/controller.html?bar=bar&path=foo%3Dfoo', new ControllerReference('controller', array('foo' => 'foo'), array('bar' => 'bar'))), - array('/controller.html?foo=foo', new ControllerReference('controller', array(), array('foo' => 'foo'))), - ); - } - - public function testGenerateProxyUriWithARequest() - { - $request = Request::create('/'); - $request->attributes->set('_format', 'json'); - $controller = new ControllerReference('controller', array(), array()); - - $this->assertEquals('/controller.json', $this->getStrategy()->doGenerateProxyUri($controller, $request)); - } - - private function getStrategy() - { - $strategy = new Strategy(); - $strategy->setUrlGenerator($this->getUrlGenerator()); - - return $strategy; - } -} - -class Strategy extends GeneratorAwareRenderingStrategy -{ - public function render($uri, Request $request, array $options = array()) {} - public function getName() {} - - public function doGenerateProxyUri(ControllerReference $reference, Request $request) - { - return parent::generateProxyUri($reference, $request); - } -} diff --git a/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/HIncludeRenderingStrategyTest.php b/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/HIncludeRenderingStrategyTest.php index c78f2d153b5d9..b00d16eb5c338 100644 --- a/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/HIncludeRenderingStrategyTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/HIncludeRenderingStrategyTest.php @@ -16,17 +16,13 @@ use Symfony\Component\HttpKernel\UriSigner; use Symfony\Component\HttpFoundation\Request; -class HIncludeRenderingStrategyTest extends AbstractRenderingStrategyTest +class HIncludeRenderingStrategyTest extends \PHPUnit_Framework_TestCase { protected function setUp() { if (!class_exists('Symfony\Component\HttpFoundation\Request')) { $this->markTestSkipped('The "HttpFoundation" component is not available'); } - - if (!interface_exists('Symfony\Component\Routing\Generator\UrlGeneratorInterface')) { - $this->markTestSkipped('The "Routing" component is not available'); - } } /** @@ -41,8 +37,8 @@ public function testRenderExceptionWhenControllerAndNoSigner() public function testRenderWithControllerAndSigner() { $strategy = new HIncludeRenderingStrategy(null, new UriSigner('foo')); - $strategy->setUrlGenerator($this->getUrlGenerator()); - $this->assertEquals('', $strategy->render(new ControllerReference('main_controller', array(), array()), Request::create('/'))->getContent()); + + $this->assertEquals('', $strategy->render(new ControllerReference('main_controller', array(), array()), Request::create('/'))->getContent()); } public function testRenderWithUri() diff --git a/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/ProxyAwareRenderingStrategyTest.php b/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/ProxyAwareRenderingStrategyTest.php new file mode 100644 index 0000000000000..f1e1dc9044509 --- /dev/null +++ b/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/ProxyAwareRenderingStrategyTest.php @@ -0,0 +1,63 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpKernel\Tests\RenderingStrategy; + +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpKernel\Controller\ControllerReference; +use Symfony\Component\HttpKernel\RenderingStrategy\ProxyAwareRenderingStrategy; + +class ProxyAwareRenderingStrategyTest extends \PHPUnit_Framework_TestCase +{ + /** + * @dataProvider getGenerateProxyUriData + */ + public function testGenerateProxyUri($uri, $controller) + { + $this->assertEquals($uri, $this->getStrategy()->doGenerateProxyUri($controller, Request::create('/'))); + } + + public function getGenerateProxyUriData() + { + return array( + array('http://localhost/_proxy?path=_format%3Dhtml%26_controller%3Dcontroller', new ControllerReference('controller', array(), array())), + array('http://localhost/_proxy?path=_format%3Dxml%26_controller%3Dcontroller', new ControllerReference('controller', array('_format' => 'xml'), array())), + array('http://localhost/_proxy?path=foo%3Dfoo%26_format%3Djson%26_controller%3Dcontroller', new ControllerReference('controller', array('foo' => 'foo', '_format' => 'json'), array())), + array('http://localhost/_proxy?bar=bar&path=foo%3Dfoo%26_format%3Dhtml%26_controller%3Dcontroller', new ControllerReference('controller', array('foo' => 'foo'), array('bar' => 'bar'))), + array('http://localhost/_proxy?foo=foo&path=_format%3Dhtml%26_controller%3Dcontroller', new ControllerReference('controller', array(), array('foo' => 'foo'))), + ); + } + + public function testGenerateProxyUriWithARequest() + { + $request = Request::create('/'); + $request->attributes->set('_format', 'json'); + $controller = new ControllerReference('controller', array(), array()); + + $this->assertEquals('http://localhost/_proxy?path=_format%3Djson%26_controller%3Dcontroller', $this->getStrategy()->doGenerateProxyUri($controller, $request)); + } + + private function getStrategy() + { + return new Strategy(); + } +} + +class Strategy extends ProxyAwareRenderingStrategy +{ + public function render($uri, Request $request, array $options = array()) {} + public function getName() {} + + public function doGenerateProxyUri(ControllerReference $reference, Request $request) + { + return parent::generateProxyUri($reference, $request); + } +} From 3193a90815e1ca16560c9d0328122145baf02899 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 22 Jan 2013 10:24:26 +0100 Subject: [PATCH 3/5] made the proxy path configurable --- .../DependencyInjection/Configuration.php | 16 ++++++++++++++++ .../DependencyInjection/FrameworkExtension.php | 18 ++++++++++++++++++ .../Resources/config/content_generator.xml | 10 +++------- .../FrameworkBundle/Resources/config/esi.xml | 1 + .../FrameworkBundle/Resources/config/proxy.xml | 18 ++++++++++++++++++ .../Resources/config/schema/symfony-1.0.xsd | 6 ++++++ .../EventListener/RouterProxyListener.php | 12 ++++++++++-- .../ProxyAwareRenderingStrategy.php | 17 ++++++++++++++++- 8 files changed, 88 insertions(+), 10 deletions(-) create mode 100644 src/Symfony/Bundle/FrameworkBundle/Resources/config/proxy.xml diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php index 70017d727baea..cd39e49842364 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php @@ -73,6 +73,7 @@ public function getConfigTreeBuilder() $this->addFormSection($rootNode); $this->addEsiSection($rootNode); + $this->addProxySection($rootNode); $this->addProfilerSection($rootNode); $this->addRouterSection($rootNode); $this->addSessionSection($rootNode); @@ -114,6 +115,21 @@ private function addEsiSection(ArrayNodeDefinition $rootNode) ; } + private function addProxySection(ArrayNodeDefinition $rootNode) + { + $rootNode + ->children() + ->arrayNode('proxy') + ->info('proxy configuration') + ->canBeDisabled() + ->children() + ->scalarNode('path')->defaultValue('/_proxy')->end() + ->end() + ->end() + ->end() + ; + } + private function addProfilerSection(ArrayNodeDefinition $rootNode) { $rootNode diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index 6423754059b24..0cd4afb37086c 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -94,6 +94,10 @@ public function load(array $configs, ContainerBuilder $container) $this->registerEsiConfiguration($config['esi'], $loader); } + if (isset($config['proxy'])) { + $this->registerProxyConfiguration($config['proxy'], $container, $loader); + } + if (isset($config['profiler'])) { $this->registerProfilerConfiguration($config['profiler'], $container, $loader); } @@ -184,6 +188,20 @@ private function registerEsiConfiguration(array $config, XmlFileLoader $loader) } } + /** + * Loads the proxy configuration. + * + * @param array $config A proxy configuration array + * @param XmlFileLoader $loader An XmlFileLoader instance + */ + private function registerProxyConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader) + { + if (!empty($config['enabled'])) { + $loader->load('proxy.xml'); + $container->setParameter('http_content_renderer.proxy_path', $config['path']); + } + } + /** * Loads the profiler configuration. * diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/content_generator.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/content_generator.xml index 5bc6f2e3606d5..2a276c8c21a9b 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/content_generator.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/content_generator.xml @@ -9,7 +9,7 @@ Symfony\Component\HttpKernel\RenderingStrategy\DefaultRenderingStrategy Symfony\Component\HttpKernel\RenderingStrategy\HIncludeRenderingStrategy - Symfony\Component\HttpKernel\EventListener\RouterProxyListener + /_proxy @@ -22,6 +22,7 @@ + %http_content_renderer.proxy_path% @@ -29,12 +30,7 @@ %http_content_renderer.strategy.hinclude.global_template% - - - - - - + %http_content_renderer.proxy_path% diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/esi.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/esi.xml index 8b390da4f1e25..8f29f55457b32 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/esi.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/esi.xml @@ -22,6 +22,7 @@ + %http_content_renderer.proxy_path% diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/proxy.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/proxy.xml new file mode 100644 index 0000000000000..fca497721dfad --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/proxy.xml @@ -0,0 +1,18 @@ + + + + + + Symfony\Component\HttpKernel\EventListener\RouterProxyListener + + + + + + + %http_content_renderer.proxy_path% + + + diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd b/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd index 42a331c584325..91105d89822aa 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd @@ -12,6 +12,7 @@ + @@ -44,6 +45,11 @@ + + + + + diff --git a/src/Symfony/Component/HttpKernel/EventListener/RouterProxyListener.php b/src/Symfony/Component/HttpKernel/EventListener/RouterProxyListener.php index 572d0a145654e..e1e59fa5cffa6 100644 --- a/src/Symfony/Component/HttpKernel/EventListener/RouterProxyListener.php +++ b/src/Symfony/Component/HttpKernel/EventListener/RouterProxyListener.php @@ -30,10 +30,18 @@ class RouterProxyListener implements EventSubscriberInterface { private $signer; + private $proxyPath; - public function __construct(UriSigner $signer) + /** + * Constructor. + * + * @param UriSigner $signer A UriSigner instance + * @param string $proxyPath The path that triggers this listener + */ + public function __construct(UriSigner $signer, $proxyPath = '/_proxy') { $this->signer = $signer; + $this->proxyPath = $proxyPath; } /** @@ -47,7 +55,7 @@ public function onKernelRequest(GetResponseEvent $event) { $request = $event->getRequest(); - if ('/_proxy' !== rawurldecode($request->getPathInfo())) { + if ($this->proxyPath !== rawurldecode($request->getPathInfo())) { return; } diff --git a/src/Symfony/Component/HttpKernel/RenderingStrategy/ProxyAwareRenderingStrategy.php b/src/Symfony/Component/HttpKernel/RenderingStrategy/ProxyAwareRenderingStrategy.php index 1067c30d13c4f..086df457d7fd9 100644 --- a/src/Symfony/Component/HttpKernel/RenderingStrategy/ProxyAwareRenderingStrategy.php +++ b/src/Symfony/Component/HttpKernel/RenderingStrategy/ProxyAwareRenderingStrategy.php @@ -13,6 +13,7 @@ use Symfony\Component\HttpKernel\Controller\ControllerReference; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpKernel\EventListener\RouterProxyListener; /** * Adds the possibility to generate a proxy URI for a given Controller. @@ -21,6 +22,20 @@ */ abstract class ProxyAwareRenderingStrategy implements RenderingStrategyInterface { + private $proxyPath = '/_proxy'; + + /** + * Sets the proxy path that triggers the proxy listener + * + * @param string $path The path + * + * @see RouterProxyListener + */ + public function setProxyPath($path) + { + $this->proxyPath = $path; + } + /** * Generates a proxy URI for a given controller. * @@ -39,6 +54,6 @@ protected function generateProxyUri(ControllerReference $reference, Request $req $reference->query['path'] = http_build_query($reference->attributes, '', '&'); - return $request->getUriForPath('/_proxy?'.http_build_query($reference->query, '', '&')); + return $request->getUriForPath($this->proxyPath.'?'.http_build_query($reference->query, '', '&')); } } From e5135f67be3f8ecf69e02640fd89b714cc377eef Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Wed, 23 Jan 2013 07:53:21 +0100 Subject: [PATCH 4/5] [HttpKernel] renamed path to _path to avoid collision --- .../HttpKernel/EventListener/RouterProxyListener.php | 4 ++-- .../ProxyAwareRenderingStrategy.php | 2 +- .../Tests/EventListener/RouterProxyListenerTest.php | 8 ++++---- .../RenderingStrategy/EsiRenderingStrategyTest.php | 2 +- .../HIncludeRenderingStrategyTest.php | 2 +- .../ProxyAwareRenderingStrategyTest.php | 12 ++++++------ 6 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/Symfony/Component/HttpKernel/EventListener/RouterProxyListener.php b/src/Symfony/Component/HttpKernel/EventListener/RouterProxyListener.php index e1e59fa5cffa6..3554361ae3552 100644 --- a/src/Symfony/Component/HttpKernel/EventListener/RouterProxyListener.php +++ b/src/Symfony/Component/HttpKernel/EventListener/RouterProxyListener.php @@ -61,10 +61,10 @@ public function onKernelRequest(GetResponseEvent $event) $this->validateRequest($request); - parse_str($request->query->get('path', ''), $attributes); + parse_str($request->query->get('_path', ''), $attributes); $request->attributes->add($attributes); $request->attributes->set('_route_params', array_replace($request->attributes->get('_route_params', array()), $attributes)); - $request->query->remove('path'); + $request->query->remove('_path'); } protected function validateRequest(Request $request) diff --git a/src/Symfony/Component/HttpKernel/RenderingStrategy/ProxyAwareRenderingStrategy.php b/src/Symfony/Component/HttpKernel/RenderingStrategy/ProxyAwareRenderingStrategy.php index 086df457d7fd9..3c735efdeefe1 100644 --- a/src/Symfony/Component/HttpKernel/RenderingStrategy/ProxyAwareRenderingStrategy.php +++ b/src/Symfony/Component/HttpKernel/RenderingStrategy/ProxyAwareRenderingStrategy.php @@ -52,7 +52,7 @@ protected function generateProxyUri(ControllerReference $reference, Request $req $reference->attributes['_controller'] = $reference->controller; - $reference->query['path'] = http_build_query($reference->attributes, '', '&'); + $reference->query['_path'] = http_build_query($reference->attributes, '', '&'); return $request->getUriForPath($this->proxyPath.'?'.http_build_query($reference->query, '', '&')); } diff --git a/src/Symfony/Component/HttpKernel/Tests/EventListener/RouterProxyListenerTest.php b/src/Symfony/Component/HttpKernel/Tests/EventListener/RouterProxyListenerTest.php index 352e0ab1e0790..f4a3356f6caa3 100644 --- a/src/Symfony/Component/HttpKernel/Tests/EventListener/RouterProxyListenerTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/EventListener/RouterProxyListenerTest.php @@ -28,7 +28,7 @@ protected function setUp() public function testOnlyTriggeredOnProxyRoute() { - $request = Request::create('http://example.com/foo?path=foo%3Dbar%26_controller%3Dfoo'); + $request = Request::create('http://example.com/foo?_path=foo%3Dbar%26_controller%3Dfoo'); $listener = new RouterProxyListener(new UriSigner('foo')); $event = $this->createGetResponseEvent($request); @@ -38,7 +38,7 @@ public function testOnlyTriggeredOnProxyRoute() $listener->onKernelRequest($event); $this->assertEquals($expected, $request->attributes->all()); - $this->assertTrue($request->query->has('path')); + $this->assertTrue($request->query->has('_path')); } /** @@ -83,7 +83,7 @@ public function testAccessDeniedWithWrongSignature() public function testWithSignature() { $signer = new UriSigner('foo'); - $request = Request::create($signer->sign('http://example.com/_proxy?path=foo%3Dbar%26_controller%3Dfoo'), 'GET', array(), array(), array(), array('REMOTE_ADDR' => '10.0.0.1')); + $request = Request::create($signer->sign('http://example.com/_proxy?_path=foo%3Dbar%26_controller%3Dfoo'), 'GET', array(), array(), array(), array('REMOTE_ADDR' => '10.0.0.1')); $listener = new RouterProxyListener($signer); $event = $this->createGetResponseEvent($request); @@ -91,7 +91,7 @@ public function testWithSignature() $listener->onKernelRequest($event); $this->assertEquals(array('foo' => 'bar', '_controller' => 'foo'), $request->attributes->get('_route_params')); - $this->assertFalse($request->query->has('path')); + $this->assertFalse($request->query->has('_path')); } private function createGetResponseEvent(Request $request) diff --git a/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/EsiRenderingStrategyTest.php b/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/EsiRenderingStrategyTest.php index bace5d6f98fb5..99c51926e43fa 100644 --- a/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/EsiRenderingStrategyTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/EsiRenderingStrategyTest.php @@ -47,7 +47,7 @@ public function testRender() $this->assertEquals('', $strategy->render('/', $request)->getContent()); $this->assertEquals("\n", $strategy->render('/', $request, array('comment' => 'This is a comment'))->getContent()); $this->assertEquals('', $strategy->render('/', $request, array('alt' => 'foo'))->getContent()); - $this->assertEquals('', $strategy->render(new ControllerReference('main_controller', array(), array()), $request, array('alt' => new ControllerReference('alt_controller', array(), array())))->getContent()); + $this->assertEquals('', $strategy->render(new ControllerReference('main_controller', array(), array()), $request, array('alt' => new ControllerReference('alt_controller', array(), array())))->getContent()); } private function getDefaultStrategy($called = false) diff --git a/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/HIncludeRenderingStrategyTest.php b/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/HIncludeRenderingStrategyTest.php index b00d16eb5c338..7a5158d3ea43d 100644 --- a/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/HIncludeRenderingStrategyTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/HIncludeRenderingStrategyTest.php @@ -38,7 +38,7 @@ public function testRenderWithControllerAndSigner() { $strategy = new HIncludeRenderingStrategy(null, new UriSigner('foo')); - $this->assertEquals('', $strategy->render(new ControllerReference('main_controller', array(), array()), Request::create('/'))->getContent()); + $this->assertEquals('', $strategy->render(new ControllerReference('main_controller', array(), array()), Request::create('/'))->getContent()); } public function testRenderWithUri() diff --git a/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/ProxyAwareRenderingStrategyTest.php b/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/ProxyAwareRenderingStrategyTest.php index f1e1dc9044509..51e2340117cb0 100644 --- a/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/ProxyAwareRenderingStrategyTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/ProxyAwareRenderingStrategyTest.php @@ -28,11 +28,11 @@ public function testGenerateProxyUri($uri, $controller) public function getGenerateProxyUriData() { return array( - array('http://localhost/_proxy?path=_format%3Dhtml%26_controller%3Dcontroller', new ControllerReference('controller', array(), array())), - array('http://localhost/_proxy?path=_format%3Dxml%26_controller%3Dcontroller', new ControllerReference('controller', array('_format' => 'xml'), array())), - array('http://localhost/_proxy?path=foo%3Dfoo%26_format%3Djson%26_controller%3Dcontroller', new ControllerReference('controller', array('foo' => 'foo', '_format' => 'json'), array())), - array('http://localhost/_proxy?bar=bar&path=foo%3Dfoo%26_format%3Dhtml%26_controller%3Dcontroller', new ControllerReference('controller', array('foo' => 'foo'), array('bar' => 'bar'))), - array('http://localhost/_proxy?foo=foo&path=_format%3Dhtml%26_controller%3Dcontroller', new ControllerReference('controller', array(), array('foo' => 'foo'))), + array('http://localhost/_proxy?_path=_format%3Dhtml%26_controller%3Dcontroller', new ControllerReference('controller', array(), array())), + array('http://localhost/_proxy?_path=_format%3Dxml%26_controller%3Dcontroller', new ControllerReference('controller', array('_format' => 'xml'), array())), + array('http://localhost/_proxy?_path=foo%3Dfoo%26_format%3Djson%26_controller%3Dcontroller', new ControllerReference('controller', array('foo' => 'foo', '_format' => 'json'), array())), + array('http://localhost/_proxy?bar=bar&_path=foo%3Dfoo%26_format%3Dhtml%26_controller%3Dcontroller', new ControllerReference('controller', array('foo' => 'foo'), array('bar' => 'bar'))), + array('http://localhost/_proxy?foo=foo&_path=_format%3Dhtml%26_controller%3Dcontroller', new ControllerReference('controller', array(), array('foo' => 'foo'))), ); } @@ -42,7 +42,7 @@ public function testGenerateProxyUriWithARequest() $request->attributes->set('_format', 'json'); $controller = new ControllerReference('controller', array(), array()); - $this->assertEquals('http://localhost/_proxy?path=_format%3Djson%26_controller%3Dcontroller', $this->getStrategy()->doGenerateProxyUri($controller, $request)); + $this->assertEquals('http://localhost/_proxy?_path=_format%3Djson%26_controller%3Dcontroller', $this->getStrategy()->doGenerateProxyUri($controller, $request)); } private function getStrategy() From 23f51450bd5af35055db47b1787b5623b78df29b Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Wed, 23 Jan 2013 07:57:42 +0100 Subject: [PATCH 5/5] renamed proxy to router_proxy --- .../FrameworkBundle/DependencyInjection/Configuration.php | 8 ++++---- .../DependencyInjection/FrameworkExtension.php | 8 ++++---- .../Resources/config/schema/symfony-1.0.xsd | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php index cd39e49842364..885752b89da6b 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php @@ -73,7 +73,7 @@ public function getConfigTreeBuilder() $this->addFormSection($rootNode); $this->addEsiSection($rootNode); - $this->addProxySection($rootNode); + $this->addRouterProxySection($rootNode); $this->addProfilerSection($rootNode); $this->addRouterSection($rootNode); $this->addSessionSection($rootNode); @@ -115,12 +115,12 @@ private function addEsiSection(ArrayNodeDefinition $rootNode) ; } - private function addProxySection(ArrayNodeDefinition $rootNode) + private function addRouterProxySection(ArrayNodeDefinition $rootNode) { $rootNode ->children() - ->arrayNode('proxy') - ->info('proxy configuration') + ->arrayNode('router_proxy') + ->info('proxy configuration for the HTTP content renderer') ->canBeDisabled() ->children() ->scalarNode('path')->defaultValue('/_proxy')->end() diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index 0cd4afb37086c..958c2ce35065c 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -94,8 +94,8 @@ public function load(array $configs, ContainerBuilder $container) $this->registerEsiConfiguration($config['esi'], $loader); } - if (isset($config['proxy'])) { - $this->registerProxyConfiguration($config['proxy'], $container, $loader); + if (isset($config['router_proxy'])) { + $this->registerRouterProxyConfiguration($config['router_proxy'], $container, $loader); } if (isset($config['profiler'])) { @@ -189,12 +189,12 @@ private function registerEsiConfiguration(array $config, XmlFileLoader $loader) } /** - * Loads the proxy configuration. + * Loads the router proxy configuration. * * @param array $config A proxy configuration array * @param XmlFileLoader $loader An XmlFileLoader instance */ - private function registerProxyConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader) + private function registerRouterProxyConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader) { if (!empty($config['enabled'])) { $loader->load('proxy.xml'); diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd b/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd index 91105d89822aa..14dcf9f8a59be 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd @@ -12,7 +12,7 @@ - + @@ -45,7 +45,7 @@ - +