Completed
Pull Request — master (#444)
by
unknown
04:46
created

FOSHttpCacheExtension::loadTagRules()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 2
crap 2
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);
0 ignored issues
show
Bug introduced by
It seems like $configuration defined by $this->getConfiguration($configs, $container) on line 48 can be null; however, Symfony\Component\Depend...:processConfiguration() does not accept null, maybe add an additional type check?

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:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
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)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 6
            && $container->hasDefinition('session_listener')
335
        ) {
336
            $container->getDefinition('fos_http_cache.user_context.session_listener')
337
                ->setArgument(1, strtolower($config['user_hash_header']))
338
                ->setArgument(2, $completeUserIdentifierHeaders);
339
        } else {
340 6
            $container->removeDefinition('fos_http_cache.user_context.session_listener');
341
        }
342 6
    }
343
344 18
    private function loadProxyClient(ContainerBuilder $container, XmlFileLoader $loader, array $config)
345
    {
346 18
        if (isset($config['varnish'])) {
347 14
            $this->loadVarnish($container, $loader, $config['varnish']);
348
        }
349 17
        if (isset($config['nginx'])) {
350 2
            $this->loadNginx($container, $loader, $config['nginx']);
351
        }
352 17
        if (isset($config['symfony'])) {
353 1
            $this->loadSymfony($container, $loader, $config['symfony']);
354
        }
355 17
        if (isset($config['noop'])) {
356 1
            $loader->load('noop.xml');
357
        }
358
359 17
        $container->setAlias(
360 17
            $this->getAlias().'.default_proxy_client',
361 17
            $this->getAlias().'.proxy_client.'.$this->getDefaultProxyClient($config)
362
        );
363 17
    }
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
     */
372 17
    private function createHttpDispatcherDefinition(ContainerBuilder $container, array $config, $serviceName)
373
    {
374 17
        foreach ($config['servers'] as $url) {
375 17
            $this->validateUrl($url, 'Not a valid Varnish server address: "%s"');
376
        }
377 17
        if (!empty($config['base_url'])) {
378 17
            $baseUrl = $this->prefixSchema($config['base_url']);
379 17
            $this->validateUrl($baseUrl, 'Not a valid base path: "%s"');
380
        } else {
381
            $baseUrl = null;
382
        }
383 16
        $httpClient = null;
384 16
        if ($config['http_client']) {
385 1
            $httpClient = new Reference($config['http_client']);
386
        }
387
388 16
        $definition = new Definition(HttpDispatcher::class, [
389 16
            $config['servers'],
390 16
            $baseUrl,
391 16
            $httpClient,
392
        ]);
393
394 16
        $container->setDefinition($serviceName, $definition);
395 16
    }
396
397 14
    private function loadVarnish(ContainerBuilder $container, XmlFileLoader $loader, array $config)
398
    {
399 14
        $this->createHttpDispatcherDefinition($container, $config['http'], $this->getAlias().'.proxy_client.varnish.http_dispatcher');
400
        $options = [
401 13
            'tags_header' => $config['tags_header'],
402
        ];
403 13
        if (!empty($config['header_length'])) {
404
            $options['header_length'] = $config['header_length'];
405
        }
406 13
        if (!empty($config['default_ban_headers'])) {
407
            $options['default_ban_headers'] = $config['default_ban_headers'];
408
        }
409 13
        $container->setParameter($this->getAlias().'.proxy_client.varnish.options', $options);
410
411 13
        $loader->load('varnish.xml');
412 13
    }
413
414 2
    private function loadNginx(ContainerBuilder $container, XmlFileLoader $loader, array $config)
415
    {
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 2
            'purge_location' => $config['purge_location'],
419
        ]);
420 2
        $loader->load('nginx.xml');
421 2
    }
422
423 1
    private function loadSymfony(ContainerBuilder $container, XmlFileLoader $loader, array $config)
424
    {
425 1
        $this->createHttpDispatcherDefinition($container, $config['http'], $this->getAlias().'.proxy_client.symfony.http_dispatcher');
426
        $options = [
427 1
            'tags_header' => $config['tags_header'],
428 1
            'tags_method' => $config['tags_method'],
429 1
            'purge_method' => $config['purge_method'],
430
        ];
431 1
        if (!empty($config['header_length'])) {
432
            $options['header_length'] = $config['header_length'];
433
        }
434 1
        $container->setParameter($this->getAlias().'.proxy_client.symfony.options', $options);
435
436 1
        $loader->load('symfony.xml');
437 1
    }
438
439
    /**
440
     * @param ContainerBuilder $container
441
     * @param XmlFileLoader    $loader
442
     * @param array            $config    Configuration section for the tags node
443
     * @param string           $client    Name of the client used with the cache manager,
444
     *                                    "custom" when a custom client is used
445
     */
446 18
    private function loadCacheTagging(ContainerBuilder $container, XmlFileLoader $loader, array $config, $client)
