Skip to content

Commit

Permalink
made the proxy path configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Jan 23, 2013
1 parent ad82893 commit 3193a90
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 10 deletions.
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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.
*
Expand Down
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,19 +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="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>
</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
Expand Up @@ -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>
18 changes: 18 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/proxy.xml
@@ -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>
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="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" />
Expand Down Expand Up @@ -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" />
Expand Down
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,7 +55,7 @@ public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();

if ('/_proxy' !== rawurldecode($request->getPathInfo())) {
if ($this->proxyPath !== rawurldecode($request->getPathInfo())) {
return;
}

Expand Down
Expand Up @@ -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.
Expand All @@ -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.
*
Expand All @@ -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, '', '&'));
}
}

0 comments on commit 3193a90

Please sign in to comment.