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