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 $hashHeader; |
||
53 | |||
54 | /** |
||
55 | * @var integer |
||
56 | */ |
||
57 | private $ttl; |
||
58 | |||
59 | /** |
||
60 | * @var bool |
||
61 | */ |
||
62 | private $addVaryOnHash; |
||
63 | |||
64 | 23 | public function __construct( |
|
65 | RequestMatcherInterface $requestMatcher, |
||
66 | HashGenerator $hashGenerator, |
||
67 | array $userIdentifierHeaders = array('Cookie', 'Authorization'), |
||
68 | $hashHeader = "X-User-Context-Hash", |
||
69 | $ttl = 0, |
||
70 | $addVaryOnHash = true |
||
71 | ) { |
||
72 | 23 | if (!count($userIdentifierHeaders)) { |
|
73 | 1 | throw new \InvalidArgumentException('The user context must vary on some request headers'); |
|
74 | } |
||
75 | 22 | $this->requestMatcher = $requestMatcher; |
|
76 | 22 | $this->hashGenerator = $hashGenerator; |
|
77 | 22 | $this->userIdentifierHeaders = $userIdentifierHeaders; |
|
78 | 22 | $this->hashHeader = $hashHeader; |
|
79 | 22 | $this->ttl = $ttl; |
|
80 | 22 | $this->addVaryOnHash = $addVaryOnHash; |
|
81 | 22 | } |
|
82 | |||
83 | /** |
||
84 | * Return the response to the context hash request with a header containing |
||
85 | * the generated hash. |
||
86 | * |
||
87 | * If the ttl is bigger than 0, cache headers will be set for this response. |
||
88 | * |
||
89 | * @param GetResponseEvent $event |
||
90 | */ |
||
91 | 18 | public function onKernelRequest(GetResponseEvent $event) |
|
120 | |||
121 | /** |
||
122 | * Add the context hash header to the headers to vary on if the header was |
||
123 | * present in the request. |
||
124 | * |
||
125 | * @param FilterResponseEvent $event |
||
126 | */ |
||
127 | 18 | public function onKernelResponse(FilterResponseEvent $event) |
|
152 | |||
153 | /** |
||
154 | * {@inheritdoc} |
||
155 | */ |
||
156 | 23 | public static function getSubscribedEvents() |
|
163 | } |
||
164 |