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