Complex classes like FOSHttpCacheExtension often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FOSHttpCacheExtension, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | class FOSHttpCacheExtension extends Extension |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * {@inheritdoc} |
||
| 39 | */ |
||
| 40 | 29 | public function getConfiguration(array $config, ContainerBuilder $container) |
|
| 41 | { |
||
| 42 | 29 | return new Configuration($container->getParameter('kernel.debug')); |
|
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * {@inheritdoc} |
||
| 47 | */ |
||
| 48 | 29 | public function load(array $configs, ContainerBuilder $container) |
|
| 49 | { |
||
| 50 | 29 | $configuration = $this->getConfiguration($configs, $container); |
|
| 51 | 29 | $config = $this->processConfiguration($configuration, $configs); |
|
|
|
|||
| 52 | |||
| 53 | 29 | $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
| 54 | 29 | $loader->load('matcher.xml'); |
|
| 55 | |||
| 56 | 29 | if ($config['debug']['enabled'] || (!empty($config['cache_control']))) { |
|
| 57 | 7 | $debugHeader = $config['debug']['enabled'] ? $config['debug']['header'] : false; |
|
| 58 | 7 | $container->setParameter('fos_http_cache.debug_header', $debugHeader); |
|
| 59 | 7 | $loader->load('cache_control_listener.xml'); |
|
| 60 | } |
||
| 61 | |||
| 62 | 29 | $this->loadCacheable($container, $config['cacheable']); |
|
| 63 | |||
| 64 | 29 | if (!empty($config['cache_control'])) { |
|
| 65 | 7 | $this->loadCacheControl($container, $config['cache_control']); |
|
| 66 | } |
||
| 67 | |||
| 68 | 28 | if (isset($config['proxy_client'])) { |
|
| 69 | 19 | $this->loadProxyClient($container, $loader, $config['proxy_client']); |
|
| 70 | } |
||
| 71 | |||
| 72 | 27 | if (isset($config['test'])) { |
|
| 73 | 2 | $this->loadTest($container, $loader, $config['test']); |
|
| 74 | } |
||
| 75 | |||
| 76 | 27 | if ($config['cache_manager']['enabled']) { |
|
| 77 | 19 | if (array_key_exists('custom_proxy_client', $config['cache_manager'])) { |
|
| 78 | // overwrite the previously set alias, if a proxy client was also configured |
||
| 79 | 1 | $container->setAlias( |
|
| 80 | 1 | 'fos_http_cache.default_proxy_client', |
|
| 81 | 1 | $config['cache_manager']['custom_proxy_client'] |
|
| 82 | ); |
||
| 83 | } |
||
| 84 | 19 | if ('auto' === $config['cache_manager']['generate_url_type']) { |
|
| 85 | 19 | if (array_key_exists('custom_proxy_client', $config['cache_manager'])) { |
|
| 86 | 1 | $generateUrlType = UrlGeneratorInterface::ABSOLUTE_URL; |
|
| 87 | } else { |
||
| 88 | 18 | $defaultClient = $this->getDefaultProxyClient($config['proxy_client']); |
|
| 89 | 18 | if ('noop' !== $defaultClient |
|
| 90 | 18 | && array_key_exists('base_url', $config['proxy_client'][$defaultClient])) { |
|
| 91 | $generateUrlType = UrlGeneratorInterface::ABSOLUTE_PATH; |
||
| 92 | } else { |
||
| 93 | 19 | $generateUrlType = UrlGeneratorInterface::ABSOLUTE_URL; |
|
| 94 | } |
||
| 95 | } |
||
| 96 | } else { |
||
| 97 | $generateUrlType = $config['cache_manager']['generate_url_type']; |
||
| 98 | } |
||
| 99 | 19 | $container->setParameter('fos_http_cache.cache_manager.generate_url_type', $generateUrlType); |
|
| 100 | 19 | $loader->load('cache_manager.xml'); |
|
| 101 | 19 | if (class_exists(Application::class)) { |
|
| 102 | 19 | $loader->load('cache_manager_commands.xml'); |
|
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | 27 | if ($config['tags']['enabled']) { |
|
| 107 | 19 | $this->loadCacheTagging( |
|
| 108 | 19 | $container, |
|
| 109 | 19 | $loader, |
|
| 110 | 19 | $config['tags'], |
|
| 111 | 19 | array_key_exists('proxy_client', $config) |
|
| 112 | 18 | ? $this->getDefaultProxyClient($config['proxy_client']) |
|
| 113 | 19 | : 'custom' |
|
| 114 | ); |
||
| 115 | } else { |
||
| 116 | 8 | $container->setParameter('fos_http_cache.compiler_pass.tag_annotations', false); |
|
| 117 | } |
||
| 118 | |||
| 119 | 26 | if ($config['invalidation']['enabled']) { |
|
| 120 | 18 | $loader->load('invalidation_listener.xml'); |
|
| 121 | |||
| 122 | 18 | if (!empty($config['invalidation']['expression_language'])) { |
|
| 123 | $container->setAlias( |
||
| 124 | 'fos_http_cache.invalidation.expression_language', |
||
| 125 | $config['invalidation']['expression_language'] |
||
| 126 | ); |
||
| 127 | } |
||
| 128 | |||
| 129 | 18 | if (!empty($config['invalidation']['rules'])) { |
|
| 130 | 3 | $this->loadInvalidatorRules($container, $config['invalidation']['rules']); |
|
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | 26 | if ($config['user_context']['enabled']) { |
|
| 135 | 6 | $this->loadUserContext($container, $loader, $config['user_context']); |
|
| 136 | } |
||
| 137 | |||
| 138 | 26 | if (!empty($config['flash_message']) && $config['flash_message']['enabled']) { |
|
| 139 | 3 | unset($config['flash_message']['enabled']); |
|
| 140 | 3 | $container->setParameter('fos_http_cache.event_listener.flash_message.options', $config['flash_message']); |
|
| 141 | |||
| 142 | 3 | $loader->load('flash_message.xml'); |
|
| 143 | } |
||
| 144 | 26 | } |
|
| 145 | |||
| 146 | 29 | private function loadCacheable(ContainerBuilder $container, array $config) |
|
| 161 | |||
| 162 | /** |
||
| 163 | * @param ContainerBuilder $container |
||
| 164 | * @param array $config |
||
| 165 | * |
||
| 166 | * @throws InvalidConfigurationException |
||
| 167 | */ |
||
| 168 | 7 | private function loadCacheControl(ContainerBuilder $container, array $config) |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Parse one cache control rule match configuration. |
||
| 185 | * |
||
| 186 | * @param ContainerBuilder $container |
||
| 187 | * @param array $match Request and response match criteria |
||
| 188 | * |
||
| 189 | * @return Reference pointing to a rule matcher service |
||
| 190 | */ |
||
| 191 | 7 | private function parseRuleMatcher(ContainerBuilder $container, array $match) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Used for cache control, tag and invalidation rules. |
||
| 214 | * |
||
| 215 | * @param ContainerBuilder $container |
||
| 216 | * @param array $match |
||
| 217 | * |
||
| 218 | * @return Reference to the request matcher |
||
| 219 | */ |
||
| 220 | 9 | private function parseRequestMatcher(ContainerBuilder $container, array $match) |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Used only for cache control rules. |
||
| 250 | * |
||
| 251 | * @param ContainerBuilder $container |
||
| 252 | * @param array $config |
||
| 253 | * |
||
| 254 | * @return Reference to the correct response matcher service |
||
| 255 | */ |
||
| 256 | 7 | private function parseResponseMatcher(ContainerBuilder $container, array $config) |
|
| 280 | |||
| 281 | 6 | private function loadUserContext(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
| 282 | { |
||
| 283 | 6 | $configuredUserIdentifierHeaders = array_map('strtolower', $config['user_identifier_headers']); |
|
| 284 | 6 | $completeUserIdentifierHeaders = $configuredUserIdentifierHeaders; |
|
| 285 | 6 | if (false !== $config['session_name_prefix'] && !in_array('cookie', $completeUserIdentifierHeaders)) { |
|
| 286 | $completeUserIdentifierHeaders[] = 'cookie'; |
||
| 287 | } |
||
| 288 | |||
| 289 | 6 | $loader->load('user_context.xml'); |
|
| 290 | |||
| 291 | 6 | $container->getDefinition('fos_http_cache.user_context.request_matcher') |
|
| 292 | 6 | ->replaceArgument(0, $config['match']['accept']) |
|
| 293 | 6 | ->replaceArgument(1, $config['match']['method']); |
|
| 294 | |||
| 295 | 6 | $container->setParameter('fos_http_cache.event_listener.user_context.options', [ |
|
| 296 | 6 | 'user_identifier_headers' => $completeUserIdentifierHeaders, |
|
| 297 | 6 | 'user_hash_header' => $config['user_hash_header'], |
|
| 298 | 6 | 'ttl' => $config['hash_cache_ttl'], |
|
| 299 | 6 | 'add_vary_on_hash' => $config['always_vary_on_context_hash'], |
|
| 300 | ]); |
||
| 301 | |||
| 302 | 6 | $container->getDefinition('fos_http_cache.event_listener.user_context') |
|
| 303 | 6 | ->replaceArgument(0, new Reference($config['match']['matcher_service'])) |
|
| 304 | ; |
||
| 305 | |||
| 306 | $options = [ |
||
| 307 | 6 | 'user_identifier_headers' => $configuredUserIdentifierHeaders, |
|
| 308 | 6 | 'session_name_prefix' => $config['session_name_prefix'], |
|
| 309 | ]; |
||
| 310 | 6 | $container->getDefinition('fos_http_cache.user_context.anonymous_request_matcher') |
|
| 311 | 6 | ->replaceArgument(0, $options); |
|
| 312 | |||
| 313 | 6 | if ($config['logout_handler']['enabled']) { |
|
| 314 | 5 | $container->getDefinition('fos_http_cache.user_context_invalidator') |
|
| 315 | 5 | ->replaceArgument(1, $completeUserIdentifierHeaders) |
|
| 316 | 5 | ->replaceArgument(2, $config['match']['accept']); |
|
| 317 | |||
| 318 | 5 | $container->setAlias('security.logout.handler.session', 'fos_http_cache.user_context.session_logout_handler'); |
|
| 319 | } else { |
||
| 320 | 1 | $container->removeDefinition('fos_http_cache.user_context.logout_handler'); |
|
| 321 | 1 | $container->removeDefinition('fos_http_cache.user_context.session_logout_handler'); |
|
| 322 | 1 | $container->removeDefinition('fos_http_cache.user_context_invalidator'); |
|
| 323 | } |
||
| 324 | |||
| 325 | 6 | if ($config['role_provider']) { |
|
| 326 | 4 | $container->getDefinition('fos_http_cache.user_context.role_provider') |
|
| 327 | 4 | ->addTag(HashGeneratorPass::TAG_NAME) |
|
| 328 | 4 | ->setAbstract(false); |
|
| 329 | } |
||
| 330 | |||
| 331 | // Only decorate default SessionListener for Symfony 3.4 - 4.0 |
||
| 332 | // For Symfony 4.1+, the UserContextListener sets the header that tells |
||
| 333 | // the SessionListener to leave the cache-control header unchanged. |
||
| 334 | 6 | if (version_compare(Kernel::VERSION, '3.4', '>=') |
|
| 335 | 6 | && version_compare(Kernel::VERSION, '4.1', '<') |
|
| 336 | ) { |
||
| 337 | $container->getDefinition('fos_http_cache.user_context.session_listener') |
||
| 338 | ->setArgument(1, strtolower($config['user_hash_header'])) |
||
| 339 | ->setArgument(2, $completeUserIdentifierHeaders); |
||
| 340 | } else { |
||
| 341 | 6 | $container->removeDefinition('fos_http_cache.user_context.session_listener'); |
|
| 342 | } |
||
| 343 | 6 | } |
|
| 344 | |||
| 345 | 19 | private function loadProxyClient(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Define the http dispatcher service for the proxy client $name. |
||
| 372 | * |
||
| 373 | * @param ContainerBuilder $container |
||
| 374 | * @param array $config |
||
| 375 | * @param string $serviceName |
||
| 376 | */ |
||
| 377 | 17 | private function createHttpDispatcherDefinition(ContainerBuilder $container, array $config, $serviceName) |
|
| 401 | |||
| 402 | 14 | private function loadVarnish(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
| 418 | |||
| 419 | 2 | private function loadNginx(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
| 427 | |||
| 428 | 2 | private function loadSymfony(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
| 453 | |||
| 454 | /** |
||
| 455 | * @param ContainerBuilder $container |
||
| 456 | * @param XmlFileLoader $loader |
||
| 457 | * @param array $config Configuration section for the tags node |
||
| 458 | * @param string $client Name of the client used with the cache manager, |
||
| 459 | * "custom" when a custom client is used |
||
| 460 | */ |
||
| 461 | 19 | private function loadCacheTagging(ContainerBuilder $container, XmlFileLoader $loader, array $config, $client) |
|
| 491 | |||
| 492 | 2 | private function loadTest(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
| 500 | |||
| 501 | 2 | private function loadProxyServer(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
| 516 | |||
| 517 | 2 | private function loadVarnishProxyServer(ContainerBuilder $container, XmlFileLoader $loader, $config) |
|
| 527 | |||
| 528 | private function loadNginxProxyServer(ContainerBuilder $container, XmlFileLoader $loader, $config) |
||
| 538 | |||
| 539 | 3 | private function loadTagRules(ContainerBuilder $container, array $config) |
|
| 554 | |||
| 555 | 3 | private function loadInvalidatorRules(ContainerBuilder $container, array $config) |
|
| 564 | |||
| 565 | 17 | private function validateUrl($url, $msg) |
|
| 573 | |||
| 574 | 17 | private function prefixSchema($url) |
|
| 582 | |||
| 583 | 18 | private function getDefaultProxyClient(array $config) |
|
| 607 | |||
| 608 | /** |
||
| 609 | * Build the child definition with fallback for Symfony versions < 3.3. |
||
| 610 | * |
||
| 611 | * @param string $id Id of the service to extend |
||
| 612 | * |
||
| 613 | * @return ChildDefinition|DefinitionDecorator |
||
| 614 | */ |
||
| 615 | 9 | private function createChildDefinition($id) |
|
| 623 | } |
||
| 624 |
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: