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 |
||
| 30 | class FOSHttpCacheExtension extends Extension |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * {@inheritdoc} |
||
| 34 | */ |
||
| 35 | 23 | public function getConfiguration(array $config, ContainerBuilder $container) |
|
| 39 | |||
| 40 | /** |
||
| 41 | * {@inheritdoc} |
||
| 42 | */ |
||
| 43 | 23 | public function load(array $configs, ContainerBuilder $container) |
|
| 44 | { |
||
| 45 | 23 | $configuration = $this->getConfiguration($configs, $container); |
|
| 46 | 23 | $config = $this->processConfiguration($configuration, $configs); |
|
|
|
|||
| 47 | |||
| 48 | 23 | $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
| 49 | 23 | $loader->load('matcher.xml'); |
|
| 50 | |||
| 51 | 23 | if ($config['debug']['enabled'] || (!empty($config['cache_control']))) { |
|
| 52 | 3 | $debugHeader = $config['debug']['enabled'] ? $config['debug']['header'] : false; |
|
| 53 | 3 | $container->setParameter($this->getAlias().'.debug_header', $debugHeader); |
|
| 54 | 3 | $loader->load('cache_control_listener.xml'); |
|
| 55 | } |
||
| 56 | |||
| 57 | 23 | $this->loadCacheable($container, $config['cacheable']); |
|
| 58 | |||
| 59 | 23 | if (!empty($config['cache_control'])) { |
|
| 60 | 3 | $this->loadCacheControl($container, $config['cache_control']); |
|
| 61 | } |
||
| 62 | |||
| 63 | 23 | if (isset($config['proxy_client'])) { |
|
| 64 | 17 | $this->loadProxyClient($container, $loader, $config['proxy_client']); |
|
| 65 | } |
||
| 66 | |||
| 67 | 22 | if (isset($config['test'])) { |
|
| 68 | 1 | $this->loadTest($container, $loader, $config['test']); |
|
| 69 | } |
||
| 70 | |||
| 71 | 22 | if ($config['cache_manager']['enabled']) { |
|
| 72 | 17 | if (array_key_exists('custom_proxy_client', $config['cache_manager'])) { |
|
| 73 | // overwrite the previously set alias, if a proxy client was also configured |
||
| 74 | 1 | $container->setAlias( |
|
| 75 | 1 | $this->getAlias().'.default_proxy_client', |
|
| 76 | 1 | $config['cache_manager']['custom_proxy_client'] |
|
| 77 | ); |
||
| 78 | } |
||
| 79 | 17 | if ('auto' === $config['cache_manager']['generate_url_type']) { |
|
| 80 | 17 | if (array_key_exists('custom_proxy_client', $config['cache_manager'])) { |
|
| 81 | 1 | $generateUrlType = UrlGeneratorInterface::ABSOLUTE_URL; |
|
| 82 | } else { |
||
| 83 | 16 | $defaultClient = $this->getDefaultProxyClient($config['proxy_client']); |
|
| 84 | 16 | if ($defaultClient !== 'noop' |
|
| 85 | 15 | && array_key_exists('base_url', $config['proxy_client'][$defaultClient])) { |
|
| 86 | $generateUrlType = UrlGeneratorInterface::ABSOLUTE_PATH; |
||
| 87 | } else { |
||
| 88 | 16 | $generateUrlType = UrlGeneratorInterface::ABSOLUTE_URL; |
|
| 89 | } |
||
| 90 | } |
||
| 91 | } else { |
||
| 92 | $generateUrlType = $config['cache_manager']['generate_url_type']; |
||
| 93 | } |
||
| 94 | 17 | $container->setParameter($this->getAlias().'.cache_manager.generate_url_type', $generateUrlType); |
|
| 95 | 17 | $loader->load('cache_manager.xml'); |
|
| 96 | } |
||
| 97 | |||
| 98 | 22 | if ($config['tags']['enabled']) { |
|
| 99 | 17 | $this->loadCacheTagging( |
|
| 100 | 17 | $container, |
|
| 101 | 17 | $loader, |
|
| 102 | 17 | $config['tags'], |
|
| 103 | 17 | array_key_exists('proxy_client', $config) |
|
| 104 | 16 | ? $this->getDefaultProxyClient($config['proxy_client']) |
|
| 105 | 17 | : 'custom' |
|
| 106 | ); |
||
| 107 | } else { |
||
| 108 | 5 | $container->setParameter($this->getAlias().'.compiler_pass.tag_annotations', false); |
|
| 109 | } |
||
| 110 | |||
| 111 | 21 | if ($config['invalidation']['enabled']) { |
|
| 112 | 16 | $loader->load('invalidation_listener.xml'); |
|
| 113 | |||
| 114 | 16 | if (!empty($config['invalidation']['expression_language'])) { |
|
| 115 | $container->setAlias( |
||
| 116 | $this->getAlias().'.invalidation.expression_language', |
||
| 117 | $config['invalidation']['expression_language'] |
||
| 118 | ); |
||
| 119 | } |
||
| 120 | |||
| 121 | 16 | if (!empty($config['invalidation']['rules'])) { |
|
| 122 | 2 | $this->loadInvalidatorRules($container, $config['invalidation']['rules']); |
|
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | 21 | if ($config['user_context']['enabled']) { |
|
| 127 | 4 | $this->loadUserContext($container, $loader, $config['user_context']); |
|
| 128 | } |
||
| 129 | |||
| 130 | 21 | if (!empty($config['flash_message']) && $config['flash_message']['enabled']) { |
|
| 131 | 1 | $container->setParameter($this->getAlias().'.event_listener.flash_message.options', $config['flash_message']); |
|
| 132 | |||
| 133 | 1 | $loader->load('flash_message.xml'); |
|
| 134 | } |
||
| 135 | 21 | } |
|
| 136 | |||
| 137 | 23 | private function loadCacheable(ContainerBuilder $container, array $config) |
|
| 138 | { |
||
| 139 | 23 | $definition = $container->getDefinition($this->getAlias().'.response_matcher.cacheable'); |
|
| 140 | |||
| 141 | // Change CacheableResponseMatcher to ExpressionResponseMatcher |
||
| 142 | 23 | if ($config['response']['expression']) { |
|
| 143 | $definition->setClass(ExpressionResponseMatcher::class) |
||
| 144 | ->setArguments([$config['response']['expression']]); |
||
| 145 | } else { |
||
| 146 | 23 | $container->setParameter( |
|
| 147 | 23 | $this->getAlias().'.cacheable.response.additional_status', |
|
| 148 | 23 | $config['response']['additional_status'] |
|
| 149 | ); |
||
| 150 | } |
||
| 151 | 23 | } |
|
| 152 | |||
| 153 | /** |
||
| 154 | * @param ContainerBuilder $container |
||
| 155 | * @param array $config |
||
| 156 | * |
||
| 157 | * @throws InvalidConfigurationException |
||
| 158 | */ |
||
| 159 | 3 | private function loadCacheControl(ContainerBuilder $container, array $config) |
|
| 173 | |||
| 174 | 5 | private function parseRuleMatcher(ContainerBuilder $container, array $match) |
|
| 175 | { |
||
| 176 | 5 | $match['ips'] = (empty($match['ips'])) ? null : $match['ips']; |
|
| 177 | |||
| 178 | 5 | return $this->createRequestMatcher( |
|
| 179 | 5 | $container, |
|
| 180 | 5 | $match['path'], |
|
| 181 | 5 | $match['host'], |
|
| 182 | 5 | $match['methods'], |
|
| 183 | 5 | $match['ips'], |
|
| 184 | 5 | $match['attributes'] |
|
| 185 | ); |
||
| 186 | } |
||
| 187 | |||
| 188 | 4 | private function loadUserContext(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
| 222 | |||
| 223 | 5 | private function createRequestMatcher(ContainerBuilder $container, $path = null, $host = null, $methods = null, $ips = null, array $attributes = []) |
|
| 238 | |||
| 239 | 17 | private function loadProxyClient(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Define the http dispatcher service for the proxy client $name. |
||
| 262 | * |
||
| 263 | * @param ContainerBuilder $container |
||
| 264 | * @param array $config |
||
| 265 | * @param string $serviceName |
||
| 266 | */ |
||
| 267 | 16 | private function createHttpDispatcherDefinition(ContainerBuilder $container, array $config, $serviceName) |
|
| 291 | |||
| 292 | 13 | private function loadVarnish(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
| 293 | { |
||
| 308 | |||
| 309 | 2 | private function loadNginx(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
| 317 | |||
| 318 | 1 | private function loadSymfony(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
| 323 | |||
| 324 | /** |
||
| 325 | * @param ContainerBuilder $container |
||
| 326 | * @param XmlFileLoader $loader |
||
| 327 | * @param array $config Configuration section for the tags node |
||
| 328 | * @param string $client Name of the client used with the cache manager, |
||
| 329 | * "custom" when a custom client is used |
||
| 330 | */ |
||
| 331 | 17 | private function loadCacheTagging(ContainerBuilder $container, XmlFileLoader $loader, array $config, $client) |
|
| 358 | |||
| 359 | 1 | private function loadTest(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
| 367 | |||
| 368 | 1 | private function loadProxyServer(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
| 383 | |||
| 384 | 1 | private function loadVarnishProxyServer(ContainerBuilder $container, XmlFileLoader $loader, $config) |
|
| 394 | |||
| 395 | private function loadNginxProxyServer(ContainerBuilder $container, XmlFileLoader $loader, $config) |
||
| 405 | |||
| 406 | 2 | private function loadTagRules(ContainerBuilder $container, array $config) |
|
| 421 | |||
| 422 | 2 | private function loadInvalidatorRules(ContainerBuilder $container, array $config) |
|
| 431 | |||
| 432 | 16 | private function validateUrl($url, $msg) |
|
| 440 | |||
| 441 | 16 | private function prefixSchema($url) |
|
| 449 | |||
| 450 | 16 | private function getDefaultProxyClient(array $config) |
|
| 474 | } |
||
| 475 |
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: