Complex classes like SonataCacheExtension 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 SonataCacheExtension, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class SonataCacheExtension extends Extension |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * Loads the url shortener configuration. |
||
| 31 | * |
||
| 32 | * @param array $configs An array of configuration settings |
||
| 33 | * @param ContainerBuilder $container A ContainerBuilder instance |
||
| 34 | */ |
||
| 35 | public function load(array $configs, ContainerBuilder $container) |
||
| 36 | { |
||
| 37 | $processor = new Processor(); |
||
| 38 | $configuration = new Configuration(); |
||
| 39 | $config = $processor->processConfiguration($configuration, $configs); |
||
| 40 | |||
| 41 | $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
||
| 42 | $loader->load('cache.xml'); |
||
| 43 | $loader->load('counter.xml'); |
||
| 44 | |||
| 45 | $useOrm = 'auto' == $config['cache_invalidation']['orm_listener'] ? |
||
| 46 | class_exists('Doctrine\\ORM\\Version') : |
||
| 47 | $config['cache_invalidation']['orm_listener']; |
||
| 48 | |||
| 49 | if ($useOrm) { |
||
| 50 | $loader->load('orm.xml'); |
||
| 51 | } |
||
| 52 | |||
| 53 | $usePhpcrOdm = 'auto' == $config['cache_invalidation']['phpcr_odm_listener'] ? |
||
| 54 | class_exists('Doctrine\\PHPCR\\ODM\\Version') : |
||
| 55 | $config['cache_invalidation']['phpcr_odm_listener']; |
||
| 56 | if ($usePhpcrOdm) { |
||
| 57 | $loader->load('phpcr_odm.xml'); |
||
| 58 | } |
||
| 59 | |||
| 60 | $this->configureInvalidation($container, $config); |
||
| 61 | if ($useOrm) { |
||
| 62 | $this->configureORM($container, $config); |
||
| 63 | } |
||
| 64 | if ($usePhpcrOdm) { |
||
| 65 | $this->configurePHPCRODM($container, $config); |
||
| 66 | } |
||
| 67 | $this->configureCache($container, $config); |
||
| 68 | $this->configureCounter($container, $config); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param ContainerBuilder $container |
||
| 73 | * @param array $config |
||
| 74 | */ |
||
| 75 | public function configureInvalidation(ContainerBuilder $container, $config) |
||
| 76 | { |
||
| 77 | $cacheManager = $container->getDefinition('sonata.cache.manager'); |
||
| 78 | |||
| 79 | $cacheManager->replaceArgument(0, new Reference($config['cache_invalidation']['service'])); |
||
| 80 | |||
| 81 | $recorder = $container->getDefinition('sonata.cache.model_identifier'); |
||
| 82 | foreach ($config['cache_invalidation']['classes'] as $class => $method) { |
||
| 83 | $recorder->addMethodCall('addClass', array($class, $method)); |
||
| 84 | } |
||
| 85 | |||
| 86 | $cacheManager->addMethodCall('setRecorder', array(new Reference($config['cache_invalidation']['recorder']))); |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @param ContainerBuilder $container |
||
| 91 | * @param array $config |
||
| 92 | */ |
||
| 93 | public function configureORM(ContainerBuilder $container, $config) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @param ContainerBuilder $container |
||
| 105 | * @param array $config |
||
| 106 | */ |
||
| 107 | public function configurePHPCRODM(ContainerBuilder $container, $config) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param ContainerBuilder $container |
||
| 119 | * @param array $config |
||
| 120 | * |
||
| 121 | * @throws \RuntimeException if the Mongo or Memcached library is not installed |
||
| 122 | */ |
||
| 123 | public function configureCache(ContainerBuilder $container, $config) |
||
| 124 | { |
||
| 125 | if ($config['default_cache']) { |
||
| 126 | $container->setAlias('sonata.cache', $config['default_cache']); |
||
| 127 | } |
||
| 128 | |||
| 129 | if (isset($config['caches']['esi'])) { |
||
| 130 | $container |
||
| 131 | ->getDefinition('sonata.cache.esi') |
||
| 132 | ->replaceArgument(0, $config['caches']['esi']['token']) |
||
| 133 | ->replaceArgument(1, $config['caches']['esi']['servers']) |
||
| 134 | ->replaceArgument(3, 3 === $config['caches']['esi']['version'] ? 'ban' : 'purge'); |
||
| 135 | } else { |
||
| 136 | $container->removeDefinition('sonata.cache.esi'); |
||
| 137 | } |
||
| 138 | |||
| 139 | if (isset($config['caches']['ssi'])) { |
||
| 140 | $container |
||
| 141 | ->getDefinition('sonata.cache.ssi') |
||
| 142 | ->replaceArgument(0, $config['caches']['ssi']['token']) |
||
| 143 | ; |
||
| 144 | } else { |
||
| 145 | $container->removeDefinition('sonata.cache.ssi'); |
||
| 146 | } |
||
| 147 | |||
| 148 | if (isset($config['caches']['mongo'])) { |
||
| 149 | $this->checkMongo(); |
||
| 150 | |||
| 151 | $database = $config['caches']['mongo']['database']; |
||
| 152 | $servers = array(); |
||
| 153 | foreach ($config['caches']['mongo']['servers'] as $server) { |
||
| 154 | if ($server['user']) { |
||
| 155 | $servers[] = sprintf('%s:%s@%s:%s/%s', $server['user'], $server['password'], $server['host'], $server['port'], $database); |
||
| 156 | } else { |
||
| 157 | $servers[] = sprintf('%s:%s', $server['host'], $server['port']); |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | $container |
||
| 162 | ->getDefinition('sonata.cache.mongo') |
||
| 163 | ->replaceArgument(0, $servers) |
||
| 164 | ->replaceArgument(1, $database) |
||
| 165 | ->replaceArgument(2, $config['caches']['mongo']['collection']) |
||
| 166 | ; |
||
| 167 | } else { |
||
| 168 | $container->removeDefinition('sonata.cache.mongo'); |
||
| 169 | } |
||
| 170 | |||
| 171 | if (isset($config['caches']['memcached'])) { |
||
| 172 | $this->checkMemcached(); |
||
| 173 | |||
| 174 | $container |
||
| 175 | ->getDefinition('sonata.cache.memcached') |
||
| 176 | ->replaceArgument(0, $config['caches']['memcached']['prefix']) |
||
| 177 | ->replaceArgument(1, $config['caches']['memcached']['servers']) |
||
| 178 | ; |
||
| 179 | } else { |
||
| 180 | $container->removeDefinition('sonata.cache.memcached'); |
||
| 181 | } |
||
| 182 | |||
| 183 | if (isset($config['caches']['predis'])) { |
||
| 184 | $this->checkPRedis(); |
||
| 185 | |||
| 186 | $container |
||
| 187 | ->getDefinition('sonata.cache.predis') |
||
| 188 | ->replaceArgument(0, $config['caches']['predis']['servers']) |
||
| 189 | ; |
||
| 190 | } else { |
||
| 191 | $container->removeDefinition('sonata.cache.predis'); |
||
| 192 | } |
||
| 193 | |||
| 194 | if (isset($config['caches']['apc'])) { |
||
| 195 | $this->checkApc(); |
||
| 196 | |||
| 197 | $container |
||
| 198 | ->getDefinition('sonata.cache.apc') |
||
| 199 | ->replaceArgument(1, $config['caches']['apc']['token']) |
||
| 200 | ->replaceArgument(2, $config['caches']['apc']['prefix']) |
||
| 201 | ->replaceArgument(3, $this->configureServers($config['caches']['apc']['servers'])) |
||
| 202 | ->replaceArgument(4, $config['caches']['apc']['timeout']) |
||
| 203 | ; |
||
| 204 | } else { |
||
| 205 | $container->removeDefinition('sonata.cache.apc'); |
||
| 206 | } |
||
| 207 | |||
| 208 | if (isset($config['caches']['symfony'])) { |
||
| 209 | $container |
||
| 210 | ->getDefinition('sonata.cache.symfony') |
||
| 211 | ->replaceArgument(3, $config['caches']['symfony']['token']) |
||
| 212 | ->replaceArgument(4, $config['caches']['symfony']['php_cache_enabled']) |
||
| 213 | ->replaceArgument(5, $config['caches']['symfony']['types']) |
||
| 214 | ->replaceArgument(6, $this->configureServers($config['caches']['symfony']['servers'])) |
||
| 215 | ; |
||
| 216 | } else { |
||
| 217 | $container->removeDefinition('sonata.cache.symfony'); |
||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @param ContainerBuilder $container |
||
| 223 | * @param array $config |
||
| 224 | * |
||
| 225 | * @throws \RuntimeException if the Mongo or Memcached library is not installed |
||
| 226 | */ |
||
| 227 | public function configureCounter(ContainerBuilder $container, $config) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Returns servers list with hash for basic auth computed if provided. |
||
| 292 | * |
||
| 293 | * @param array $servers |
||
| 294 | * |
||
| 295 | * @return array |
||
| 296 | */ |
||
| 297 | public function configureServers(array $servers) |
||
| 310 | |||
| 311 | protected function checkMemcached() |
||
| 323 | |||
| 324 | protected function checkApc() |
||
| 336 | |||
| 337 | protected function checkMongo() |
||
| 349 | |||
| 350 | protected function checkPRedis() |
||
| 361 | } |
||
| 362 |