447
    {
448 18
        if ('auto' === $config['enabled'] && !in_array($client, ['varnish', 'symfony'])) {
449 3
            $container->setParameter($this->getAlias().'.compiler_pass.tag_annotations', false);
450
451 3
            return;
452
        }
453 15
        if (!in_array($client, ['varnish', 'symfony', 'custom', 'noop'])) {
454 1
            throw new InvalidConfigurationException(sprintf('You can not enable cache tagging with the %s client', $client));
455
        }
456
457 14
        $container->setParameter($this->getAlias().'.compiler_pass.tag_annotations', true);
458 14
        $container->setParameter($this->getAlias().'.tag_handler.response_header', $config['response_header']);
459 14
        $container->setParameter($this->getAlias().'.tag_handler.strict', $config['strict']);
460 14
        $loader->load('cache_tagging.xml');
461 14
        if (class_exists(Application::class)) {
462 14
            $loader->load('cache_tagging_commands.xml');
463
        }
464
465 14
        if (!empty($config['expression_language'])) {
466
            $container->setAlias(
467
                $this->getAlias().'.tag_handler.expression_language',
468
                $config['expression_language']
469
            );
470
        }
471
472 14
        if (!empty($config['rules'])) {
473 3
            $this->loadTagRules($container, $config['rules']);
474
        }
475 14
    }
476
477 2
    private function loadTest(ContainerBuilder $container, XmlFileLoader $loader, array $config)
478
    {
479 2
        $container->setParameter($this->getAlias().'.test.cache_header', $config['cache_header']);
480
481 2
        if ($config['proxy_server']) {
482 2
            $this->loadProxyServer($container, $loader, $config['proxy_server']);
483
        }
484 2
    }
485
486 2
    private function loadProxyServer(ContainerBuilder $container, XmlFileLoader $loader, array $config)
487
    {
488 2
        if (isset($config['varnish'])) {
489 2
            $this->loadVarnishProxyServer($container, $loader, $config['varnish']);
490
        }
491
492 2
        if (isset($config['nginx'])) {
493
            $this->loadNginxProxyServer($container, $loader, $config['nginx']);
494
        }
495
496 2
        $container->setAlias(
497 2
            $this->getAlias().'.test.default_proxy_server',
498 2
            $this->getAlias().'.test.proxy_server.'.$this->getDefaultProxyClient($config)
499
        );
500 2
    }
501
502 2
    private function loadVarnishProxyServer(ContainerBuilder $container, XmlFileLoader $loader, $config)
503
    {
504 2
        $loader->load('varnish_proxy.xml');
505 2
        foreach ($config as $key => $value) {
506 2
            $container->setParameter(
507 2
                $this->getAlias().'.test.proxy_server.varnish.'.$key,
508 2
                $value
509
            );
510
        }
511 2
    }
512
513
    private function loadNginxProxyServer(ContainerBuilder $container, XmlFileLoader $loader, $config)
514
    {
515
        $loader->load('nginx_proxy.xml');
516
        foreach ($config as $key => $value) {
517
            $container->setParameter(
518
                $this->getAlias().'.test.proxy_server.nginx.'.$key,
519
                $value
520
            );
521
        }
522
    }
523
524 3
    private function loadTagRules(ContainerBuilder $container, array $config)
525
    {
526 3
        $tagDefinition = $container->getDefinition($this->getAlias().'.event_listener.tag');
527
528 3
        foreach ($config as $rule) {
529 3
            $ruleMatcher = $this->parseRequestMatcher($container, $rule['match']);
530
531
            $tags = [
532 3
                'tags' => $rule['tags'],
533 3
                'expressions' => $rule['tag_expressions'],
534
            ];
535
536 3
            $tagDefinition->addMethodCall('addRule', [$ruleMatcher, $tags]);
537
        }
538 3
    }
539
540 3
    private function loadInvalidatorRules(ContainerBuilder $container, array $config)
541
    {
542 3
        $tagDefinition = $container->getDefinition($this->getAlias().'.event_listener.invalidation');
543
544 3
        foreach ($config as $rule) {
545 3
            $ruleMatcher = $this->parseRequestMatcher($container, $rule['match']);
546 3
            $tagDefinition->addMethodCall('addRule', [$ruleMatcher, $rule['routes']]);
547
        }
548 3
    }
549
550 17
    private function validateUrl($url, $msg)
551
    {
552 17
        $prefixed = $this->prefixSchema($url);
553
554 17
        if (!$parts = parse_url($prefixed)) {
555 1
            throw new InvalidConfigurationException(sprintf($msg, $url));
556
        }
557 17
    }
558
559 17
    private function prefixSchema($url)
560
    {
561 17
        if (false === strpos($url, '://')) {
562 17
            $url = sprintf('%s://%s', 'http', $url);
563
        }
564
565 17
        return $url;
566
    }
567
568 17
    private function getDefaultProxyClient(array $config)
569
    {
570 17
        if (isset($config['default'])) {
571
            return $config['default'];
572
        }
573
574 17
        if (isset($config['varnish'])) {
575 13
            return 'varnish';
576
        }
577
578 4
        if (isset($config['nginx'])) {
579 2
            return 'nginx';
580
        }
581
582 2
        if (isset($config['symfony'])) {
583 1
            return 'symfony';
584
        }
585
586 1
        if (isset($config['noop'])) {
587 1
            return 'noop';
588
        }
589
590
        throw new InvalidConfigurationException('No proxy client configured');
591
    }
592
593
    /**
594
     * Build the child definition with fallback for Symfony versions < 3.3.
595
     *
596
     * @param string $id Id of the service to extend
597
     *
598
     * @return ChildDefinition|DefinitionDecorator
599
     */
600 9
    private function createChildDefinition($id)
601
    {
602 9
        if (class_exists(ChildDefinition::class)) {
603 9
            return new ChildDefinition($id);
604
        }
605
606
        return new DefinitionDecorator($id);
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Depend...ion\DefinitionDecorator has been deprecated with message: The DefinitionDecorator class is deprecated since version 3.3 and will be removed in 4.0. Use the Symfony\Component\DependencyInjection\ChildDefinition class instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
607
    }
608
}
609