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 |
||
| 36 | class FOSHttpCacheExtension extends Extension |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * {@inheritdoc} |
||
| 40 | */ |
||
| 41 | 37 | public function getConfiguration(array $config, ContainerBuilder $container) |
|
| 45 | |||
| 46 | /** |
||
| 47 | * {@inheritdoc} |
||
| 48 | */ |
||
| 49 | 37 | public function load(array $configs, ContainerBuilder $container) |
|
| 50 | { |
||
| 51 | 37 | $configuration = $this->getConfiguration($configs, $container); |
|
| 52 | 37 | $config = $this->processConfiguration($configuration, $configs); |
|
|
|
|||
| 53 | |||
| 54 | 37 | $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
| 55 | 37 | $loader->load('matcher.xml'); |
|
| 56 | |||
| 57 | 37 | 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 | 37 | $this->loadCacheable($container, $config['cacheable']); |
|
| 64 | |||
| 65 | 37 | if (!empty($config['cache_control'])) { |
|
| 66 | 7 | $this->loadCacheControl($container, $config['cache_control']); |
|
| 67 | } |
||
| 68 | |||
| 69 | 36 | if (isset($config['proxy_client'])) { |
|
| 70 | 27 | $this->loadProxyClient($container, $loader, $config['proxy_client']); |
|
| 71 | } |
||
| 72 | |||
| 73 | 35 | if (isset($config['test'])) { |
|
| 74 | 2 | $this->loadTest($container, $loader, $config['test']); |
|
| 75 | } |
||
| 76 | |||
| 77 | 35 | if ($config['cache_manager']['enabled']) { |
|
| 78 | 27 | 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 | 27 | if ('auto' === $config['cache_manager']['generate_url_type']) { |
|
| 86 | 27 | if (array_key_exists('custom_proxy_client', $config['cache_manager'])) { |
|
| 87 | 1 | $generateUrlType = UrlGeneratorInterface::ABSOLUTE_URL; |
|
| 88 | } else { |
||
| 89 | 26 | $defaultClient = $this->getDefaultProxyClient($config['proxy_client']); |
|
| 90 | 26 | if ('noop' !== $defaultClient |
|
| 91 | 26 | && array_key_exists('base_url', $config['proxy_client'][$defaultClient])) { |
|
| 92 | $generateUrlType = UrlGeneratorInterface::ABSOLUTE_PATH; |
||
| 93 | } else { |
||
| 94 | 27 | $generateUrlType = UrlGeneratorInterface::ABSOLUTE_URL; |
|
| 95 | } |
||
| 96 | } |
||
| 97 | } else { |
||
| 98 | $generateUrlType = $config['cache_manager']['generate_url_type']; |
||
| 99 | } |
||
| 100 | 27 | $container->setParameter('fos_http_cache.cache_manager.generate_url_type', $generateUrlType); |
|
| 101 | 27 | $loader->load('cache_manager.xml'); |
|
| 102 | 27 | if (class_exists(Application::class)) { |
|
| 103 | 27 | $loader->load('cache_manager_commands.xml'); |
|
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | 35 | if ($config['tags']['enabled']) { |
|
| 108 | 27 | $this->loadCacheTagging( |
|
| 109 | 27 | $container, |
|
| 110 | $loader, |
||
| 111 | 27 | $config['tags'], |
|
| 112 | 27 | array_key_exists('proxy_client', $config) |
|
| 113 | 26 | ? $this->getDefaultProxyClient($config['proxy_client']) |
|
| 114 | 27 | : 'custom' |
|
| 115 | ); |
||
| 116 | } else { |
||
| 117 | 8 | $container->setParameter('fos_http_cache.compiler_pass.tag_annotations', false); |
|
| 118 | } |
||
| 119 | |||
| 120 | 34 | if ($config['invalidation']['enabled']) { |
|
| 121 | 26 | $loader->load('invalidation_listener.xml'); |
|
| 122 | |||
| 123 | 26 | 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 | 26 | if (!empty($config['invalidation']['rules'])) { |
|
| 131 | 3 | $this->loadInvalidatorRules($container, $config['invalidation']['rules']); |
|
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | 34 | if ($config['user_context']['enabled']) { |
|
| 136 | 6 | $this->loadUserContext($container, $loader, $config['user_context']); |
|
| 137 | } |
||
| 138 | |||
| 139 | 34 | 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 | 34 | ||
| 146 | if (\PHP_VERSION_ID >= 80000) { |
||
| 147 | 37 | $loader->load('php8_attributes.xml'); |
|
| 148 | } |
||
| 149 | 37 | } |
|
| 150 | |||
| 151 | private function loadCacheable(ContainerBuilder $container, array $config) |
||
| 152 | 37 | { |
|
| 153 | $definition = $container->getDefinition('fos_http_cache.response_matcher.cacheable'); |
||
| 154 | |||
| 155 | // Change CacheableResponseMatcher to ExpressionResponseMatcher |
||
| 156 | 37 | if ($config['response']['expression']) { |
|
| 157 | 37 | $definition->setClass(ExpressionResponseMatcher::class) |
|
| 158 | 37 | ->setArguments([$config['response']['expression']]); |
|
| 159 | } else { |
||
| 160 | $container->setParameter( |
||
| 161 | 37 | 'fos_http_cache.cacheable.response.additional_status', |
|
| 162 | $config['response']['additional_status'] |
||
| 163 | ); |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @throws InvalidConfigurationException |
||
| 169 | 7 | */ |
|
| 170 | private function loadCacheControl(ContainerBuilder $container, array $config) |
||
| 171 | 7 | { |
|
| 172 | $controlDefinition = $container->getDefinition('fos_http_cache.event_listener.cache_control'); |
||
| 173 | 7 | ||
| 174 | 7 | foreach ($config['rules'] as $rule) { |
|
| 175 | $ruleMatcher = $this->parseRuleMatcher($container, $rule['match']); |
||
| 176 | 7 | ||
| 177 | 7 | if ('default' === $rule['headers']['overwrite']) { |
|
| 178 | $rule['headers']['overwrite'] = $config['defaults']['overwrite']; |
||
| 179 | } |
||
| 180 | 7 | ||
| 181 | $controlDefinition->addMethodCall('addRule', [$ruleMatcher, $rule['headers']]); |
||
| 182 | 6 | } |
|
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Parse one cache control rule match configuration. |
||
| 187 | * |
||
| 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) |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Used for cache control, tag and invalidation rules. |
||
| 215 | * |
||
| 216 | * @return Reference to the request matcher |
||
| 217 | */ |
||
| 218 | private function parseRequestMatcher(ContainerBuilder $container, array $match) |
||
| 245 | |||
| 246 | 9 | /** |
|
| 247 | * Used only for cache control rules. |
||
| 248 | * |
||
| 249 | * @return Reference to the correct response matcher service |
||
| 250 | */ |
||
| 251 | private function parseResponseMatcher(ContainerBuilder $container, array $config) |
||
| 275 | |||
| 276 | 4 | private function loadUserContext(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
| 335 | |||
| 336 | private function loadProxyClient(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
||
| 360 | |||
| 361 | 26 | /** |
|
| 362 | 26 | * Define the http dispatcher service for the proxy client $name. |
|
| 363 | 26 | * |
|
| 364 | * @param string $serviceName |
||
| 365 | 26 | */ |
|
| 366 | private function createHttpDispatcherDefinition(ContainerBuilder $container, array $config, $serviceName) |
||
| 399 | 24 | ||
| 400 | 24 | private function loadVarnish(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
| 418 | |||
| 419 | 21 | private function loadNginx(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
| 427 | 2 | ||
| 428 | private function loadSymfony(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
||
| 453 | |||
| 454 | 2 | /** |
|
| 455 | * @param array $config Configuration section for the tags node |
||
| 456 | * @param string $client Name of the client used with the cache manager, |
||
| 457 | 2 | * "custom" when a custom client is used |
|
| 458 | */ |
||
| 459 | 2 | private function loadCacheTagging(ContainerBuilder $container, XmlFileLoader $loader, array $config, $client) |
|
| 498 | 3 | ||
| 499 | private function loadTest(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
||
| 507 | 23 | ||
| 508 | private function loadProxyServer(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
||
| 523 | |||
| 524 | 2 | private function loadVarnishProxyServer(ContainerBuilder $container, XmlFileLoader $loader, $config) |
|
| 534 | 2 | ||
| 535 | private function loadNginxProxyServer(ContainerBuilder $container, XmlFileLoader $loader, $config) |
||
| 545 | |||
| 546 | private function loadTagRules(ContainerBuilder $container, array $config) |
||
| 561 | 3 | ||
| 562 | private function loadInvalidatorRules(ContainerBuilder $container, array $config) |
||
| 571 | |||
| 572 | 3 | private function validateUrl($url, $msg) |
|
| 580 | 3 | ||
| 581 | private function prefixSchema($url) |
||
| 589 | 25 | ||
| 590 | private function getDefaultProxyClient(array $config) |
||
| 614 | 3 | ||
| 615 | 2 | /** |
|
| 616 | * Build the child definition with fallback for Symfony versions < 3.3. |
||
| 617 | * |
||
| 618 | 1 | * @param string $id Id of the service to extend |
|
| 619 | 1 | * |
|
| 620 | * @return ChildDefinition|DefinitionDecorator |
||
| 621 | */ |
||
| 622 | private function createChildDefinition($id) |
||
| 630 | } |
||
| 631 |
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: