|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\controller_annotations\EventSubscriber; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\HttpKernel\Event\FilterControllerEvent; |
|
6
|
|
|
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; |
|
7
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
9
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
10
|
|
|
use Symfony\Component\ExpressionLanguage\ExpressionLanguage; |
|
11
|
|
|
|
|
12
|
|
|
class HttpCacheEventSubscriber implements EventSubscriberInterface |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var \SplObjectStorage |
|
17
|
|
|
*/ |
|
18
|
|
|
private $lastModifiedDates; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var \SplObjectStorage |
|
22
|
|
|
*/ |
|
23
|
|
|
private $eTags; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var ExpressionLanguage |
|
27
|
|
|
*/ |
|
28
|
|
|
private $expressionLanguage; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
*/ |
|
32
|
19 |
|
public function __construct() |
|
33
|
|
|
{ |
|
34
|
19 |
|
$this->lastModifiedDates = new \SplObjectStorage(); |
|
35
|
19 |
|
$this->eTags = new \SplObjectStorage(); |
|
36
|
19 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Handles HTTP validation headers. |
|
40
|
|
|
* |
|
41
|
|
|
* @param FilterControllerEvent $event |
|
42
|
|
|
*/ |
|
43
|
10 |
|
public function onKernelController(FilterControllerEvent $event) |
|
44
|
|
|
{ |
|
45
|
10 |
|
$request = $event->getRequest(); |
|
46
|
10 |
|
if (!$configuration = $request->attributes->get('_cache')) { |
|
47
|
6 |
|
return; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
4 |
|
$response = new Response(); |
|
51
|
|
|
|
|
52
|
4 |
|
$lastModifiedDate = ''; |
|
53
|
4 |
|
if ($configuration->getLastModified()) { |
|
54
|
2 |
|
$lastModifiedDate = $this->getExpressionLanguage()->evaluate($configuration->getLastModified(), $request->attributes->all()); |
|
55
|
2 |
|
$response->setLastModified($lastModifiedDate); |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
4 |
|
$eTag = ''; |
|
59
|
4 |
|
if ($configuration->getETag()) { |
|
60
|
2 |
|
$eTag = hash('sha256', $this->getExpressionLanguage()->evaluate($configuration->getETag(), $request->attributes->all())); |
|
61
|
2 |
|
$response->setETag($eTag); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
4 |
|
if ($response->isNotModified($request)) { |
|
65
|
2 |
|
$event->setController(function () use ($response) { |
|
66
|
2 |
|
return $response; |
|
67
|
2 |
|
}); |
|
68
|
2 |
|
$event->stopPropagation(); |
|
69
|
|
|
} else { |
|
70
|
2 |
|
if ($eTag) { |
|
71
|
1 |
|
$this->eTags[$request] = $eTag; |
|
72
|
|
|
} |
|
73
|
2 |
|
if ($lastModifiedDate) { |
|
74
|
1 |
|
$this->lastModifiedDates[$request] = $lastModifiedDate; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
4 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Modifies the response to apply HTTP cache headers when needed. |
|
81
|
|
|
* |
|
82
|
|
|
* @param FilterResponseEvent $event |
|
83
|
|
|
*/ |
|
84
|
17 |
|
public function onKernelResponse(FilterResponseEvent $event) |
|
85
|
|
|
{ |
|
86
|
17 |
|
$request = $event->getRequest(); |
|
87
|
|
|
|
|
88
|
17 |
|
if (!$configuration = $request->attributes->get('_cache')) { |
|
89
|
7 |
|
return; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
10 |
|
$response = $event->getResponse(); |
|
93
|
|
|
|
|
94
|
|
|
// http://tools.ietf.org/html/draft-ietf-httpbis-p4-conditional-12#section-3.1 |
|
95
|
10 |
|
if (!in_array($response->getStatusCode(), array(200, 203, 300, 301, 302, 304, 404, 410))) { |
|
96
|
1 |
|
return; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
9 |
View Code Duplication |
if (null !== $age = $configuration->getSMaxAge()) { |
|
|
|
|
|
|
100
|
2 |
|
if (!is_numeric($age)) { |
|
101
|
1 |
|
$now = microtime(true); |
|
102
|
|
|
|
|
103
|
1 |
|
$age = ceil(strtotime($configuration->getSMaxAge(), $now) - $now); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
2 |
|
$response->setSharedMaxAge($age); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
9 |
View Code Duplication |
if (null !== $age = $configuration->getMaxAge()) { |
|
|
|
|
|
|
110
|
2 |
|
if (!is_numeric($age)) { |
|
111
|
1 |
|
$now = microtime(true); |
|
112
|
|
|
|
|
113
|
1 |
|
$age = ceil(strtotime($configuration->getMaxAge(), $now) - $now); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
2 |
|
$response->setMaxAge($age); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
9 |
|
if (null !== $configuration->getExpires()) { |
|
120
|
1 |
|
$date = \DateTime::createFromFormat('U', strtotime($configuration->getExpires()), new \DateTimeZone('UTC')); |
|
121
|
1 |
|
$response->setExpires($date); |
|
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
9 |
|
if (null !== $configuration->getVary()) { |
|
125
|
1 |
|
$response->setVary($configuration->getVary()); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
9 |
|
if ($configuration->isPublic()) { |
|
129
|
1 |
|
$response->setPublic(); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
9 |
|
if ($configuration->isPrivate()) { |
|
133
|
1 |
|
$response->setPrivate(); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
9 |
|
if (isset($this->lastModifiedDates[$request])) { |
|
137
|
1 |
|
$response->setLastModified($this->lastModifiedDates[$request]); |
|
138
|
|
|
|
|
139
|
1 |
|
unset($this->lastModifiedDates[$request]); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
9 |
|
if (isset($this->eTags[$request])) { |
|
143
|
1 |
|
$response->setETag($this->eTags[$request]); |
|
144
|
|
|
|
|
145
|
1 |
|
unset($this->eTags[$request]); |
|
146
|
|
|
} |
|
147
|
9 |
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @codeCoverageIgnore |
|
151
|
|
|
* @return ExpressionLanguage |
|
152
|
|
|
*/ |
|
153
|
|
|
private function getExpressionLanguage() |
|
154
|
|
|
{ |
|
155
|
|
|
if (null === $this->expressionLanguage) { |
|
156
|
|
|
if (!class_exists(ExpressionLanguage::class)) { |
|
157
|
|
|
throw new \RuntimeException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.'); |
|
158
|
|
|
} |
|
159
|
|
|
$this->expressionLanguage = new ExpressionLanguage(); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
return $this->expressionLanguage; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @return array |
|
167
|
|
|
*/ |
|
168
|
6 |
|
public static function getSubscribedEvents() |
|
169
|
|
|
{ |
|
170
|
|
|
return [ |
|
171
|
|
|
KernelEvents::CONTROLLER => [ |
|
172
|
|
|
['onKernelController', 0], |
|
173
|
6 |
|
], |
|
174
|
|
|
KernelEvents::RESPONSE => [ |
|
175
|
|
|
['onKernelResponse', 100], |
|
176
|
|
|
], |
|
177
|
|
|
]; |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: