made the proxy path configurable

This commit is contained in:
Fabien Potencier 2013-01-22 10:24:26 +01:00
parent ad82893691
commit 3193a90815
8 changed files with 88 additions and 10 deletions

View File

@ -73,6 +73,7 @@ class Configuration implements ConfigurationInterface
$this->addFormSection($rootNode);
$this->addEsiSection($rootNode);
$this->addProxySection($rootNode);
$this->addProfilerSection($rootNode);
$this->addRouterSection($rootNode);
$this->addSessionSection($rootNode);
@ -114,6 +115,21 @@ class Configuration implements ConfigurationInterface
;
}
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

View File

@ -94,6 +94,10 @@ class FrameworkExtension extends Extension
$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 @@ class FrameworkExtension extends Extension
}
}
/**
* 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.
*

View File

@ -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>
@ -22,6 +22,7 @@
<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="setProxyPath"><argument>%http_content_renderer.proxy_path%</argument></call>
</service>
<service id="http_content_renderer.strategy.hinclude" class="%http_content_renderer.strategy.hinclude.class%">
@ -29,12 +30,7 @@
<argument type="service" id="templating" on-invalid="null" />
<argument type="service" id="uri_signer" />
<argument>%http_content_renderer.strategy.hinclude.global_template%</argument>
</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>

View File

@ -22,6 +22,7 @@
<tag name="kernel.content_renderer_strategy" />
<argument type="service" id="esi" />
<argument type="service" id="http_content_renderer.strategy.default" />
<call method="setProxyPath"><argument>%http_content_renderer.proxy_path%</argument></call>
</service>
</services>
</container>

View File

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

View File

@ -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="proxy" type="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" />
@ -44,6 +45,11 @@
<xsd:attribute name="enabled" type="xsd:boolean" />
</xsd:complexType>
<xsd:complexType name="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" />

View File

@ -30,10 +30,18 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface;
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 @@ class RouterProxyListener implements EventSubscriberInterface
{
$request = $event->getRequest();
if ('/_proxy' !== rawurldecode($request->getPathInfo())) {
if ($this->proxyPath !== rawurldecode($request->getPathInfo())) {
return;
}

View File

@ -13,6 +13,7 @@ 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.
@ -21,6 +22,20 @@ use Symfony\Component\HttpFoundation\Request;
*/
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 @@ abstract class ProxyAwareRenderingStrategy implements RenderingStrategyInterface
$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, '', '&'));
}
}