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