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
|
|
|
* Since Symfony 3.4, the default Symfony session listener automatically |
23
|
|
|
* overwrites the Cache-Control headers to `private` in case the session has |
24
|
|
|
* been started. This destroys the user context feature of FOSHttpCache. |
25
|
|
|
* Since Symfony 4.1, there is a header we can set to skip this behaviour. We |
26
|
|
|
* set that header in UserContextListener. |
27
|
|
|
* For Symfony 3.4 and 4.0, we decorate the listener to only call the default |
28
|
|
|
* behaviour if `Vary` contains neither the context hash header nor any of the |
29
|
|
|
* user identifier headers, to avoid impacts on other parts of your application. |
30
|
|
|
* |
31
|
|
|
* @author Yanick Witschi <[email protected]> |
32
|
|
|
*/ |
33
|
|
|
final class SessionListener implements EventSubscriberInterface |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var BaseSessionListener |
37
|
|
|
*/ |
38
|
|
|
private $inner; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $userHashHeader; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
private $userIdentifierHeaders; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param BaseSessionListener $inner |
52
|
|
|
* @param string $userHashHeader Must be lower-cased |
53
|
|
|
* @param array $userIdentifierHeaders Must be lower-cased |
54
|
|
|
*/ |
55
|
6 |
|
public function __construct(BaseSessionListener $inner, string $userHashHeader, array $userIdentifierHeaders) |
56
|
|
|
{ |
57
|
6 |
|
$this->inner = $inner; |
58
|
6 |
|
$this->userHashHeader = $userHashHeader; |
59
|
6 |
|
$this->userIdentifierHeaders = $userIdentifierHeaders; |
60
|
6 |
|
} |
61
|
|
|
|
62
|
1 |
|
public function onKernelRequest(GetResponseEvent $event) |
63
|
|
|
{ |
64
|
1 |
|
return $this->inner->onKernelRequest($event); |
65
|
|
|
} |
66
|
|
|
|
67
|
5 |
|
public function onKernelResponse(FilterResponseEvent $event) |
68
|
|
|
{ |
69
|
5 |
|
if (!$event->isMasterRequest()) { |
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
|
73
|
5 |
|
$varyHeaders = array_map('strtolower', $event->getResponse()->getVary()); |
74
|
5 |
|
$relevantHeaders = array_merge($this->userIdentifierHeaders, [$this->userHashHeader]); |
75
|
|
|
|
76
|
|
|
// Call default behaviour if it's an irrelevant request for the user context |
77
|
5 |
|
if (0 === count(array_intersect($varyHeaders, $relevantHeaders))) { |
78
|
2 |
|
$this->inner->onKernelResponse($event); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// noop, see class description |
82
|
5 |
|
} |
83
|
|
|
|
84
|
|
|
public static function getSubscribedEvents(): array |
85
|
|
|
{ |
86
|
|
|
return BaseSessionListener::getSubscribedEvents(); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|