1 | <?php |
||
32 | class UserContextSubscriber implements EventSubscriberInterface |
||
33 | { |
||
34 | /** |
||
35 | * @var RequestMatcherInterface |
||
36 | */ |
||
37 | private $requestMatcher; |
||
38 | |||
39 | /** |
||
40 | * @var HashGenerator |
||
41 | */ |
||
42 | private $hashGenerator; |
||
43 | |||
44 | /** |
||
45 | * @var string[] |
||
46 | */ |
||
47 | private $userIdentifierHeaders; |
||
48 | |||
49 | /** |
||
50 | * @var string |
||
51 | */ |
||
52 | private $hash; |
||
53 | |||
54 | /** |
||
55 | * @var string |
||
56 | */ |
||
57 | private $hashHeader; |
||
58 | |||
59 | /** |
||
60 | * @var int |
||
61 | */ |
||
62 | private $ttl; |
||
63 | |||
64 | /** |
||
65 | * @var bool Whether to automatically add the Vary header for the hash / user identifier if there was no hash. |
||
66 | */ |
||
67 | private $addVaryOnHash; |
||
68 | |||
69 | 25 | public function __construct( |
|
70 | RequestMatcherInterface $requestMatcher, |
||
71 | HashGenerator $hashGenerator, |
||
72 | array $userIdentifierHeaders = array('Cookie', 'Authorization'), |
||
73 | $hashHeader = 'X-User-Context-Hash', |
||
74 | $ttl = 0, |
||
75 | $addVaryOnHash = true |
||
76 | ) { |
||
77 | 25 | if (!count($userIdentifierHeaders)) { |
|
78 | 1 | throw new \InvalidArgumentException('The user context must vary on some request headers'); |
|
79 | } |
||
80 | 24 | $this->requestMatcher = $requestMatcher; |
|
81 | 24 | $this->hashGenerator = $hashGenerator; |
|
82 | 24 | $this->userIdentifierHeaders = $userIdentifierHeaders; |
|
83 | 24 | $this->hashHeader = $hashHeader; |
|
84 | 24 | $this->ttl = $ttl; |
|
85 | 24 | $this->addVaryOnHash = $addVaryOnHash; |
|
86 | 24 | } |
|
87 | |||
88 | /** |
||
89 | * Return the response to the context hash request with a header containing |
||
90 | * the generated hash. |
||
91 | * |
||
92 | * If the ttl is bigger than 0, cache headers will be set for this response. |
||
93 | * |
||
94 | * @param GetResponseEvent $event |
||
95 | */ |
||
96 | 20 | public function onKernelRequest(GetResponseEvent $event) |
|
129 | |||
130 | /** |
||
131 | * Add the context hash header to the headers to vary on if the header was |
||
132 | * present in the request. |
||
133 | * |
||
134 | * @param FilterResponseEvent $event |
||
135 | */ |
||
136 | 20 | public function onKernelResponse(FilterResponseEvent $event) |
|
175 | |||
176 | /** |
||
177 | * {@inheritdoc} |
||
178 | */ |
||
179 | 23 | public static function getSubscribedEvents() |
|
186 | } |
||
187 |