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) |
|
| 257 | { |
||
| 258 | 7 | if (!empty($config['additional_response_status'])) { |
|
| 259 | 1 | $id = 'fos_http_cache.cache_control.expression.'.md5(serialize($config['additional_response_status'])); |
|
| 260 | 1 | if (!$container->hasDefinition($id)) { |
|
| 261 | $container |
||
| 262 | 1 | ->setDefinition($id, $this->createChildDefinition('fos_http_cache.response_matcher.cache_control.cacheable_response')) |
|
| 263 | 1 | ->setArguments([$config['additional_response_status']]) |
|
| 264 | ; |
||
| 265 | } |
||
| 266 | 6 | } elseif (!empty($config['match_response'])) { |
|
| 267 | 2 | $id = 'fos_http_cache.cache_control.match_response.'.md5($config['match_response']); |
|
| 268 | 2 | if (!$container->hasDefinition($id)) { |
|
| 269 | $container |
||
| 270 | 2 | ->setDefinition($id, $this->createChildDefinition('fos_http_cache.response_matcher.cache_control.expression')) |
|
| 271 | 2 | ->replaceArgument(0, $config['match_response']) |
|
| 272 | ; |
||
| 273 | } |
||
| 274 | } else { |
||
| 275 | 4 | $id = 'fos_http_cache.response_matcher.cacheable'; |
|
| 276 | } |
||
| 277 | |||
| 278 | 7 | return new Reference($id); |
|
| 279 | } |
||
| 280 | |||
| 281 | 6 | private function loadUserContext(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
| 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: