4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\EventListener;
14 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15 use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
16 use Symfony\Component\HttpKernel\HttpCache\HttpCache;
17 use Symfony\Component\HttpKernel\HttpCache\SurrogateInterface;
18 use Symfony\Component\HttpKernel\KernelEvents;
21 * SurrogateListener adds a Surrogate-Control HTTP header when the Response needs to be parsed for Surrogates.
23 * @author Fabien Potencier <fabien@symfony.com>
25 class SurrogateListener implements EventSubscriberInterface
29 public function __construct(SurrogateInterface $surrogate = null)
31 $this->surrogate = $surrogate;
35 * Filters the Response.
37 public function onKernelResponse(FilterResponseEvent $event)
39 if (!$event->isMasterRequest()) {
43 $kernel = $event->getKernel();
44 $surrogate = $this->surrogate;
45 if ($kernel instanceof HttpCache) {
46 $surrogate = $kernel->getSurrogate();
47 if (null !== $this->surrogate && $this->surrogate->getName() !== $surrogate->getName()) {
48 $surrogate = $this->surrogate;
52 if (null === $surrogate) {
56 $surrogate->addSurrogateControl($event->getResponse());
59 public static function getSubscribedEvents()
62 KernelEvents::RESPONSE => 'onKernelResponse',