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\DependencyInjection; |
13
|
|
|
|
14
|
|
|
use FOS\HttpCache\ProxyClient\HttpDispatcher; |
15
|
|
|
use FOS\HttpCache\ProxyClient\ProxyClient; |
16
|
|
|
use FOS\HttpCache\SymfonyCache\KernelDispatcher; |
17
|
|
|
use FOS\HttpCacheBundle\DependencyInjection\Compiler\HashGeneratorPass; |
18
|
|
|
use FOS\HttpCacheBundle\Http\ResponseMatcher\ExpressionResponseMatcher; |
19
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
20
|
|
|
use Symfony\Component\Config\FileLocator; |
21
|
|
|
use Symfony\Component\Console\Application; |
22
|
|
|
use Symfony\Component\DependencyInjection\ChildDefinition; |
23
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
24
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
25
|
|
|
use Symfony\Component\DependencyInjection\DefinitionDecorator; |
26
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
27
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
28
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
29
|
|
|
use Symfony\Component\HttpKernel\Kernel; |
30
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
|
|
class FOSHttpCacheExtension extends Extension |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
29 |
|
public function getConfiguration(array $config, ContainerBuilder $container) |
41
|
|
|
{ |
42
|
29 |
|
return new Configuration($container->getParameter('kernel.debug')); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
29 |
|
public function load(array $configs, ContainerBuilder $container) |
49
|
|
|
{ |
50
|
29 |
|
$configuration = $this->getConfiguration($configs, $container); |
51
|
29 |
|
$config = $this->processConfiguration($configuration, $configs); |
|
|
|
|
52
|
|
|
|
53
|
29 |
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
54
|
29 |
|
$loader->load('matcher.xml'); |
55
|
|
|
|
56
|
29 |
|
if ($config['debug']['enabled'] || (!empty($config['cache_control']))) { |
57
|
7 |
|
$debugHeader = $config['debug']['enabled'] ? $config['debug']['header'] : false; |
58
|
7 |
|
$container->setParameter('fos_http_cache.debug_header', $debugHeader); |
59
|
7 |
|
$loader->load('cache_control_listener.xml'); |
60
|
|
|
} |
61
|
|
|
|
62
|
29 |
|
$this->loadCacheable($container, $config['cacheable']); |
63
|
|
|
|
64
|
29 |
|
if (!empty($config['cache_control'])) { |
65
|
7 |
|
$this->loadCacheControl($container, $config['cache_control']); |
66
|
|
|
} |
67
|
|
|
|
68
|
28 |
|
if (isset($config['proxy_client'])) { |
69
|
19 |
|
$this->loadProxyClient($container, $loader, $config['proxy_client']); |
70
|
|
|
} |
71
|
|
|
|
72
|
27 |
|
if (isset($config['test'])) { |
73
|
2 |
|
$this->loadTest($container, $loader, $config['test']); |
74
|
|
|
} |
75
|
|
|
|
76
|
27 |
|
if ($config['cache_manager']['enabled']) { |
77
|
19 |
|
if (array_key_exists('custom_proxy_client', $config['cache_manager'])) { |
78
|
|
|
// overwrite the previously set alias, if a proxy client was also configured |
79
|
1 |
|
$container->setAlias( |
80
|
1 |
|
'fos_http_cache.default_proxy_client', |
81
|
1 |
|
$config['cache_manager']['custom_proxy_client'] |
82
|
|
|
); |
83
|
|
|
} |
84
|
19 |
|
if ('auto' === $config['cache_manager']['generate_url_type']) { |
85
|
19 |
|
if (array_key_exists('custom_proxy_client', $config['cache_manager'])) { |
86
|
1 |
|
$generateUrlType = UrlGeneratorInterface::ABSOLUTE_URL; |
87
|
|
|
} else { |
88
|
18 |
|
$defaultClient = $this->getDefaultProxyClient($config['proxy_client']); |
89
|
18 |
|
if ('noop' !== $defaultClient |
90
|
18 |
|
&& array_key_exists('base_url', $config['proxy_client'][$defaultClient])) { |
91
|
|
|
$generateUrlType = UrlGeneratorInterface::ABSOLUTE_PATH; |
92
|
|
|
} else { |
93
|
19 |
|
$generateUrlType = UrlGeneratorInterface::ABSOLUTE_URL; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} else { |
97
|
|
|
$generateUrlType = $config['cache_manager']['generate_url_type']; |
98
|
|
|
} |
99
|
19 |
|
$container->setParameter('fos_http_cache.cache_manager.generate_url_type', $generateUrlType); |
100
|
19 |
|
$loader->load('cache_manager.xml'); |
101
|
19 |
|
if (class_exists(Application::class)) { |
102
|
19 |
|
$loader->load('cache_manager_commands.xml'); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
27 |
|
if ($config['tags']['enabled']) { |
107
|
19 |
|
$this->loadCacheTagging( |
108
|
19 |
|
$container, |
109
|
19 |
|
$loader, |
110
|
19 |
|
$config['tags'], |
111
|
19 |
|
array_key_exists('proxy_client', $config) |
112
|
18 |
|
? $this->getDefaultProxyClient($config['proxy_client']) |
113
|
19 |
|
: 'custom' |
114
|
|
|
); |
115
|
|
|
} else { |
116
|
8 |
|
$container->setParameter('fos_http_cache.compiler_pass.tag_annotations', false); |
117
|
|
|
} |
118
|
|
|
|
119
|
26 |
|
if ($config['invalidation']['enabled']) { |
120
|
18 |
|
$loader->load('invalidation_listener.xml'); |
121
|
|
|
|
122
|
18 |
|
if (!empty($config['invalidation']['expression_language'])) { |
123
|
|
|
$container->setAlias( |
124
|
|
|
'fos_http_cache.invalidation.expression_language', |
125
|
|
|
$config['invalidation']['expression_language'] |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
|
129
|
18 |
|
if (!empty($config['invalidation']['rules'])) { |
130
|
3 |
|
$this->loadInvalidatorRules($container, $config['invalidation']['rules']); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
26 |
|
if ($config['user_context']['enabled']) { |
135
|
6 |
|
$this->loadUserContext($container, $loader, $config['user_context']); |
136
|
|
|
} |
137
|
|
|
|
138
|
26 |
|
if (!empty($config['flash_message']) && $config['flash_message']['enabled']) { |
139
|
3 |
|
unset($config['flash_message']['enabled']); |
140
|
3 |
|
$container->setParameter('fos_http_cache.event_listener.flash_message.options', $config['flash_message']); |
141
|
|
|
|
142
|
3 |
|
$loader->load('flash_message.xml'); |
143
|
|
|
} |
144
|
26 |
|
} |
145
|
|
|
|
146
|
29 |
|
private function loadCacheable(ContainerBuilder $container, array $config) |
147
|
|
|
{ |
148
|
29 |
|
$definition = $container->getDefinition('fos_http_cache.response_matcher.cacheable'); |
149
|
|
|
|
150
|
|
|
// Change CacheableResponseMatcher to ExpressionResponseMatcher |
151
|
29 |
|
if ($config['response']['expression']) { |
152
|
|
|
$definition->setClass(ExpressionResponseMatcher::class) |
153
|
|
|
->setArguments([$config['response']['expression']]); |
154
|
|
|
} else { |
155
|
29 |
|
$container->setParameter( |
156
|
29 |
|
'fos_http_cache.cacheable.response.additional_status', |
157
|
29 |
|
$config['response']['additional_status'] |
158
|
|
|
); |
159
|
|
|
} |
160
|
29 |
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param ContainerBuilder $container |
164
|
|
|
* @param array $config |
165
|
|
|
* |
166
|
|
|
* @throws InvalidConfigurationException |
167
|
|
|
*/ |
168
|
7 |
|
private function loadCacheControl(ContainerBuilder $container, array $config) |
169
|
|
|
{ |
170
|
7 |
|
$controlDefinition = $container->getDefinition('fos_http_cache.event_listener.cache_control'); |
171
|
|
|
|
172
|
7 |
|
foreach ($config['rules'] as $rule) { |
173
|
7 |
|
$ruleMatcher = $this->parseRuleMatcher($container, $rule['match']); |
174
|
|
|
|
175
|
7 |
|
if ('default' === $rule['headers']['overwrite']) { |
176
|
7 |
|
$rule['headers']['overwrite'] = $config['defaults']['overwrite']; |
177
|
|
|
} |
178
|
|
|
|
179
|
7 |
|
$controlDefinition->addMethodCall('addRule', [$ruleMatcher, $rule['headers']]); |
180
|
|
|
} |
181
|
6 |
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Parse one cache control rule match configuration. |
185
|
|
|
* |
186
|
|
|
* @param ContainerBuilder $container |
187
|
|
|
* @param array $match Request and response match criteria |
188
|
|
|
* |
189
|
|
|
* @return Reference pointing to a rule matcher service |
190
|
|
|
*/ |
191
|
7 |
|
private function parseRuleMatcher(ContainerBuilder $container, array $match) |
192
|
|
|
{ |
193
|
7 |
|
$requestMatcher = $this->parseRequestMatcher($container, $match); |
194
|
7 |
|
$responseMatcher = $this->parseResponseMatcher($container, $match); |
195
|
|
|
|
196
|
7 |
|
$signature = serialize([(string) $requestMatcher, (string) $responseMatcher]); |
197
|
7 |
|
$id = 'fos_http_cache.cache_control.rule_matcher.'.md5($signature); |
198
|
|
|
|
199
|
7 |
|
if ($container->hasDefinition($id)) { |
200
|
1 |
|
throw new InvalidConfigurationException('Duplicate match criteria. Would be hidden by a previous rule. match: '.json_encode($match)); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
$container |
204
|
7 |
|
->setDefinition($id, $this->createChildDefinition('fos_http_cache.rule_matcher')) |
205
|
7 |
|
->replaceArgument(0, $requestMatcher) |
206
|
7 |
|
->replaceArgument(1, $responseMatcher) |
207
|
|
|
; |
208
|
|
|
|
209
|
7 |
|
return new Reference($id); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Used for cache control, tag and invalidation rules. |
214
|
|
|
* |
215
|
|
|
* @param ContainerBuilder $container |
216
|
|
|
* @param array $match |
217
|
|
|
* |
218
|
|
|
* @return Reference to the request matcher |
219
|
|
|
*/ |
220
|
9 |
|
private function parseRequestMatcher(ContainerBuilder $container, array $match) |
221
|
|
|
{ |
222
|
9 |
|
$match['ips'] = (empty($match['ips'])) ? null : $match['ips']; |
223
|
|
|
|
224
|
|
|
$arguments = [ |
225
|
9 |
|
$match['path'], |
226
|
9 |
|
$match['host'], |
227
|
9 |
|
$match['methods'], |
228
|
9 |
|
$match['ips'], |
229
|
9 |
|
$match['attributes'], |
230
|
|
|
]; |
231
|
9 |
|
$serialized = serialize($arguments); |
232
|
9 |
|
$id = 'fos_http_cache.request_matcher.'.md5($serialized).sha1($serialized); |
233
|
|
|
|
234
|
9 |
|
if (!$container->hasDefinition($id)) { |
235
|
|
|
$container |
236
|
9 |
|
->setDefinition($id, $this->createChildDefinition('fos_http_cache.request_matcher')) |
237
|
9 |
|
->setArguments($arguments) |
238
|
|
|
; |
239
|
|
|
|
240
|
9 |
|
if (!empty($match['query_string'])) { |
241
|
|
|
$container->getDefinition($id)->addMethodCall('setQueryString', [$match['query_string']]); |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
245
|
9 |
|
return new Reference($id); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Used only for cache control rules. |
250
|
|
|
* |
251
|
|
|
* @param ContainerBuilder $container |
252
|
|
|
* @param array $config |
253
|
|
|
* |
254
|
|
|
* @return Reference to the correct response matcher service |
255
|
|
|
*/ |
256
|
7 |
|
private function parseResponseMatcher(ContainerBuilder $container, array $config) |
257
|
|
|
{ |
258
|
7 |
|
if (!empty($config['additional_response_status'])) { |
259
|
1 |
|
$id = 'fos_http_cache.cache_control.expression.'.md5(serialize($config['additional_response_status'])); |
260
|
1 |
|
if (!$container->hasDefinition($id)) { |
261
|
|
|
$container |
262
|
1 |
|
->setDefinition($id, $this->createChildDefinition('fos_http_cache.response_matcher.cache_control.cacheable_response')) |
263
|
1 |
|
->setArguments([$config['additional_response_status']]) |
264
|
|
|
; |
265
|
|
|
} |
266
|
6 |
|
} elseif (!empty($config['match_response'])) { |
267
|
2 |
|
$id = 'fos_http_cache.cache_control.match_response.'.md5($config['match_response']); |
268
|
2 |
|
if (!$container->hasDefinition($id)) { |
269
|
|
|
$container |
270
|
2 |
|
->setDefinition($id, $this->createChildDefinition('fos_http_cache.response_matcher.cache_control.expression')) |
271
|
2 |
|
->replaceArgument(0, $config['match_response']) |
272
|
|
|
; |
273
|
|
|
} |
274
|
|
|
} else { |
275
|
4 |
|
$id = 'fos_http_cache.response_matcher.cacheable'; |
276
|
|
|
} |
277
|
|
|
|
278
|
7 |
|
return new Reference($id); |
279
|
|
|
} |
280
|
|
|
|
281
|
6 |
|
private function loadUserContext(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
282
|
|
|
{ |
283
|
6 |
|
$configuredUserIdentifierHeaders = array_map('strtolower', $config['user_identifier_headers']); |
284
|
6 |
|
$completeUserIdentifierHeaders = $configuredUserIdentifierHeaders; |
285
|
6 |
|
if (false !== $config['session_name_prefix'] && !in_array('cookie', $completeUserIdentifierHeaders)) { |
286
|
|
|
$completeUserIdentifierHeaders[] = 'cookie'; |
287
|
|
|
} |
288
|
|
|
|
289
|
6 |
|
$loader->load('user_context.xml'); |
290
|
|
|
|
291
|
6 |
|
$container->getDefinition('fos_http_cache.user_context.request_matcher') |
292
|
6 |
|
->replaceArgument(0, $config['match']['accept']) |
293
|
6 |
|
->replaceArgument(1, $config['match']['method']); |
294
|
|
|
|
295
|
6 |
|
$container->setParameter('fos_http_cache.event_listener.user_context.options', [ |
296
|
6 |
|
'user_identifier_headers' => $completeUserIdentifierHeaders, |
297
|
6 |
|
'user_hash_header' => $config['user_hash_header'], |
298
|
6 |
|
'ttl' => $config['hash_cache_ttl'], |
299
|
6 |
|
'add_vary_on_hash' => $config['always_vary_on_context_hash'], |
300
|
|
|
]); |
301
|
|
|
|
302
|
6 |
|
$container->getDefinition('fos_http_cache.event_listener.user_context') |
303
|
6 |
|
->replaceArgument(0, new Reference($config['match']['matcher_service'])) |
304
|
|
|
; |
305
|
|
|
|
306
|
|
|
$options = [ |
307
|
6 |
|
'user_identifier_headers' => $configuredUserIdentifierHeaders, |
308
|
6 |
|
'session_name_prefix' => $config['session_name_prefix'], |
309
|
|
|
]; |
310
|
6 |
|
$container->getDefinition('fos_http_cache.user_context.anonymous_request_matcher') |
311
|
6 |
|
->replaceArgument(0, $options); |
312
|
|
|
|
313
|
6 |
|
if ($config['logout_handler']['enabled']) { |
314
|
5 |
|
$container->getDefinition('fos_http_cache.user_context_invalidator') |
315
|
5 |
|
->replaceArgument(1, $completeUserIdentifierHeaders) |
316
|
5 |
|
->replaceArgument(2, $config['match']['accept']); |
317
|
|
|
|
318
|
5 |
|
$container->setAlias('security.logout.handler.session', 'fos_http_cache.user_context.session_logout_handler'); |
319
|
|
|
} else { |
320
|
1 |
|
$container->removeDefinition('fos_http_cache.user_context.logout_handler'); |
321
|
1 |
|
$container->removeDefinition('fos_http_cache.user_context.session_logout_handler'); |
322
|
1 |
|
$container->removeDefinition('fos_http_cache.user_context_invalidator'); |
323
|
|
|
} |
324
|
|
|
|
325
|
6 |
|
if ($config['role_provider']) { |
326
|
4 |
|
$container->getDefinition('fos_http_cache.user_context.role_provider') |
327
|
4 |
|
->addTag(HashGeneratorPass::TAG_NAME) |
328
|
4 |
|
->setAbstract(false); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
// Only decorate default SessionListener for Symfony 3.4 - 4.0 |
332
|
|
|
// For Symfony 4.1+, the UserContextListener sets the header that tells |
333
|
|
|
// the SessionListener to leave the cache-control header unchanged. |
334
|
6 |
|
if (version_compare(Kernel::VERSION, '3.4', '>=') |
335
|
6 |
|
&& version_compare(Kernel::VERSION, '4.1', '<') |
336
|
|
|
) { |
337
|
|
|
$container->getDefinition('fos_http_cache.user_context.session_listener') |
338
|
|
|
->setArgument(1, strtolower($config['user_hash_header'])) |
339
|
|
|
->setArgument(2, $completeUserIdentifierHeaders); |
340
|
|
|
} else { |
341
|
6 |
|
$container->removeDefinition('fos_http_cache.user_context.session_listener'); |
342
|
|
|
} |
343
|
6 |
|
} |
344
|
|
|
|
345
|
19 |
|
private function loadProxyClient(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
346
|
|
|
{ |
347
|
19 |
|
if (isset($config['varnish'])) { |
348
|
14 |
|
$this->loadVarnish($container, $loader, $config['varnish']); |
349
|
|
|
} |
350
|
18 |
|
if (isset($config['nginx'])) { |
351
|
2 |
|
$this->loadNginx($container, $loader, $config['nginx']); |
352
|
|
|
} |
353
|
18 |
|
if (isset($config['symfony'])) { |
354
|
2 |
|
$this->loadSymfony($container, $loader, $config['symfony']); |
355
|
|
|
} |
356
|
18 |
|
if (isset($config['noop'])) { |
357
|
1 |
|
$loader->load('noop.xml'); |
358
|
|
|
} |
359
|
|
|
|
360
|
18 |
|
$container->setAlias( |
361
|
18 |
|
'fos_http_cache.default_proxy_client', |
362
|
18 |
|
'fos_http_cache.proxy_client.'.$this->getDefaultProxyClient($config) |
363
|
|
|
); |
364
|
18 |
|
$container->setAlias( |
365
|
18 |
|
ProxyClient::class, |
366
|
18 |
|
'fos_http_cache.default_proxy_client' |
367
|
|
|
); |
368
|
18 |
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* Define the http dispatcher service for the proxy client $name. |
372
|
|
|
* |
373
|
|
|
* @param ContainerBuilder $container |
374
|
|
|
* @param array $config |
375
|
|
|
* @param string $serviceName |
376
|
|
|
*/ |
377
|
17 |
|
private function createHttpDispatcherDefinition(ContainerBuilder $container, array $config, $serviceName) |
378
|
|
|
{ |
379
|
17 |
|
foreach ($config['servers'] as $url) { |
380
|
17 |
|
$this->validateUrl($url, 'Not a valid Varnish server address: "%s"'); |
381
|
|
|
} |
382
|
17 |
|
if (!empty($config['base_url'])) { |
383
|
17 |
|
$baseUrl = $this->prefixSchema($config['base_url']); |
384
|
17 |
|
$this->validateUrl($baseUrl, 'Not a valid base path: "%s"'); |
385
|
|
|
} else { |
386
|
|
|
$baseUrl = null; |
387
|
|
|
} |
388
|
16 |
|
$httpClient = null; |
389
|
16 |
|
if ($config['http_client']) { |
390
|
1 |
|
$httpClient = new Reference($config['http_client']); |
391
|
|
|
} |
392
|
|
|
|
393
|
16 |
|
$definition = new Definition(HttpDispatcher::class, [ |
394
|
16 |
|
$config['servers'], |
395
|
16 |
|
$baseUrl, |
396
|
16 |
|
$httpClient, |
397
|
|
|
]); |
398
|
|
|
|
399
|
16 |
|
$container->setDefinition($serviceName, $definition); |
400
|
16 |
|
} |
401
|
|
|
|
402
|
14 |
|
private function loadVarnish(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
403
|
|
|
{ |
404
|
14 |
|
$this->createHttpDispatcherDefinition($container, $config['http'], 'fos_http_cache.proxy_client.varnish.http_dispatcher'); |
405
|
|
|
$options = [ |
406
|
13 |
|
'tags_header' => $config['tags_header'], |
407
|
|
|
]; |
408
|
13 |
|
if (!empty($config['header_length'])) { |
409
|
|
|
$options['header_length'] = $config['header_length']; |
410
|
|
|
} |
411
|
13 |
|
if (!empty($config['default_ban_headers'])) { |
412
|
|
|
$options['default_ban_headers'] = $config['default_ban_headers']; |
413
|
|
|
} |
414
|
13 |
|
$container->setParameter('fos_http_cache.proxy_client.varnish.options', $options); |
415
|
|
|
|
416
|
13 |
|
$loader->load('varnish.xml'); |
417
|
13 |
|
} |
418
|
|
|
|
419
|
2 |
|
private function loadNginx(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
420
|
|
|
{ |
421
|
2 |
|
$this->createHttpDispatcherDefinition($container, $config['http'], 'fos_http_cache.proxy_client.nginx.http_dispatcher'); |
422
|
2 |
|
$container->setParameter('fos_http_cache.proxy_client.nginx.options', [ |
423
|
2 |
|
'purge_location' => $config['purge_location'], |
424
|
|
|
]); |
425
|
2 |
|
$loader->load('nginx.xml'); |
426
|
2 |
|
} |
427
|
|
|
|
428
|
2 |
|
private function loadSymfony(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
429
|
|
|
{ |
430
|
2 |
|
$serviceName = 'fos_http_cache.proxy_client.symfony.http_dispatcher'; |
431
|
|
|
|
432
|
2 |
|
if ($config['use_kernel_dispatcher']) { |
433
|
1 |
|
$definition = new Definition(KernelDispatcher::class, [ |
434
|
1 |
|
new Reference('kernel'), |
435
|
|
|
]); |
436
|
1 |
|
$container->setDefinition($serviceName, $definition); |
437
|
|
|
} else { |
438
|
1 |
|
$this->createHttpDispatcherDefinition($container, $config['http'], $serviceName); |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
$options = [ |
442
|
2 |
|
'tags_header' => $config['tags_header'], |
443
|
2 |
|
'tags_method' => $config['tags_method'], |
444
|
2 |
|
'purge_method' => $config['purge_method'], |
445
|
|
|
]; |
446
|
2 |
|
if (!empty($config['header_length'])) { |
447
|
|
|
$options['header_length'] = $config['header_length']; |
448
|
|
|
} |
449
|
2 |
|
$container->setParameter('fos_http_cache.proxy_client.symfony.options', $options); |
450
|
|
|
|
451
|
2 |
|
$loader->load('symfony.xml'); |
452
|
2 |
|
} |
453
|
|
|
|
454
|
|
|
/** |
455
|
|
|
* @param ContainerBuilder $container |
456
|
|
|
* @param XmlFileLoader $loader |
457
|
|
|
* @param array $config Configuration section for the tags node |
458
|
|
|
* @param string $client Name of the client used with the cache manager, |
459
|
|
|
* "custom" when a custom client is used |
460
|
|
|
*/ |
461
|
19 |
|
private function loadCacheTagging(ContainerBuilder $container, XmlFileLoader $loader, array $config, $client) |
462
|
|
|
{ |
463
|
19 |
|
if ('auto' === $config['enabled'] && !in_array($client, ['varnish', 'symfony'])) { |
464
|
3 |
|
$container->setParameter('fos_http_cache.compiler_pass.tag_annotations', false); |
465
|
|
|
|
466
|
3 |
|
return; |
467
|
|
|
} |
468
|
16 |
|
if (!in_array($client, ['varnish', 'symfony', 'custom', 'noop'])) { |
469
|
1 |
|
throw new InvalidConfigurationException(sprintf('You can not enable cache tagging with the %s client', $client)); |
470
|
|
|
} |
471
|
|
|
|
472
|
15 |
|
$container->setParameter('fos_http_cache.compiler_pass.tag_annotations', true); |
473
|
15 |
|
$container->setParameter('fos_http_cache.tag_handler.response_header', $config['response_header']); |
474
|
15 |
|
$container->setParameter('fos_http_cache.tag_handler.strict', $config['strict']); |
475
|
15 |
|
$loader->load('cache_tagging.xml'); |
476
|
15 |
|
if (class_exists(Application::class)) { |
477
|
15 |
|
$loader->load('cache_tagging_commands.xml'); |
478
|
|
|
} |
479
|
|
|
|
480
|
15 |
|
if (!empty($config['expression_language'])) { |
481
|
|
|
$container->setAlias( |
482
|
|
|
'fos_http_cache.tag_handler.expression_language', |
483
|
|
|
$config['expression_language'] |
484
|
|
|
); |
485
|
|
|
} |
486
|
|
|
|
487
|
15 |
|
if (!empty($config['rules'])) { |
488
|
3 |
|
$this->loadTagRules($container, $config['rules']); |
489
|
|
|
} |
490
|
15 |
|
} |
491
|
|
|
|
492
|
2 |
|
private function loadTest(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
493
|
|
|
{ |
494
|
2 |
|
$container->setParameter('fos_http_cache.test.cache_header', $config['cache_header']); |
495
|
|
|
|
496
|
2 |
|
if ($config['proxy_server']) { |
497
|
2 |
|
$this->loadProxyServer($container, $loader, $config['proxy_server']); |
498
|
|
|
} |
499
|
2 |
|
} |
500
|
|
|
|
501
|
2 |
|
private function loadProxyServer(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
502
|
|
|
{ |
503
|
2 |
|
if (isset($config['varnish'])) { |
504
|
2 |
|
$this->loadVarnishProxyServer($container, $loader, $config['varnish']); |
505
|
|
|
} |
506
|
|
|
|
507
|
2 |
|
if (isset($config['nginx'])) { |
508
|
|
|
$this->loadNginxProxyServer($container, $loader, $config['nginx']); |
509
|
|
|
} |
510
|
|
|
|
511
|
2 |
|
$container->setAlias( |
512
|
2 |
|
'fos_http_cache.test.default_proxy_server', |
513
|
2 |
|
'fos_http_cache.test.proxy_server.'.$this->getDefaultProxyClient($config) |
514
|
|
|
); |
515
|
2 |
|
} |
516
|
|
|
|
517
|
2 |
|
private function loadVarnishProxyServer(ContainerBuilder $container, XmlFileLoader $loader, $config) |
518
|
|
|
{ |
519
|
2 |
|
$loader->load('varnish_proxy.xml'); |
520
|
2 |
|
foreach ($config as $key => $value) { |
521
|
2 |
|
$container->setParameter( |
522
|
2 |
|
'fos_http_cache.test.proxy_server.varnish.'.$key, |
523
|
2 |
|
$value |
524
|
|
|
); |
525
|
|
|
} |
526
|
2 |
|
} |
527
|
|
|
|
528
|
|
|
private function loadNginxProxyServer(ContainerBuilder $container, XmlFileLoader $loader, $config) |
529
|
|
|
{ |
530
|
|
|
$loader->load('nginx_proxy.xml'); |
531
|
|
|
foreach ($config as $key => $value) { |
532
|
|
|
$container->setParameter( |
533
|
|
|
'fos_http_cache.test.proxy_server.nginx.'.$key, |
534
|
|
|
$value |
535
|
|
|
); |
536
|
|
|
} |
537
|
|
|
} |
538
|
|
|
|
539
|
3 |
|
private function loadTagRules(ContainerBuilder $container, array $config) |
540
|
|
|
{ |
541
|
3 |
|
$tagDefinition = $container->getDefinition('fos_http_cache.event_listener.tag'); |
542
|
|
|
|
543
|
3 |
|
foreach ($config as $rule) { |
544
|
3 |
|
$ruleMatcher = $this->parseRequestMatcher($container, $rule['match']); |
545
|
|
|
|
546
|
|
|
$tags = [ |
547
|
3 |
|
'tags' => $rule['tags'], |
548
|
3 |
|
'expressions' => $rule['tag_expressions'], |
549
|
|
|
]; |
550
|
|
|
|
551
|
3 |
|
$tagDefinition->addMethodCall('addRule', [$ruleMatcher, $tags]); |
552
|
|
|
} |
553
|
3 |
|
} |
554
|
|
|
|
555
|
3 |
|
private function loadInvalidatorRules(ContainerBuilder $container, array $config) |
556
|
|
|
{ |
557
|
3 |
|
$tagDefinition = $container->getDefinition('fos_http_cache.event_listener.invalidation'); |
558
|
|
|
|
559
|
3 |
|
foreach ($config as $rule) { |
560
|
3 |
|
$ruleMatcher = $this->parseRequestMatcher($container, $rule['match']); |
561
|
3 |
|
$tagDefinition->addMethodCall('addRule', [$ruleMatcher, $rule['routes']]); |
562
|
|
|
} |
563
|
3 |
|
} |
564
|
|
|
|
565
|
17 |
|
private function validateUrl($url, $msg) |
566
|
|
|
{ |
567
|
17 |
|
$prefixed = $this->prefixSchema($url); |
568
|
|
|
|
569
|
17 |
|
if (!$parts = parse_url($prefixed)) { |
570
|
1 |
|
throw new InvalidConfigurationException(sprintf($msg, $url)); |
571
|
|
|
} |
572
|
17 |
|
} |
573
|
|
|
|
574
|
17 |
|
private function prefixSchema($url) |
575
|
|
|
{ |
576
|
17 |
|
if (false === strpos($url, '://')) { |
577
|
17 |
|
$url = sprintf('%s://%s', 'http', $url); |
578
|
|
|
} |
579
|
|
|
|
580
|
17 |
|
return $url; |
581
|
|
|
} |
582
|
|
|
|
583
|
18 |
|
private function getDefaultProxyClient(array $config) |
584
|
|
|
{ |
585
|
18 |
|
if (isset($config['default'])) { |
586
|
|
|
return $config['default']; |
587
|
|
|
} |
588
|
|
|
|
589
|
18 |
|
if (isset($config['varnish'])) { |
590
|
13 |
|
return 'varnish'; |
591
|
|
|
} |
592
|
|
|
|
593
|
5 |
|
if (isset($config['nginx'])) { |
594
|
2 |
|
return 'nginx'; |
595
|
|
|
} |
596
|
|
|
|
597
|
3 |
|
if (isset($config['symfony'])) { |
598
|
2 |
|
return 'symfony'; |
599
|
|
|
} |
600
|
|
|
|
601
|
1 |
|
if (isset($config['noop'])) { |
602
|
1 |
|
return 'noop'; |
603
|
|
|
} |
604
|
|
|
|
605
|
|
|
throw new InvalidConfigurationException('No proxy client configured'); |
606
|
|
|
} |
607
|
|
|
|
608
|
|
|
/** |
609
|
|
|
* Build the child definition with fallback for Symfony versions < 3.3. |
610
|
|
|
* |
611
|
|
|
* @param string $id Id of the service to extend |
612
|
|
|
* |
613
|
|
|
* @return ChildDefinition|DefinitionDecorator |
614
|
|
|
*/ |
615
|
9 |
|
private function createChildDefinition($id) |
616
|
|
|
{ |
617
|
9 |
|
if (class_exists(ChildDefinition::class)) { |
618
|
9 |
|
return new ChildDefinition($id); |
619
|
|
|
} |
620
|
|
|
|
621
|
|
|
return new DefinitionDecorator($id); |
|
|
|
|
622
|
|
|
} |
623
|
|
|
} |
624
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: