Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Content renderer simplification #6829

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 23, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
*/
Expand All @@ -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") }}')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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']);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

router_proxy_path ?

}
}

/**
* Loads the profiler configuration.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parameter key="http_content_renderer.strategy.default.class">Symfony\Component\HttpKernel\RenderingStrategy\DefaultRenderingStrategy</parameter>
<parameter key="http_content_renderer.strategy.hinclude.class">Symfony\Component\HttpKernel\RenderingStrategy\HIncludeRenderingStrategy</parameter>
<parameter key="http_content_renderer.strategy.hinclude.global_template"></parameter>
<parameter key="http_content_renderer.listener.router_proxy.class">Symfony\Component\HttpKernel\EventListener\RouterProxyListener</parameter>
<parameter key="http_content_renderer.proxy_path">/_proxy</parameter>
</parameters>

<services>
Expand All @@ -22,21 +22,15 @@
<service id="http_content_renderer.strategy.default" class="%http_content_renderer.strategy.default.class%">
<tag name="kernel.content_renderer_strategy" />
<argument type="service" id="http_kernel" />
<call method="setUrlGenerator"><argument type="service" id="router" /></call>
<call method="setProxyPath"><argument>%http_content_renderer.proxy_path%</argument></call>
</service>

<service id="http_content_renderer.strategy.hinclude" class="%http_content_renderer.strategy.hinclude.class%">
<tag name="kernel.content_renderer_strategy" />
<argument type="service" id="templating" on-invalid="null" />
<argument type="service" id="uri_signer" />
<argument>%http_content_renderer.strategy.hinclude.global_template%</argument>
<call method="setUrlGenerator"><argument type="service" id="router" /></call>
</service>

<!-- FIXME: make the listener registration optional via a configuration setting? -->
<service id="http_content_renderer.listener.router_proxy" class="%http_content_renderer.listener.router_proxy.class%">
<tag name="kernel.event_subscriber" />
<argument type="service" id="uri_signer" />
<call method="setProxyPath"><argument>%http_content_renderer.proxy_path%</argument></call>
</service>

</services>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<tag name="kernel.content_renderer_strategy" />
<argument type="service" id="esi" />
<argument type="service" id="http_content_renderer.strategy.default" />
<call method="setUrlGenerator"><argument type="service" id="router" /></call>
<call method="setProxyPath"><argument>%http_content_renderer.proxy_path%</argument></call>
</service>
</services>
</container>
18 changes: 18 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/proxy.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="http_content_renderer.listener.router_proxy.class">Symfony\Component\HttpKernel\EventListener\RouterProxyListener</parameter>
</parameters>

<services>
<service id="http_content_renderer.listener.router_proxy" class="%http_content_renderer.listener.router_proxy.class%">
<tag name="kernel.event_subscriber" />
<argument type="service" id="uri_signer" />
<argument>%http_content_renderer.proxy_path%</argument>
</service>
</services>
</container>

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<xsd:element name="form" type="form" minOccurs="0" maxOccurs="1" />
<xsd:element name="csrf-protection" type="csrf_protection" minOccurs="0" maxOccurs="1" />
<xsd:element name="esi" type="esi" minOccurs="0" maxOccurs="1" />
<xsd:element name="router-proxy" type="router-proxy" minOccurs="0" maxOccurs="1" />
<xsd:element name="profiler" type="profiler" minOccurs="0" maxOccurs="1" />
<xsd:element name="router" type="router" minOccurs="0" maxOccurs="1" />
<xsd:element name="session" type="session" minOccurs="0" maxOccurs="1" />
Expand Down Expand Up @@ -44,6 +45,11 @@
<xsd:attribute name="enabled" type="xsd:boolean" />
</xsd:complexType>

<xsd:complexType name="router-proxy">
<xsd:attribute name="enabled" type="xsd:boolean" />
<xsd:attribute name="path" type="xsd:string" />
</xsd:complexType>

<xsd:complexType name="profiler">
<xsd:all>
<xsd:element name="matcher" type="profiler_matcher" minOccurs="0" maxOccurs="1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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)
Expand Down Expand Up @@ -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)),
);
}
}
10 changes: 7 additions & 3 deletions src/Symfony/Component/HttpKernel/HttpContentRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
*
* @see RenderingStrategyInterface
*/
class HttpContentRenderer implements EventSubscriberInterface
{
Expand Down Expand Up @@ -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));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*
* @author Fabien Potencier <[email protected]>
*/
class DefaultRenderingStrategy extends GeneratorAwareRenderingStrategy
class DefaultRenderingStrategy extends ProxyAwareRenderingStrategy
{
private $kernel;

Expand All @@ -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);
Expand Down Expand Up @@ -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()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the condition useful here ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, because you can disable the session entirely in FrameworkBundle

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also the phpdoc says it can return null.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still don't see the pb with set(null) ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, now I see :)

$subRequest->setSession($session);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*
* @author Fabien Potencier <[email protected]>
*/
class EsiRenderingStrategy extends GeneratorAwareRenderingStrategy
class EsiRenderingStrategy extends ProxyAwareRenderingStrategy
{
private $esi;
private $defaultStrategy;
Expand Down Expand Up @@ -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);
}

Expand Down
Loading