Version 1
[yaffs-website] / vendor / symfony / http-kernel / EventListener / SurrogateListener.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\HttpKernel\EventListener;
13
14 use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
15 use Symfony\Component\HttpKernel\HttpCache\SurrogateInterface;
16 use Symfony\Component\HttpKernel\KernelEvents;
17 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
18
19 /**
20  * SurrogateListener adds a Surrogate-Control HTTP header when the Response needs to be parsed for Surrogates.
21  *
22  * @author Fabien Potencier <fabien@symfony.com>
23  */
24 class SurrogateListener implements EventSubscriberInterface
25 {
26     private $surrogate;
27
28     /**
29      * Constructor.
30      *
31      * @param SurrogateInterface $surrogate An SurrogateInterface instance
32      */
33     public function __construct(SurrogateInterface $surrogate = null)
34     {
35         $this->surrogate = $surrogate;
36     }
37
38     /**
39      * Filters the Response.
40      *
41      * @param FilterResponseEvent $event A FilterResponseEvent instance
42      */
43     public function onKernelResponse(FilterResponseEvent $event)
44     {
45         if (!$event->isMasterRequest() || null === $this->surrogate) {
46             return;
47         }
48
49         $this->surrogate->addSurrogateControl($event->getResponse());
50     }
51
52     public static function getSubscribedEvents()
53     {
54         return array(
55             KernelEvents::RESPONSE => 'onKernelResponse',
56         );
57     }
58 }