From 02564a2fe82dfd7481c2fae624578ff8c0e5fd21 Mon Sep 17 00:00:00 2001 From: Sebastian Krebs Date: Sun, 13 Jan 2013 00:25:02 +0100 Subject: [PATCH] Implement SSI rendering strategy --- .../DependencyInjection/Configuration.php | 11 +++ .../FrameworkExtension.php | 16 ++++ .../FrameworkBundle/Resources/config/ssi.xml | 18 ++++ .../SsiRenderingStrategy.php | 84 +++++++++++++++++++ 4 files changed, 129 insertions(+) create mode 100644 src/Symfony/Bundle/FrameworkBundle/Resources/config/ssi.xml create mode 100644 src/Symfony/Component/HttpKernel/RenderingStrategy/SsiRenderingStrategy.php diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php index 70017d727baea..54120e05ef490 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->addSsiSection($rootNode); $this->addProfilerSection($rootNode); $this->addRouterSection($rootNode); $this->addSessionSection($rootNode); @@ -114,6 +115,16 @@ private function addEsiSection(ArrayNodeDefinition $rootNode) ; } + private function addSsiSection (ArrayNodeDefinition $rootNode) { + $rootNode + ->children() + ->arrayNode('ssi') + ->info('ssi configuration') + ->canBeDisabled() + ->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 bdf1720eadf85..0752a3da52791 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['ssi'])) { + $this->registerSsiConfiguration($config['ssi'], $loader); + } + if (isset($config['profiler'])) { $this->registerProfilerConfiguration($config['profiler'], $container, $loader); } @@ -184,6 +188,18 @@ private function registerEsiConfiguration(array $config, XmlFileLoader $loader) } } + /** + * Loads the SSI configuration. + * + * @param array $config An SSI configuration array + * @param XmlFileLoader $loader An XmlFileLoader instance + */ + private function registerSsiConfiguration (array $config, XmlFileLoader $loader) { + if (!empty($config['enabled'])) { + $loader->load('ssi.xml'); + } + } + /** * Loads the profiler configuration. * diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/ssi.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/ssi.xml new file mode 100644 index 0000000000000..2c4f7b43d7b7d --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/ssi.xml @@ -0,0 +1,18 @@ + + + + + + Symfony\Component\HttpKernel\RenderingStrategy\SsiRenderingStrategy + + + + + + + + + + diff --git a/src/Symfony/Component/HttpKernel/RenderingStrategy/SsiRenderingStrategy.php b/src/Symfony/Component/HttpKernel/RenderingStrategy/SsiRenderingStrategy.php new file mode 100644 index 0000000000000..1c82c92ebf68c --- /dev/null +++ b/src/Symfony/Component/HttpKernel/RenderingStrategy/SsiRenderingStrategy.php @@ -0,0 +1,84 @@ + + * + * 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\HttpFoundation\Request; +use Symfony\Component\HttpKernel\Controller\ControllerReference; + +/** + * Implements the ESI rendering strategy. + * + * @author Sebastian Krebs + */ +class SsiRenderingStrategy extends GeneratorAwareRenderingStrategy +{ + private $defaultStrategy; + + /** + * Constructor. + * + * The "fallback" strategy when ESI is not available should always be an + * instance of DefaultRenderingStrategy (or a class you are using for the + * default strategy). + * + * @param RenderingStrategyInterface $defaultStrategy The default strategy to use when ESI is not supported + */ + public function __construct(RenderingStrategyInterface $defaultStrategy) + { + $this->defaultStrategy = $defaultStrategy; + } + + /** + * {@inheritdoc} + * + * Note that if the current Request has no ESI capability, this method + * falls back to use the default rendering strategy. + * + * Additional available options: + * + * * comment: a comment to add when returning an esi:include tag + */ + public function render($uri, Request $request = null, array $options = array()) + { + if (null === $request) { + return $this->defaultStrategy->render($uri, $request, $options); + } + + if ($uri instanceof ControllerReference) { + $uri = $this->generateProxyUri($uri, $request); + } + + return $this->renderIncludeTag($uri, isset($options['ignore_errors']) ? $options['ignore_errors'] : false, isset($options['comment']) ? $options['comment'] : ''); + } + + /** + * {@inheritdoc} + */ + public function getName() + { + return 'ssi'; + } + + + private function renderIncludeTag ($uri, $ignoreErrors = true, $comment = '') { + $html = sprintf('', + $uri, + $ignoreErrors ? ' fmt="?"' : '' + ); + + if (!empty($comment)) { + return sprintf("\n%s", $comment, $html); + } + + return $html; + } +}