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 |
||
| 31 | class FOSHttpCacheExtension extends Extension |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * {@inheritdoc} |
||
| 35 | */ |
||
| 36 | 23 | public function getConfiguration(array $config, ContainerBuilder $container) |
|
| 37 | { |
||
| 38 | 23 | return new Configuration($container->getParameter('kernel.debug')); |
|
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * {@inheritdoc} |
||
| 43 | */ |
||
| 44 | 23 | public function load(array $configs, ContainerBuilder $container) |
|
| 45 | { |
||
| 46 | 23 | $configuration = $this->getConfiguration($configs, $container); |
|
| 47 | 23 | $config = $this->processConfiguration($configuration, $configs); |
|
|
|
|||
| 48 | |||
| 49 | 23 | $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
| 50 | 23 | $loader->load('matcher.xml'); |
|
| 51 | |||
| 52 | 23 | if ($config['debug']['enabled'] || (!empty($config['cache_control']))) { |
|
| 53 | 3 | $debugHeader = $config['debug']['enabled'] ? $config['debug']['header'] : false; |
|
| 54 | 3 | $container->setParameter($this->getAlias().'.debug_header', $debugHeader); |
|
| 55 | 3 | $loader->load('cache_control_listener.xml'); |
|
| 56 | } |
||
| 57 | |||
| 58 | 23 | $this->loadCacheable($container, $config['cacheable']); |
|
| 59 | |||
| 60 | 23 | if (!empty($config['cache_control'])) { |
|
| 61 | 3 | $this->loadCacheControl($container, $config['cache_control']); |
|
| 62 | } |
||
| 63 | |||
| 64 | 23 | if (isset($config['proxy_client'])) { |
|
| 65 | 17 | $this->loadProxyClient($container, $loader, $config['proxy_client']); |
|
| 66 | } |
||
| 67 | |||
| 68 | 22 | if (isset($config['test'])) { |
|
| 69 | 1 | $this->loadTest($container, $loader, $config['test']); |
|
| 70 | } |
||
| 71 | |||
| 72 | 22 | if ($config['cache_manager']['enabled']) { |
|
| 73 | 17 | if (array_key_exists('custom_proxy_client', $config['cache_manager'])) { |
|
| 74 | // overwrite the previously set alias, if a proxy client was also configured |
||
| 75 | 1 | $container->setAlias( |
|
| 76 | 1 | $this->getAlias().'.default_proxy_client', |
|
| 77 | 1 | $config['cache_manager']['custom_proxy_client'] |
|
| 78 | ); |
||
| 79 | } |
||
| 80 | 17 | if ('auto' === $config['cache_manager']['generate_url_type']) { |
|
| 81 | 17 | if (array_key_exists('custom_proxy_client', $config['cache_manager'])) { |
|
| 82 | 1 | $generateUrlType = UrlGeneratorInterface::ABSOLUTE_URL; |
|
| 83 | } else { |
||
| 84 | 16 | $defaultClient = $this->getDefaultProxyClient($config['proxy_client']); |
|
| 85 | 16 | if ($defaultClient !== 'noop' |
|
| 86 | 15 | && array_key_exists('base_url', $config['proxy_client'][$defaultClient])) { |
|
| 87 | $generateUrlType = UrlGeneratorInterface::ABSOLUTE_PATH; |
||
| 88 | } else { |
||
| 89 | 16 | $generateUrlType = UrlGeneratorInterface::ABSOLUTE_URL; |
|
| 90 | } |
||
| 91 | } |
||
| 92 | } else { |
||
| 93 | $generateUrlType = $config['cache_manager']['generate_url_type']; |
||
| 94 | } |
||
| 95 | 17 | $container->setParameter($this->getAlias().'.cache_manager.generate_url_type', $generateUrlType); |
|
| 96 | 17 | $loader->load('cache_manager.xml'); |
|
| 97 | } |
||
| 98 | |||
| 99 | 22 | if ($config['tags']['enabled']) { |
|
| 100 | 17 | $this->loadCacheTagging( |
|
| 101 | 17 | $container, |
|
| 102 | 17 | $loader, |
|
| 103 | 17 | $config['tags'], |
|
| 104 | 17 | array_key_exists('proxy_client', $config) |
|
| 105 | 16 | ? $this->getDefaultProxyClient($config['proxy_client']) |
|
| 106 | 17 | : 'custom' |
|
| 107 | ); |
||
| 108 | } else { |
||
| 109 | 5 | $container->setParameter($this->getAlias().'.compiler_pass.tag_annotations', false); |
|
| 110 | } |
||
| 111 | |||
| 112 | 21 | if ($config['invalidation']['enabled']) { |
|
| 113 | 16 | $loader->load('invalidation_listener.xml'); |
|
| 114 | |||
| 115 | 16 | if (!empty($config['invalidation']['expression_language'])) { |
|
| 116 | $container->setAlias( |
||
| 117 | $this->getAlias().'.invalidation.expression_language', |
||
| 118 | $config['invalidation']['expression_language'] |
||
| 119 | ); |
||
| 120 | } |
||
| 121 | |||
| 122 | 16 | if (!empty($config['invalidation']['rules'])) { |
|
| 123 | 2 | $this->loadInvalidatorRules($container, $config['invalidation']['rules']); |
|
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | 21 | if ($config['user_context']['enabled']) { |
|
| 128 | 4 | $this->loadUserContext($container, $loader, $config['user_context']); |
|
| 129 | } |
||
| 130 | |||
| 131 | 21 | if (!empty($config['flash_message']) && $config['flash_message']['enabled']) { |
|
| 132 | 1 | $container->setParameter($this->getAlias().'.event_listener.flash_message.options', $config['flash_message']); |
|
| 133 | |||
| 134 | 1 | $loader->load('flash_message.xml'); |
|
| 135 | } |
||
| 136 | 21 | } |
|
| 137 | |||
| 138 | 23 | private function loadCacheable(ContainerBuilder $container, array $config) |
|
| 139 | { |
||
| 140 | 23 | $definition = $container->getDefinition($this->getAlias().'.response_matcher.cacheable'); |
|
| 141 | |||
| 142 | // Change CacheableResponseMatcher to ExpressionResponseMatcher |
||
| 143 | 23 | if ($config['response']['expression']) { |
|
| 144 | $definition->setClass(ExpressionResponseMatcher::class) |
||
| 145 | ->setArguments([$config['response']['expression']]); |
||
| 146 | } else { |
||
| 147 | 23 | $container->setParameter( |
|
| 148 | 23 | $this->getAlias().'.cacheable.response.additional_status', |
|
| 149 | 23 | $config['response']['additional_status'] |
|
| 150 | ); |
||
| 151 | } |
||
| 152 | 23 | } |
|
| 153 | |||
| 154 | /** |
||
| 155 | * @param ContainerBuilder $container |
||
| 156 | * @param array $config |
||
| 157 | * |
||
| 158 | * @throws InvalidConfigurationException |
||
| 159 | */ |
||
| 160 | 3 | private function loadCacheControl(ContainerBuilder $container, array $config) |
|
| 161 | { |
||
| 162 | 3 | $controlDefinition = $container->getDefinition($this->getAlias().'.event_listener.cache_control'); |
|
| 163 | |||
| 164 | 3 | foreach ($config['rules'] as $rule) { |
|
| 165 | 3 | $ruleMatcher = $this->parseRuleMatcher($container, $rule['match']); |
|
| 166 | |||
| 167 | 3 | if ('default' === $rule['headers']['overwrite']) { |
|
| 168 | 3 | $rule['headers']['overwrite'] = $config['defaults']['overwrite']; |
|
| 169 | } |
||
| 170 | |||
| 171 | 3 | $controlDefinition->addMethodCall('addRule', [$ruleMatcher, $rule['headers']]); |
|
| 172 | } |
||
| 173 | 3 | } |
|
| 174 | |||
| 175 | 5 | private function parseRuleMatcher(ContainerBuilder $container, array $match) |
|
| 176 | { |
||
| 177 | 5 | $match['ips'] = (empty($match['ips'])) ? null : $match['ips']; |
|
| 178 | |||
| 179 | 5 | return $this->createRequestMatcher( |
|
| 180 | 5 | $container, |
|
| 181 | 5 | $match['path'], |
|
| 182 | 5 | $match['host'], |
|
| 183 | 5 | $match['methods'], |
|
| 184 | 5 | $match['ips'], |
|
| 185 | 5 | $match['attributes'] |
|
| 186 | ); |
||
| 187 | } |
||
| 188 | |||
| 189 | 4 | private function loadUserContext(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
| 190 | { |
||
| 191 | 4 | $loader->load('user_context.xml'); |
|
| 192 | |||
| 193 | 4 | $container->getDefinition($this->getAlias().'.user_context.request_matcher') |
|
| 194 | 4 | ->replaceArgument(0, $config['match']['accept']) |
|
| 195 | 4 | ->replaceArgument(1, $config['match']['method']); |
|
| 196 | |||
| 197 | 4 | $container->setParameter($this->getAlias().'.event_listener.user_context.options', [ |
|
| 198 | 4 | 'user_identifier_headers' => $config['user_identifier_headers'], |
|
| 199 | 4 | 'user_hash_header' => $config['user_hash_header'], |
|
| 200 | 4 | 'ttl' => $config['hash_cache_ttl'], |
|
| 201 | 4 | 'add_vary_on_hash' => $config['always_vary_on_context_hash'], |
|
| 202 | ]); |
||
| 203 | 4 | $container->getDefinition($this->getAlias().'.event_listener.user_context') |
|
| 204 | 4 | ->replaceArgument(0, new Reference($config['match']['matcher_service'])); |
|
| 205 | |||
| 206 | 4 | $container->getDefinition($this->getAlias().'.user_context.anonymous_request_matcher') |
|
| 207 | 4 | ->replaceArgument(0, $config['user_identifier_headers']); |
|
| 208 | |||
| 209 | 4 | if ($config['logout_handler']['enabled']) { |
|
| 210 | 4 | $container->getDefinition($this->getAlias().'.user_context.logout_handler') |
|
| 211 | 4 | ->replaceArgument(1, $config['user_identifier_headers']) |
|
| 212 | 4 | ->replaceArgument(2, $config['match']['accept']); |
|
| 213 | } else { |
||
| 214 | $container->removeDefinition($this->getAlias().'.user_context.logout_handler'); |
||
| 215 | } |
||
| 216 | |||
| 217 | 4 | if ($config['role_provider']) { |
|
| 218 | 2 | $container->getDefinition($this->getAlias().'.user_context.role_provider') |
|
| 219 | 2 | ->addTag(HashGeneratorPass::TAG_NAME) |
|
| 220 | 2 | ->setAbstract(false); |
|
| 221 | } |
||
| 222 | 4 | } |
|
| 223 | |||
| 224 | 5 | private function createRequestMatcher(ContainerBuilder $container, $path = null, $host = null, $methods = null, $ips = null, array $attributes = []) |
|
| 225 | { |
||
| 226 | 5 | $arguments = [$path, $host, $methods, $ips, $attributes]; |
|
| 227 | 5 | $serialized = serialize($arguments); |
|
| 228 | 5 | $id = $this->getAlias().'.request_matcher.'.md5($serialized).sha1($serialized); |
|
| 229 | |||
| 230 | 5 | if (!$container->hasDefinition($id)) { |
|
| 231 | 5 | if (class_exists(ChildDefinition::class)) { |
|
| 232 | $container |
||
| 233 | ->setDefinition($id, new ChildDefinition($this->getAlias().'.request_matcher')) |
||
| 234 | ->setArguments($arguments) |
||
| 235 | ; |
||
| 236 | } else { |
||
| 237 | $container |
||
| 238 | 5 | ->setDefinition($id, new DefinitionDecorator($this->getAlias().'.request_matcher')) |
|
| 239 | 5 | ->setArguments($arguments) |
|
| 240 | ; |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | 5 | return new Reference($id); |
|
| 245 | } |
||
| 246 | |||
| 247 | 17 | private function loadProxyClient(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
| 267 | |||
| 268 | /** |
||
| 269 | * Define the http dispatcher service for the proxy client $name. |
||
| 270 | * |
||
| 271 | * @param ContainerBuilder $container |
||
| 272 | * @param array $config |
||
| 273 | * @param string $serviceName |
||
| 274 | */ |
||
| 275 | 16 | private function createHttpDispatcherDefinition(ContainerBuilder $container, array $config, $serviceName) |
|
| 299 | |||
| 300 | 13 | private function loadVarnish(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
| 301 | { |
||
| 302 | 13 | $this->createHttpDispatcherDefinition($container, $config['http'], $this->getAlias().'.proxy_client.varnish.http_dispatcher'); |
|
| 303 | $options = [ |
||
| 304 | 12 | 'tags_header' => $config['tags_header'], |
|
| 305 | ]; |
||
| 306 | 12 | if (!empty($config['header_length'])) { |
|
| 316 | |||
| 317 | 2 | private function loadNginx(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
| 325 | |||
| 326 | 1 | private function loadSymfony(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
| 331 | |||
| 332 | /** |
||
| 333 | * @param ContainerBuilder $container |
||
| 334 | * @param XmlFileLoader $loader |
||
| 335 | * @param array $config Configuration section for the tags node |
||
| 336 | * @param string $client Name of the client used with the cache manager, |
||
| 337 | * "custom" when a custom client is used |
||
| 338 | */ |
||
| 339 | 17 | private function loadCacheTagging(ContainerBuilder $container, XmlFileLoader $loader, array $config, $client) |
|
| 366 | |||
| 367 | 1 | private function loadTest(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
| 375 | |||
| 376 | 1 | private function loadProxyServer(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
| 391 | |||
| 392 | 1 | private function loadVarnishProxyServer(ContainerBuilder $container, XmlFileLoader $loader, $config) |
|
| 402 | |||
| 403 | private function loadNginxProxyServer(ContainerBuilder $container, XmlFileLoader $loader, $config) |
||
| 413 | |||
| 414 | 2 | private function loadTagRules(ContainerBuilder $container, array $config) |
|
| 429 | |||
| 430 | 2 | private function loadInvalidatorRules(ContainerBuilder $container, array $config) |
|
| 439 | |||
| 440 | 16 | private function validateUrl($url, $msg) |
|
| 448 | |||
| 449 | 16 | private function prefixSchema($url) |
|
| 457 | |||
| 458 | 16 | private function getDefaultProxyClient(array $config) |
|
| 482 | } |
||
| 483 |
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: