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/Bundle/FrameworkBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php
index 70017d727baea..885752b89da6b 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->addRouterProxySection($rootNode);
$this->addProfilerSection($rootNode);
$this->addRouterSection($rootNode);
$this->addSessionSection($rootNode);
@@ -114,6 +115,21 @@ private function addEsiSection(ArrayNodeDefinition $rootNode)
;
}
+ private function addRouterProxySection(ArrayNodeDefinition $rootNode)
+ {
+ $rootNode
+ ->children()
+ ->arrayNode('router_proxy')
+ ->info('proxy configuration for the HTTP content renderer')
+ ->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..958c2ce35065c 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['router_proxy'])) {
+ $this->registerRouterProxyConfiguration($config['router_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 router proxy configuration.
+ *
+ * @param array $config A proxy configuration array
+ * @param XmlFileLoader $loader An XmlFileLoader instance
+ */
+ private function registerRouterProxyConfiguration(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 332cf0de38804..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,7 +22,7 @@
-
+ %http_content_renderer.proxy_path%
@@ -30,13 +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 0c4a271863a8d..8f29f55457b32 100644
--- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/esi.xml
+++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/esi.xml
@@ -22,7 +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/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/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd b/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd
index 42a331c584325..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,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 b88350c20fa32..3554361ae3552 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,16 +55,16 @@ public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
- if ('_proxy' !== $request->attributes->get('_route')) {
+ if ($this->proxyPath !== rawurldecode($request->getPathInfo())) {
return;
}
$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)
@@ -92,7 +100,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/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..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;
@@ -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..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;
@@ -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
deleted file mode 100644
index a5ba272f81e9b..0000000000000
--- a/src/Symfony/Component/HttpKernel/RenderingStrategy/GeneratorAwareRenderingStrategy.php
+++ /dev/null
@@ -1,84 +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 = null)
- {
- 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']);
- } elseif (null !== $request) {
- $format = $request->getRequestFormat();
- } else {
- $format = 'html';
- }
-
- 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 cdcb6acee9109..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;
@@ -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/ProxyAwareRenderingStrategy.php b/src/Symfony/Component/HttpKernel/RenderingStrategy/ProxyAwareRenderingStrategy.php
new file mode 100644
index 0000000000000..3c735efdeefe1
--- /dev/null
+++ b/src/Symfony/Component/HttpKernel/RenderingStrategy/ProxyAwareRenderingStrategy.php
@@ -0,0 +1,59 @@
+
+ *
+ * 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\HttpKernel\EventListener\RouterProxyListener;
+
+/**
+ * Adds the possibility to generate a proxy URI for a given Controller.
+ *
+ * @author Fabien Potencier
+ */
+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.
+ *
+ * @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($this->proxyPath.'?'.http_build_query($reference->query, '', '&'));
+ }
+}
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/EventListener/RouterProxyListenerTest.php b/src/Symfony/Component/HttpKernel/Tests/EventListener/RouterProxyListenerTest.php
index b0e091d50414a..f4a3356f6caa3 100644
--- a/src/Symfony/Component/HttpKernel/Tests/EventListener/RouterProxyListenerTest.php
+++ b/src/Symfony/Component/HttpKernel/Tests/EventListener/RouterProxyListenerTest.php
@@ -28,17 +28,17 @@ 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();
$listener->onKernelRequest($event);
$this->assertEquals($expected, $request->attributes->all());
- $this->assertTrue($request->query->has('path'));
+ $this->assertTrue($request->query->has('_path'));
}
/**
@@ -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()
- {
- $signer = new UriSigner('foo');
- $request = Request::create($signer->sign('http://example.com/foo'), '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->assertFalse($request->query->has('path'));
- }
-
- public function testWithSignatureAndPath()
+ public function testWithSignature()
{
$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'));
+ $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', 'bar' => 'bar'), $request->attributes->get('_route_params'));
- $this->assertFalse($request->query->has('path'));
+ $this->assertEquals(array('foo' => 'bar', '_controller' => 'foo'), $request->attributes->get('_route_params'));
+ $this->assertFalse($request->query->has('_path'));
}
- private function createGetResponseEvent(Request $request, $route = '_proxy')
+ private function createGetResponseEvent(Request $request)
{
- $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/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/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 8a208f48e389f..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()
{
@@ -35,15 +35,14 @@ 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()
{
$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 +52,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 +69,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..99c51926e43fa 100644
--- a/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/EsiRenderingStrategyTest.php
+++ b/src/Symfony/Component/HttpKernel/Tests/RenderingStrategy/EsiRenderingStrategyTest.php
@@ -16,23 +16,19 @@
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()
{
$strategy = new EsiRenderingStrategy(new Esi(), $this->getDefaultStrategy(true));
- $strategy->render('/');
+ $strategy->render('/', Request::create('/'));
}
public function testRenderFallbackToDefaultStrategyIfEsiNotSupported()
@@ -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 387ab3e2a0f20..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()));
- }
-
- /**
- * @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()));
- }
-
- /**
- * @dataProvider getGeneratorProxyUriData
- */
- public function testGenerateProxyUri($uri, $controller)
- {
- $this->assertEquals($uri, $this->getStrategy()->doGenerateProxyUri($controller));
- }
-
- 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 = null, array $options = array()) {}
- public function getName() {}
-
- public function doGenerateProxyUri(ControllerReference $reference, Request $request = null)
- {
- 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..7a5158d3ea43d 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');
- }
}
/**
@@ -35,37 +31,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());
}
}
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..51e2340117cb0
--- /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);
+ }
+}