-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpKernel][FrameworkBundle] SSI support #10702
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,7 +39,7 @@ public function __construct(HttpKernelInterface $kernel, $cacheDir = null) | |
$this->kernel = $kernel; | ||
$this->cacheDir = $cacheDir; | ||
|
||
parent::__construct($kernel, $this->createStore(), $this->createEsi(), array_merge(array('debug' => $kernel->isDebug()), $this->getOptions())); | ||
parent::__construct($kernel, $this->createStore(), $this->createSurrogate(), array_merge(array('debug' => $kernel->isDebug()), $this->getOptions())); | ||
} | ||
|
||
/** | ||
|
@@ -55,7 +55,7 @@ protected function forward(Request $request, $raw = false, Response $entry = nul | |
{ | ||
$this->getKernel()->boot(); | ||
$this->getKernel()->getContainer()->set('cache', $this); | ||
$this->getKernel()->getContainer()->set('esi', $this->getEsi()); | ||
$this->getKernel()->getContainer()->set($this->getSurrogate()->getName(), $this->getSurrogate()); | ||
|
||
return parent::forward($request, $raw, $entry); | ||
} | ||
|
@@ -70,6 +70,18 @@ protected function getOptions() | |
return array(); | ||
} | ||
|
||
protected function createSurrogate() | ||
{ | ||
return $this->createEsi(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you just use |
||
} | ||
|
||
/** | ||
* Creates new ESI instance | ||
* | ||
* @return Esi | ||
* | ||
* @deprecated Deprecated since version 2.6, to be removed in 3.0. Use createSurrogate() instead | ||
*/ | ||
protected function createEsi() | ||
{ | ||
return new Esi(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what about here ? shouldn't be Esi renamed too ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Couldn't decide myself (and later I forgot). Problem is, to what should I rename it? You can overwrite it in your applications protected function createSurrogate()
{
return new Ssi();
} That's not perfect, but it works and is backward compatible. The other solution is to provide two separate |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
<parameter key="fragment.renderer.hinclude.class">Symfony\Bundle\FrameworkBundle\Fragment\ContainerAwareHIncludeFragmentRenderer</parameter> | ||
<parameter key="fragment.renderer.hinclude.global_template"></parameter> | ||
<parameter key="fragment.renderer.esi.class">Symfony\Component\HttpKernel\Fragment\EsiFragmentRenderer</parameter> | ||
<parameter key="fragment.renderer.ssi.class">Symfony\Component\HttpKernel\Fragment\SsiFragmentRenderer</parameter> | ||
<parameter key="fragment.path">/_fragment</parameter> | ||
</parameters> | ||
|
||
|
@@ -41,5 +42,17 @@ | |
<argument type="service" id="fragment.renderer.inline" /> | ||
<call method="setFragmentPath"><argument>%fragment.path%</argument></call> | ||
</service> | ||
|
||
<service id="fragment.renderer.ssi" class="%fragment.renderer.ssi.class%"> | ||
<tag name="kernel.fragment_renderer" /> | ||
<argument type="service" id="ssi" on-invalid="null" /> | ||
<argument type="service" id="fragment.renderer.inline" /> | ||
<call method="setFragmentPath"> | ||
<argument>%fragment.path%</argument> | ||
</call> | ||
<call method="setUriSigner"> | ||
<argument type="service" id="uri_signer" /> | ||
</call> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't really care, but the rationale behind that was, that it keeps the constructor signatures in sync and beside that it already uses setter injection for the fragment path. Any other suggestions? Btw: Why |
||
</service> | ||
</services> | ||
</container> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?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="ssi.class">Symfony\Component\HttpKernel\HttpCache\Ssi</parameter> | ||
<parameter key="ssi_listener.class">Symfony\Component\HttpKernel\EventListener\SurrogateListener</parameter> | ||
</parameters> | ||
|
||
<services> | ||
<service id="ssi" class="%ssi.class%" /> | ||
|
||
<service id="ssi_listener" class="%ssi_listener.class%"> | ||
<tag name="kernel.event_subscriber" /> | ||
<argument type="service" id="ssi" on-invalid="ignore" /> | ||
</service> | ||
</services> | ||
</container> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,48 +11,13 @@ | |
|
||
namespace Symfony\Component\HttpKernel\EventListener; | ||
|
||
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
use Symfony\Component\HttpKernel\HttpCache\Esi; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
/** | ||
* EsiListener adds a Surrogate-Control HTTP header when the Response needs to be parsed for ESI. | ||
* | ||
* @author Fabien Potencier <[email protected]> | ||
* | ||
* @deprecated Deprecated since version 2.6, to be removed in 3.0. Use SurrogateListener instead | ||
*/ | ||
class EsiListener implements EventSubscriberInterface | ||
class EsiListener extends SurrogateListener | ||
{ | ||
private $esi; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param Esi $esi An ESI instance | ||
*/ | ||
public function __construct(Esi $esi = null) | ||
{ | ||
$this->esi = $esi; | ||
} | ||
|
||
/** | ||
* Filters the Response. | ||
* | ||
* @param FilterResponseEvent $event A FilterResponseEvent instance | ||
*/ | ||
public function onKernelResponse(FilterResponseEvent $event) | ||
{ | ||
if (!$event->isMasterRequest() || null === $this->esi) { | ||
return; | ||
} | ||
|
||
$this->esi->addSurrogateControl($event->getResponse()); | ||
} | ||
|
||
public static function getSubscribedEvents() | ||
{ | ||
return array( | ||
KernelEvents::RESPONSE => 'onKernelResponse', | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpKernel\EventListener; | ||
|
||
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; | ||
use Symfony\Component\HttpKernel\HttpCache\SurrogateInterface; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
/** | ||
* SurrogateListener adds a Surrogate-Control HTTP header when the Response needs to be parsed for Surrogates | ||
* | ||
* @author Fabien Potencier <[email protected]> | ||
*/ | ||
class SurrogateListener implements EventSubscriberInterface | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. renaming the class is a BC break. You need to keep the old one (marking it as deprecated) |
||
{ | ||
private $surrogate; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param SurrogateInterface $surrogate An SurrogateInterface instance | ||
*/ | ||
public function __construct(SurrogateInterface $surrogate = null) | ||
{ | ||
$this->surrogate = $surrogate; | ||
} | ||
|
||
/** | ||
* Filters the Response. | ||
* | ||
* @param FilterResponseEvent $event A FilterResponseEvent instance | ||
*/ | ||
public function onKernelResponse(FilterResponseEvent $event) | ||
{ | ||
if (!$event->isMasterRequest() || null === $this->surrogate) { | ||
return; | ||
} | ||
|
||
$this->surrogate->addSurrogateControl($event->getResponse()); | ||
} | ||
|
||
public static function getSubscribedEvents() | ||
{ | ||
return array( | ||
KernelEvents::RESPONSE => 'onKernelResponse', | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpKernel\Fragment; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\Controller\ControllerReference; | ||
use Symfony\Component\HttpKernel\HttpCache\SurrogateInterface; | ||
|
||
/** | ||
* Implements Surrogate rendering strategy. | ||
* | ||
* @author Fabien Potencier <[email protected]> | ||
*/ | ||
abstract class AbstractSurrogateFragmentRenderer extends RoutableFragmentRenderer | ||
{ | ||
private $surrogate; | ||
private $inlineStrategy; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* The "fallback" strategy when surrogate is not available should always be an | ||
* instance of InlineFragmentRenderer. | ||
* | ||
* @param SurrogateInterface $surrogate An Surrogate instance | ||
* @param FragmentRendererInterface $inlineStrategy The inline strategy to use when the surrogate is not supported | ||
*/ | ||
public function __construct(SurrogateInterface $surrogate = null, FragmentRendererInterface $inlineStrategy) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks wrong, first param shouln't be optional if second is required... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it is already the existing signature. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exactly (I've tried it out...) |
||
{ | ||
$this->surrogate = $surrogate; | ||
$this->inlineStrategy = $inlineStrategy; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* Note that if the current Request has no surrogate capability, this method | ||
* falls back to use the inline rendering strategy. | ||
* | ||
* Additional available options: | ||
* | ||
* * alt: an alternative URI to render in case of an error | ||
* * comment: a comment to add when returning the surrogate tag | ||
* | ||
* Note, that not all surrogate strategies support all options. For now | ||
* 'alt' and 'comment' are only supported by ESI. | ||
* | ||
* @see Symfony\Component\HttpKernel\HttpCache\SurrogateInterface | ||
*/ | ||
public function render($uri, Request $request, array $options = array()) | ||
{ | ||
if (!$this->surrogate || !$this->surrogate->hasSurrogateCapability($request)) { | ||
return $this->inlineStrategy->render($uri, $request, $options); | ||
} | ||
|
||
if ($uri instanceof ControllerReference) { | ||
$uri = $this->generateFragmentUri($uri, $request); | ||
} | ||
|
||
$alt = isset($options['alt']) ? $options['alt'] : null; | ||
if ($alt instanceof ControllerReference) { | ||
$alt = $this->generateFragmentUri($alt, $request); | ||
} | ||
|
||
$tag = $this->surrogate->renderIncludeTag($uri, $alt, isset($options['ignore_errors']) ? $options['ignore_errors'] : false, isset($options['comment']) ? $options['comment'] : ''); | ||
|
||
return new Response($tag); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you cannot rename the method. It is a BC break. If a child class is overwriting it (which is allowed as it is protected so it is an extension point), your change means that the override is not taken into account anymore.
If you want to have
createSurrogate
, you need to keep thecreateEsi
method (but deprecating it) and calling it in the new method, so that BC is preserved