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
|
6 |
|
public function __construct() |
33
|
|
|
{ |
34
|
6 |
|
$this->lastModifiedDates = new \SplObjectStorage(); |
35
|
6 |
|
$this->etags = new \SplObjectStorage(); |
36
|
6 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Handles HTTP validation headers. |
40
|
|
|
* |
41
|
|
|
* @param FilterControllerEvent $event |
42
|
|
|
*/ |
43
|
6 |
|
public function onKernelController(FilterControllerEvent $event) |
44
|
|
|
{ |
45
|
6 |
|
$request = $event->getRequest(); |
46
|
6 |
|
if (!$configuration = $request->attributes->get('_cache')) { |
47
|
6 |
|
return; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$response = new Response(); |
51
|
|
|
|
52
|
|
|
$lastModifiedDate = ''; |
53
|
|
|
if ($configuration->getLastModified()) { |
54
|
|
|
$lastModifiedDate = $this->getExpressionLanguage()->evaluate($configuration->getLastModified(), $request->attributes->all()); |
55
|
|
|
$response->setLastModified($lastModifiedDate); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$etag = ''; |
59
|
|
|
if ($configuration->getETag()) { |
60
|
|
|
$etag = hash('sha256', $this->getExpressionLanguage()->evaluate($configuration->getETag(), $request->attributes->all())); |
61
|
|
|
$response->setETag($etag); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ($response->isNotModified($request)) { |
65
|
|
|
$event->setController(function () use ($response) { |
66
|
|
|
return $response; |
67
|
|
|
}); |
68
|
|
|
$event->stopPropagation(); |
69
|
|
|
} else { |
70
|
|
|
if ($etag) { |
71
|
|
|
$this->etags[$request] = $etag; |
72
|
|
|
} |
73
|
|
|
if ($lastModifiedDate) { |
74
|
|
|
$this->lastModifiedDates[$request] = $lastModifiedDate; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Modifies the response to apply HTTP cache headers when needed. |
81
|
|
|
* |
82
|
|
|
* @param FilterResponseEvent $event |
83
|
|
|
*/ |
84
|
6 |
|
public function onKernelResponse(FilterResponseEvent $event) |
85
|
|
|
{ |
86
|
6 |
|
$request = $event->getRequest(); |
87
|
|
|
|
88
|
6 |
|
if (!$configuration = $request->attributes->get('_cache')) { |
89
|
6 |
|
return; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$response = $event->getResponse(); |
93
|
|
|
|
94
|
|
|
// http://tools.ietf.org/html/draft-ietf-httpbis-p4-conditional-12#section-3.1 |
95
|
|
|
if (!in_array($response->getStatusCode(), array(200, 203, 300, 301, 302, 304, 404, 410))) { |
96
|
|
|
return; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
View Code Duplication |
if (null !== $age = $configuration->getSMaxAge()) { |
|
|
|
|
100
|
|
|
if (!is_numeric($age)) { |
101
|
|
|
$now = microtime(true); |
102
|
|
|
|
103
|
|
|
$age = ceil(strtotime($configuration->getSMaxAge(), $now) - $now); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$response->setSharedMaxAge($age); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
View Code Duplication |
if (null !== $age = $configuration->getMaxAge()) { |
|
|
|
|
110
|
|
|
if (!is_numeric($age)) { |
111
|
|
|
$now = microtime(true); |
112
|
|
|
|
113
|
|
|
$age = ceil(strtotime($configuration->getMaxAge(), $now) - $now); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$response->setMaxAge($age); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if (null !== $configuration->getExpires()) { |
120
|
|
|
$date = \DateTime::createFromFormat('U', strtotime($configuration->getExpires()), new \DateTimeZone('UTC')); |
121
|
|
|
$response->setExpires($date); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if (null !== $configuration->getVary()) { |
125
|
|
|
$response->setVary($configuration->getVary()); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if ($configuration->isPublic()) { |
129
|
|
|
$response->setPublic(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
if ($configuration->isPrivate()) { |
133
|
|
|
$response->setPrivate(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if (isset($this->lastModifiedDates[$request])) { |
137
|
|
|
$response->setLastModified($this->lastModifiedDates[$request]); |
138
|
|
|
|
139
|
|
|
unset($this->lastModifiedDates[$request]); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
if (isset($this->etags[$request])) { |
143
|
|
|
$response->setETag($this->etags[$request]); |
144
|
|
|
|
145
|
|
|
unset($this->etags[$request]); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return ExpressionLanguage |
151
|
|
|
*/ |
152
|
|
|
private function getExpressionLanguage() |
153
|
|
|
{ |
154
|
|
|
if (null === $this->expressionLanguage) { |
155
|
|
|
if (!class_exists(ExpressionLanguage::class)) { |
156
|
|
|
throw new \RuntimeException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.'); |
157
|
|
|
} |
158
|
|
|
$this->expressionLanguage = new ExpressionLanguage(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $this->expressionLanguage; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @return array |
166
|
|
|
*/ |
167
|
6 |
|
public static function getSubscribedEvents() |
168
|
|
|
{ |
169
|
|
|
return [ |
170
|
|
|
KernelEvents::CONTROLLER => [ |
171
|
|
|
['onKernelController', 0], |
172
|
6 |
|
], |
173
|
|
|
KernelEvents::RESPONSE => [ |
174
|
|
|
['onKernelResponse', 100], |
175
|
|
|
], |
176
|
|
|
]; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.