|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the FOSHttpCacheBundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.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 FOS\HttpCacheBundle\EventListener; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
15
|
|
|
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; |
|
16
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
|
17
|
|
|
use Symfony\Component\HttpKernel\EventListener\SessionListener as BaseSessionListener; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Decorates the default Symfony session listener. |
|
21
|
|
|
* |
|
22
|
|
|
* The default Symfony session listener automatically makes responses private |
|
23
|
|
|
* in case the session was started. This kills the user context feature of |
|
24
|
|
|
* FOSHttpCache. We disable the default behaviour only if the user context header |
|
25
|
|
|
* is part of the Vary headers to reduce the possible impacts on other parts |
|
26
|
|
|
* of your application. |
|
27
|
|
|
* |
|
28
|
|
|
* @author Yanick Witschi <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
final class SessionListener implements EventSubscriberInterface |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @var BaseSessionListener |
|
34
|
|
|
*/ |
|
35
|
|
|
private $inner; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
private $userHashHeader; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var array |
|
44
|
|
|
*/ |
|
45
|
|
|
private $userIdentifierHeaders; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param BaseSessionListener $inner |
|
49
|
|
|
* @param string $userHashHeader Must be lower-cased |
|
50
|
|
|
* @param array $userIdentifierHeaders Must be lower-cased |
|
51
|
|
|
*/ |
|
52
|
23 |
|
public function __construct(BaseSessionListener $inner, string $userHashHeader, array $userIdentifierHeaders) |
|
53
|
|
|
{ |
|
54
|
23 |
|
$this->inner = $inner; |
|
55
|
23 |
|
$this->userHashHeader = $userHashHeader; |
|
56
|
23 |
|
$this->userIdentifierHeaders = $userIdentifierHeaders; |
|
57
|
23 |
|
} |
|
58
|
|
|
|
|
59
|
17 |
|
public function onKernelRequest(GetResponseEvent $event) |
|
60
|
|
|
{ |
|
61
|
17 |
|
return $this->inner->onKernelRequest($event); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
21 |
|
public function onKernelResponse(FilterResponseEvent $event) |
|
65
|
|
|
{ |
|
66
|
21 |
|
if (!$event->isMasterRequest()) { |
|
67
|
1 |
|
return; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
21 |
|
$varyHeaders = array_map('strtolower', $event->getResponse()->getVary()); |
|
71
|
21 |
|
$relevantHeaders = array_merge($this->userIdentifierHeaders, [$this->userHashHeader]); |
|
72
|
|
|
|
|
73
|
|
|
// Call default behaviour if it's an irrelevant request for the user context |
|
74
|
21 |
|
if (0 === count(array_intersect($varyHeaders, $relevantHeaders))) { |
|
75
|
2 |
|
$this->inner->onKernelResponse($event); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
// noop, see class description |
|
79
|
21 |
|
} |
|
80
|
|
|
|
|
81
|
2 |
|
public static function getSubscribedEvents(): array |
|
82
|
|
|
{ |
|
83
|
2 |
|
return BaseSessionListener::getSubscribedEvents(); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|