Skip to content

[HttpKernel][WIP] Implement SSI rendering strategy #6720

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

Closed
wants to merge 1 commit into from
Closed
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 @@ -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);
Expand Down Expand Up @@ -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
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['ssi'])) {
$this->registerSsiConfiguration($config['ssi'], $loader);
}

if (isset($config['profiler'])) {
$this->registerProfilerConfiguration($config['profiler'], $container, $loader);
}
Expand Down Expand Up @@ -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.
*
Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/ssi.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.strategy.ssi.class">Symfony\Component\HttpKernel\RenderingStrategy\SsiRenderingStrategy</parameter>
</parameters>

<services>
<service id="http_content_renderer.strategy.ssi" class="%http_content_renderer.strategy.ssi.class%">
<tag name="kernel.content_renderer_strategy" />
<argument type="service" id="http_content_renderer.strategy.default" />
<call method="setUrlGenerator"><argument type="service" id="router" /></call>
</service>
</services>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <krebs.seb@gmail.com>
*/
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('<!--#include virtual="%s"%s -->',
$uri,
$ignoreErrors ? ' fmt="?"' : ''
);

if (!empty($comment)) {
return sprintf("<!-- %s -->\n%s", $comment, $html);
}

return $html;
}
}