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