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 | 35 | public function getConfiguration(array $config, ContainerBuilder $container) |
|
| 42 | { |
||
| 43 | 35 | return new Configuration($container->getParameter('kernel.debug')); |
|
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * {@inheritdoc} |
||
| 48 | */ |
||
| 49 | 35 | public function load(array $configs, ContainerBuilder $container) |
|
| 50 | { |
||
| 51 | 35 | $configuration = $this->getConfiguration($configs, $container); |
|
| 52 | 35 | $config = $this->processConfiguration($configuration, $configs); |
|
|
|
|||
| 53 | |||
| 54 | 35 | $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
| 55 | 35 | $loader->load('matcher.xml'); |
|
| 56 | |||
| 57 | 35 | 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 | 35 | $this->loadCacheable($container, $config['cacheable']); |
|
| 64 | |||
| 65 | 35 | if (!empty($config['cache_control'])) { |
|
| 66 | 7 | $this->loadCacheControl($container, $config['cache_control']); |
|
| 67 | } |
||
| 68 | |||
| 69 | 34 | if (isset($config['proxy_client'])) { |
|
| 70 | 25 | $this->loadProxyClient($container, $loader, $config['proxy_client']); |
|
| 71 | } |
||
| 72 | |||
| 73 | 33 | if (isset($config['test'])) { |
|
| 74 | 2 | $this->loadTest($container, $loader, $config['test']); |
|
| 75 | } |
||
| 76 | |||
| 77 | 33 | if ($config['cache_manager']['enabled']) { |
|
| 78 | 25 | 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 | 25 | if ('auto' === $config['cache_manager']['generate_url_type']) { |
|
| 86 | 25 | if (array_key_exists('custom_proxy_client', $config['cache_manager'])) { |
|
| 87 | 1 | $generateUrlType = UrlGeneratorInterface::ABSOLUTE_URL; |
|
| 88 | } else { |
||
| 89 | 24 | $defaultClient = $this->getDefaultProxyClient($config['proxy_client']); |
|
| 90 | 24 | if ('noop' !== $defaultClient |
|
| 91 | 24 | && array_key_exists('base_url', $config['proxy_client'][$defaultClient])) { |
|
| 92 | $generateUrlType = UrlGeneratorInterface::ABSOLUTE_PATH; |
||
| 93 | } else { |
||
| 94 | 25 | $generateUrlType = UrlGeneratorInterface::ABSOLUTE_URL; |
|
| 95 | } |
||
| 96 | } |
||
| 97 | } else { |
||
| 98 | $generateUrlType = $config['cache_manager']['generate_url_type']; |
||
| 99 | } |
||
| 100 | 25 | $container->setParameter('fos_http_cache.cache_manager.generate_url_type', $generateUrlType); |
|
| 101 | 25 | $loader->load('cache_manager.xml'); |
|
| 102 | 25 | if (class_exists(Application::class)) { |
|
| 103 | 25 | $loader->load('cache_manager_commands.xml'); |
|
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | 33 | if ($config['tags']['enabled']) { |
|
| 108 | 25 | $this->loadCacheTagging( |
|
| 109 | 25 | $container, |
|
| 110 | 25 | $loader, |
|
| 111 | 25 | $config['tags'], |
|
| 112 | 25 | array_key_exists('proxy_client', $config) |
|
| 113 | 24 | ? $this->getDefaultProxyClient($config['proxy_client']) |
|
| 114 | 25 | : 'custom' |
|
| 115 | ); |
||
| 116 | } else { |
||
| 117 | 8 | $container->setParameter('fos_http_cache.compiler_pass.tag_annotations', false); |
|
| 118 | } |
||
| 119 | |||
| 120 | 32 | if ($config['invalidation']['enabled']) { |
|
| 121 | 24 | $loader->load('invalidation_listener.xml'); |
|
| 122 | |||
| 123 | 24 | 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 | 24 | if (!empty($config['invalidation']['rules'])) { |
|
| 131 | 3 | $this->loadInvalidatorRules($container, $config['invalidation']['rules']); |
|
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | 32 | if ($config['user_context']['enabled']) { |
|
| 136 | 6 | $this->loadUserContext($container, $loader, $config['user_context']); |
|
| 137 | } |
||
| 138 | |||
| 139 | 32 | 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 | 32 | } |
|
| 146 | |||
| 147 | 35 | private function loadCacheable(ContainerBuilder $container, array $config) |
|
| 148 | { |
||
| 149 | 35 | $definition = $container->getDefinition('fos_http_cache.response_matcher.cacheable'); |
|
| 150 | |||
| 151 | // Change CacheableResponseMatcher to ExpressionResponseMatcher |
||
| 152 | 35 | if ($config['response']['expression']) { |
|
| 153 | $definition->setClass(ExpressionResponseMatcher::class) |
||
| 154 | ->setArguments([$config['response']['expression']]); |
||
| 155 | } else { |
||
| 156 | 35 | $container->setParameter( |
|
| 157 | 35 | 'fos_http_cache.cacheable.response.additional_status', |
|
| 158 | 35 | $config['response']['additional_status'] |
|
| 159 | ); |
||
| 160 | } |
||
| 161 | 35 | } |
|
| 162 | |||
| 163 | /** |
||
| 164 | * @param ContainerBuilder $container |
||
| 165 | * @param array $config |
||
| 166 | * |
||
| 167 | * @throws InvalidConfigurationException |
||
| 168 | */ |
||
| 169 | 7 | private function loadCacheControl(ContainerBuilder $container, array $config) |
|
| 170 | { |
||
| 171 | 7 | $controlDefinition = $container->getDefinition('fos_http_cache.event_listener.cache_control'); |
|
| 172 | |||
| 173 | 7 | foreach ($config['rules'] as $rule) { |
|
| 174 | 7 | $ruleMatcher = $this->parseRuleMatcher($container, $rule['match']); |
|
| 175 | |||
| 176 | 7 | if ('default' === $rule['headers']['overwrite']) { |
|
| 177 | 7 | $rule['headers']['overwrite'] = $config['defaults']['overwrite']; |
|
| 178 | } |
||
| 179 | |||
| 180 | 7 | $controlDefinition->addMethodCall('addRule', [$ruleMatcher, $rule['headers']]); |
|
| 181 | } |
||
| 182 | 6 | } |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Parse one cache control rule match configuration. |
||
| 186 | * |
||
| 187 | * @param ContainerBuilder $container |
||
| 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) |
|
| 193 | { |
||
| 194 | 7 | $requestMatcher = $this->parseRequestMatcher($container, $match); |
|
| 195 | 7 | $responseMatcher = $this->parseResponseMatcher($container, $match); |
|
| 196 | |||
| 197 | 7 | $signature = serialize([(string) $requestMatcher, (string) $responseMatcher]); |
|
| 198 | 7 | $id = 'fos_http_cache.cache_control.rule_matcher.'.md5($signature); |
|
| 199 | |||
| 200 | 7 | if ($container->hasDefinition($id)) { |
|
| 201 | 1 | throw new InvalidConfigurationException('Duplicate match criteria. Would be hidden by a previous rule. match: '.json_encode($match)); |
|
| 202 | } |
||
| 203 | |||
| 204 | $container |
||
| 205 | 7 | ->setDefinition($id, $this->createChildDefinition('fos_http_cache.rule_matcher')) |
|
| 206 | 7 | ->replaceArgument(0, $requestMatcher) |
|
| 207 | 7 | ->replaceArgument(1, $responseMatcher) |
|
| 208 | ; |
||
| 209 | |||
| 210 | 7 | return new Reference($id); |
|
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Used for cache control, tag and invalidation rules. |
||
| 215 | * |
||
| 216 | * @param ContainerBuilder $container |
||
| 217 | * @param array $match |
||
| 218 | * |
||
| 219 | * @return Reference to the request matcher |
||
| 220 | */ |
||
| 221 | 9 | private function parseRequestMatcher(ContainerBuilder $container, array $match) |
|
| 222 | { |
||
| 223 | 9 | $match['ips'] = (empty($match['ips'])) ? null : $match['ips']; |
|
| 224 | |||
| 225 | $arguments = [ |
||
| 226 | 9 | $match['path'], |
|
| 227 | 9 | $match['host'], |
|
| 228 | 9 | $match['methods'], |
|
| 229 | 9 | $match['ips'], |
|
| 230 | 9 | $match['attributes'], |
|
| 231 | ]; |
||
| 232 | 9 | $serialized = serialize($arguments); |
|
| 233 | 9 | $id = 'fos_http_cache.request_matcher.'.md5($serialized).sha1($serialized); |
|
| 234 | |||
| 235 | 9 | if (!$container->hasDefinition($id)) { |
|
| 236 | $container |
||
| 237 | 9 | ->setDefinition($id, $this->createChildDefinition('fos_http_cache.request_matcher')) |
|
| 238 | 9 | ->setArguments($arguments) |
|
| 239 | ; |
||
| 240 | |||
| 241 | 9 | if (!empty($match['query_string'])) { |
|
| 242 | $container->getDefinition($id)->addMethodCall('setQueryString', [$match['query_string']]); |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | 9 | return new Reference($id); |
|
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Used only for cache control rules. |
||
| 251 | * |
||
| 252 | * @param ContainerBuilder $container |
||
| 253 | * @param array $config |
||
| 254 | * |
||
| 255 | * @return Reference to the correct response matcher service |
||
| 256 | */ |
||
| 257 | 7 | private function parseResponseMatcher(ContainerBuilder $container, array $config) |
|
| 258 | { |
||
| 259 | 7 | if (!empty($config['additional_response_status'])) { |
|
| 260 | 1 | $id = 'fos_http_cache.cache_control.expression.'.md5(serialize($config['additional_response_status'])); |
|
| 261 | 1 | if (!$container->hasDefinition($id)) { |
|
| 262 | $container |
||
| 263 | 1 | ->setDefinition($id, $this->createChildDefinition('fos_http_cache.response_matcher.cache_control.cacheable_response')) |
|
| 264 | 1 | ->setArguments([$config['additional_response_status']]) |
|
| 265 | ; |
||
| 266 | } |
||
| 267 | 6 | } elseif (!empty($config['match_response'])) { |
|
| 268 | 2 | $id = 'fos_http_cache.cache_control.match_response.'.md5($config['match_response']); |
|
| 269 | 2 | if (!$container->hasDefinition($id)) { |
|
| 270 | $container |
||
| 271 | 2 | ->setDefinition($id, $this->createChildDefinition('fos_http_cache.response_matcher.cache_control.expression')) |
|
| 272 | 2 | ->replaceArgument(0, $config['match_response']) |
|
| 273 | ; |
||
| 274 | } |
||
| 275 | } else { |
||
| 276 | 4 | $id = 'fos_http_cache.response_matcher.cacheable'; |
|
| 277 | } |
||
| 278 | |||
| 279 | 7 | return new Reference($id); |
|
| 280 | } |
||
| 281 | |||
| 282 | 6 | private function loadUserContext(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
| 283 | { |
||
| 284 | 6 | $configuredUserIdentifierHeaders = array_map('strtolower', $config['user_identifier_headers']); |
|
| 285 | 6 | $completeUserIdentifierHeaders = $configuredUserIdentifierHeaders; |
|
| 286 | 6 | if (false !== $config['session_name_prefix'] && !in_array('cookie', $completeUserIdentifierHeaders)) { |
|
| 287 | $completeUserIdentifierHeaders[] = 'cookie'; |
||
| 288 | } |
||
| 289 | |||
| 290 | 6 | $loader->load('user_context.xml'); |
|
| 291 | |||
| 292 | 6 | $container->getDefinition('fos_http_cache.user_context.request_matcher') |
|
| 293 | 6 | ->replaceArgument(0, $config['match']['accept']) |
|
| 294 | 6 | ->replaceArgument(1, $config['match']['method']); |
|
| 295 | |||
| 296 | 6 | $container->setParameter('fos_http_cache.event_listener.user_context.options', [ |
|
| 297 | 6 | 'user_identifier_headers' => $completeUserIdentifierHeaders, |
|
| 298 | 6 | 'user_hash_header' => $config['user_hash_header'], |
|
| 299 | 6 | 'ttl' => $config['hash_cache_ttl'], |
|
| 300 | 6 | 'add_vary_on_hash' => $config['always_vary_on_context_hash'], |
|
| 301 | ]); |
||
| 302 | |||
| 303 | 6 | $container->getDefinition('fos_http_cache.event_listener.user_context') |
|
| 304 | 6 | ->replaceArgument(0, new Reference($config['match']['matcher_service'])) |
|
| 305 | ; |
||
| 306 | |||
| 307 | $options = [ |
||
| 308 | 6 | 'user_identifier_headers' => $configuredUserIdentifierHeaders, |
|
| 309 | 6 | 'session_name_prefix' => $config['session_name_prefix'], |
|
| 310 | ]; |
||
| 311 | 6 | $container->getDefinition('fos_http_cache.user_context.anonymous_request_matcher') |
|
| 312 | 6 | ->replaceArgument(0, $options); |
|
| 313 | |||
| 314 | 6 | if ($config['logout_handler']['enabled']) { |
|
| 315 | 5 | $container->getDefinition('fos_http_cache.user_context_invalidator') |
|
| 316 | 5 | ->replaceArgument(1, $completeUserIdentifierHeaders) |
|
| 317 | 5 | ->replaceArgument(2, $config['match']['accept']); |
|
| 318 | |||
| 319 | 5 | $container->setAlias('security.logout.handler.session', 'fos_http_cache.user_context.session_logout_handler'); |
|
| 320 | } else { |
||
| 321 | 1 | $container->removeDefinition('fos_http_cache.user_context.logout_handler'); |
|
| 322 | 1 | $container->removeDefinition('fos_http_cache.user_context.session_logout_handler'); |
|
| 323 | 1 | $container->removeDefinition('fos_http_cache.user_context_invalidator'); |
|
| 324 | } |
||
| 325 | |||
| 326 | 6 | if ($config['role_provider']) { |
|
| 327 | 4 | $container->getDefinition('fos_http_cache.user_context.role_provider') |
|
| 328 | 4 | ->addTag(HashGeneratorPass::TAG_NAME) |
|
| 329 | 4 | ->setAbstract(false); |
|
| 330 | } |
||
| 331 | |||
| 332 | // Only decorate default SessionListener for Symfony 3.4 - 4.0 |
||
| 333 | // For Symfony 4.1+, the UserContextListener sets the header that tells |
||
| 334 | // the SessionListener to leave the cache-control header unchanged. |
||
| 335 | 6 | if (version_compare(Kernel::VERSION, '3.4', '>=') |
|
| 336 | 6 | && version_compare(Kernel::VERSION, '4.1', '<') |
|
| 337 | ) { |
||
| 338 | $container->getDefinition('fos_http_cache.user_context.session_listener') |
||
| 339 | ->setArgument(1, strtolower($config['user_hash_header'])) |
||
| 340 | ->setArgument(2, $completeUserIdentifierHeaders); |
||
| 341 | } else { |
||
| 342 | 6 | $container->removeDefinition('fos_http_cache.user_context.session_listener'); |
|
| 343 | } |
||
| 344 | 6 | } |
|
| 345 | |||
| 346 | 25 | private function loadProxyClient(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
| 347 | { |
||
| 348 | 25 | if (isset($config['varnish'])) { |
|
| 349 | 20 | $this->loadVarnish($container, $loader, $config['varnish']); |
|
| 350 | } |
||
| 351 | 24 | if (isset($config['nginx'])) { |
|
| 352 | 2 | $this->loadNginx($container, $loader, $config['nginx']); |
|
| 353 | } |
||
| 354 | 24 | if (isset($config['symfony'])) { |
|
| 355 | 2 | $this->loadSymfony($container, $loader, $config['symfony']); |
|
| 356 | } |
||
| 357 | 24 | if (isset($config['noop'])) { |
|
| 358 | 1 | $loader->load('noop.xml'); |
|
| 359 | } |
||
| 360 | |||
| 361 | 24 | $container->setAlias( |
|
| 362 | 24 | 'fos_http_cache.default_proxy_client', |
|
| 363 | 24 | 'fos_http_cache.proxy_client.'.$this->getDefaultProxyClient($config) |
|
| 364 | ); |
||
| 365 | 24 | $container->setAlias( |
|
| 366 | 24 | ProxyClient::class, |
|
| 367 | 24 | 'fos_http_cache.default_proxy_client' |
|
| 368 | ); |
||
| 369 | 24 | } |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Define the http dispatcher service for the proxy client $name. |
||
| 373 | * |
||
| 374 | * @param ContainerBuilder $container |
||
| 375 | * @param array $config |
||
| 376 | * @param string $serviceName |
||
| 377 | */ |
||
| 378 | 23 | private function createHttpDispatcherDefinition(ContainerBuilder $container, array $config, $serviceName) |
|
| 379 | { |
||
| 380 | 23 | foreach ($config['servers'] as $url) { |
|
| 381 | 23 | $this->validateUrl($url, 'Not a valid Varnish server address: "%s"'); |
|
| 382 | } |
||
| 383 | 23 | if (!empty($config['base_url'])) { |
|
| 384 | 23 | $baseUrl = $this->prefixSchema($config['base_url']); |
|
| 385 | 23 | $this->validateUrl($baseUrl, 'Not a valid base path: "%s"'); |
|
| 386 | } else { |
||
| 387 | $baseUrl = null; |
||
| 388 | } |
||
| 389 | 22 | $httpClient = null; |
|
| 390 | 22 | if ($config['http_client']) { |
|
| 391 | 1 | $httpClient = new Reference($config['http_client']); |
|
| 392 | } |
||
| 393 | |||
| 394 | 22 | $definition = new Definition(HttpDispatcher::class, [ |
|
| 395 | 22 | $config['servers'], |
|
| 396 | 22 | $baseUrl, |
|
| 397 | 22 | $httpClient, |
|
| 398 | ]); |
||
| 399 | |||
| 400 | 22 | $container->setDefinition($serviceName, $definition); |
|
| 401 | 22 | } |
|
| 402 | |||
| 403 | 20 | private function loadVarnish(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
| 404 | { |
||
| 405 | 20 | $this->createHttpDispatcherDefinition($container, $config['http'], 'fos_http_cache.proxy_client.varnish.http_dispatcher'); |
|
| 406 | $options = [ |
||
| 407 | 19 | 'tag_mode' => $config['tag_mode'], |
|
| 408 | 19 | 'tags_header' => $config['tags_header'], |
|
| 409 | ]; |
||
| 410 | |||
| 411 | 19 | if (!empty($config['header_length'])) { |
|
| 412 | $options['header_length'] = $config['header_length']; |
||
| 413 | } |
||
| 414 | 19 | if (!empty($config['default_ban_headers'])) { |
|
| 415 | $options['default_ban_headers'] = $config['default_ban_headers']; |
||
| 416 | } |
||
| 417 | 19 | $container->setParameter('fos_http_cache.proxy_client.varnish.options', $options); |
|
| 418 | |||
| 419 | 19 | $loader->load('varnish.xml'); |
|
| 420 | 19 | } |
|
| 421 | |||
| 422 | 2 | private function loadNginx(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
| 430 | |||
| 431 | 2 | private function loadSymfony(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
| 456 | |||
| 457 | /** |
||
| 458 | * @param ContainerBuilder $container |
||
| 459 | * @param XmlFileLoader $loader |
||
| 460 | * @param array $config Configuration section for the tags node |
||
| 461 | * @param string $client Name of the client used with the cache manager, |
||
| 462 | * "custom" when a custom client is used |
||
| 463 | */ |
||
| 464 | 25 | private function loadCacheTagging(ContainerBuilder $container, XmlFileLoader $loader, array $config, $client) |
|
| 496 | |||
| 497 | 2 | private function loadTest(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
| 505 | |||
| 506 | 2 | private function loadProxyServer(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
| 521 | |||
| 522 | 2 | private function loadVarnishProxyServer(ContainerBuilder $container, XmlFileLoader $loader, $config) |
|
| 532 | |||
| 533 | private function loadNginxProxyServer(ContainerBuilder $container, XmlFileLoader $loader, $config) |
||
| 543 | |||
| 544 | 3 | private function loadTagRules(ContainerBuilder $container, array $config) |
|
| 559 | |||
| 560 | 3 | private function loadInvalidatorRules(ContainerBuilder $container, array $config) |
|
| 569 | |||
| 570 | 23 | private function validateUrl($url, $msg) |
|
| 578 | |||
| 579 | 23 | private function prefixSchema($url) |
|
| 587 | |||
| 588 | 24 | private function getDefaultProxyClient(array $config) |
|
| 612 | |||
| 613 | /** |
||
| 614 | * Build the child definition with fallback for Symfony versions < 3.3. |
||
| 615 | * |
||
| 616 | * @param string $id Id of the service to extend |
||
| 617 | * |
||
| 618 | * @return ChildDefinition|DefinitionDecorator |
||
| 619 | */ |
||
| 620 | 9 | private function createChildDefinition($id) |
|
| 628 | } |
||
| 629 |
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: