Complex classes like DoctrineExtension 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 DoctrineExtension, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
59 | class DoctrineExtension extends CompilerExtension |
||
60 | { |
||
61 | /** |
||
62 | * @var string |
||
63 | */ |
||
64 | private const DOCTRINE_SQL_PANEL = DoctrineSQLPanel::class; |
||
65 | |||
66 | private $classMappings = []; |
||
67 | |||
68 | private $entitySources = []; |
||
69 | |||
70 | /** |
||
71 | * @var array |
||
72 | */ |
||
73 | private static $defaults = [ |
||
74 | 'debug' => '%debugMode%', |
||
75 | 'dbal' => [ |
||
76 | 'type_overrides' => [], |
||
77 | 'types' => [], |
||
78 | 'schema_filter' => null, |
||
79 | ], |
||
80 | 'prefix' => 'doctrine.default', |
||
81 | 'proxyDir' => '%tempDir%/cache/proxies', |
||
82 | 'proxyNamespace' => 'DoctrineProxies', |
||
83 | 'sourceDir' => null, |
||
84 | 'entityManagerClassName' => EntityManager::class, |
||
85 | 'defaultRepositoryClassName' => EntityRepository::class, |
||
86 | 'repositoryFactory' => null, |
||
87 | 'namingStrategy' => UnderscoreNamingStrategy::class, |
||
88 | 'sqlLogger' => null, |
||
89 | 'targetEntityMappings' => [], |
||
90 | 'metadata' => [], |
||
91 | 'functions' => [], |
||
92 | // caches |
||
93 | 'metadataCache' => 'default', |
||
94 | 'queryCache' => 'default', |
||
95 | 'resultCache' => 'default', |
||
96 | 'hydrationCache' => 'default', |
||
97 | 'secondLevelCache' => [ |
||
98 | 'enabled' => false, |
||
99 | 'factoryClass' => DefaultCacheFactory::class, |
||
100 | 'driver' => 'default', |
||
101 | 'regions' => [ |
||
102 | 'defaultLifetime' => 3600, |
||
103 | 'defaultLockLifetime' => 60, |
||
104 | ], |
||
105 | 'fileLockRegionDirectory' => '%tempDir%/cache/Doctrine.Cache.Locks', |
||
106 | 'logging' => '%debugMode%', |
||
107 | ], |
||
108 | 'cache' => [ |
||
109 | 'redis' => [ |
||
110 | 'class' => RedisCache::class, |
||
111 | ], |
||
112 | ], |
||
113 | ]; |
||
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | 1 | public function loadConfiguration(): void |
|
119 | { |
||
120 | 1 | $config = $this->parseConfig(); |
|
121 | |||
122 | 1 | $builder = $this->getContainerBuilder(); |
|
123 | 1 | $name = $config['prefix']; |
|
124 | |||
125 | 1 | $builder->addDefinition($name . '.namingStrategy') |
|
126 | 1 | ->setType($config['namingStrategy']); |
|
127 | |||
128 | 1 | $configurationDefinition = $builder->addDefinition($name . '.config') |
|
129 | 1 | ->setType(Configuration::class) |
|
130 | 1 | ->addSetup('setFilterSchemaAssetsExpression', [$config['dbal']['schema_filter']]) |
|
131 | 1 | ->addSetup('setDefaultRepositoryClassName', [$config['defaultRepositoryClassName']]) |
|
132 | 1 | ->addSetup('setProxyDir', [$config['proxyDir']]) |
|
133 | 1 | ->addSetup('setProxyNamespace', [$config['proxyNamespace']]) |
|
134 | 1 | ->addSetup('setAutoGenerateProxyClasses', [$config['debug']]) |
|
135 | 1 | ->addSetup('setNamingStrategy', ['@' . $name . '.namingStrategy']); |
|
136 | |||
137 | 1 | $builder->addDefinition($name . '.annotationReader') |
|
138 | 1 | ->setType(AnnotationReader::class) |
|
139 | 1 | ->setAutowired(false); |
|
140 | |||
141 | 1 | $metadataCache = $this->getCache($name . '.metadata', $builder, $config['metadataCache'] ?: 'array'); |
|
142 | 1 | $builder->addDefinition($name . '.reader') |
|
143 | 1 | ->setType(Reader::class) |
|
144 | 1 | ->setFactory(CachedReader::class, ['@' . $name . '.annotationReader', $metadataCache, $config['debug']]); |
|
145 | |||
146 | 1 | $builder->addDefinition($name . '.annotationDriver') |
|
147 | 1 | ->setFactory(AnnotationDriver::class, ['@' . $name . '.reader', array_values($this->entitySources)]); |
|
148 | |||
149 | 1 | $configurationDefinition->addSetup('setMetadataDriverImpl', ['@' . $name . '.annotationDriver']); |
|
150 | |||
151 | 1 | foreach ($config['functions'] as $functionName => $function) { |
|
152 | 1 | $configurationDefinition->addSetup('addCustomStringFunction', [$functionName, $function]); |
|
153 | } |
||
154 | |||
155 | 1 | if ($config['repositoryFactory']) { |
|
156 | $builder->addDefinition($name . '.repositoryFactory') |
||
157 | ->setType($config['repositoryFactory']); |
||
158 | $configurationDefinition->addSetup('setRepositoryFactory', ['@' . $name . '.repositoryFactory']); |
||
159 | } |
||
160 | 1 | if ($config['sqlLogger']) { |
|
161 | $builder->addDefinition($name . '.sqlLogger') |
||
162 | ->setType($config['sqlLogger']); |
||
163 | $configurationDefinition->addSetup('setSQLLogger', ['@' . $name . '.sqlLogger']); |
||
164 | } |
||
165 | |||
166 | 1 | if ($config['metadataCache'] !== false) { |
|
167 | 1 | $configurationDefinition->addSetup( |
|
168 | 1 | 'setMetadataCacheImpl', |
|
169 | 1 | [$this->getCache($name . '.metadata', $builder, $config['metadataCache'])] |
|
170 | ); |
||
171 | } |
||
172 | |||
173 | 1 | if ($config['queryCache'] !== false) { |
|
174 | 1 | $configurationDefinition->addSetup( |
|
175 | 1 | 'setQueryCacheImpl', |
|
176 | 1 | [$this->getCache($name . '.query', $builder, $config['queryCache'])] |
|
177 | ); |
||
178 | } |
||
179 | |||
180 | 1 | if ($config['resultCache'] !== false) { |
|
181 | 1 | $configurationDefinition->addSetup( |
|
182 | 1 | 'setResultCacheImpl', |
|
183 | 1 | [$this->getCache($name . '.ormResult', $builder, $config['resultCache'])] |
|
184 | ); |
||
185 | } |
||
186 | |||
187 | 1 | if ($config['hydrationCache'] !== false) { |
|
188 | 1 | $configurationDefinition->addSetup( |
|
189 | 1 | 'setHydrationCacheImpl', |
|
190 | 1 | [$this->getCache($name . '.hydration', $builder, $config['hydrationCache'])] |
|
191 | ); |
||
192 | } |
||
193 | |||
194 | 1 | $this->processSecondLevelCache($name, $config['secondLevelCache']); |
|
195 | |||
196 | 1 | $builder->addDefinition($name . '.connection') |
|
197 | 1 | ->setType(Connection::class) |
|
198 | 1 | ->setFactory('@' . $name . '.entityManager::getConnection'); |
|
199 | |||
200 | 1 | $builder->addDefinition($name . '.entityManager') |
|
201 | 1 | ->setType($config['entityManagerClassName']) |
|
202 | 1 | ->setFactory( |
|
203 | 1 | $config['entityManagerClassName'] . '::create', |
|
204 | 1 | [$config['connection'], '@' . $name . '.config', '@Doctrine\Common\EventManager'] |
|
205 | ); |
||
206 | |||
207 | 1 | $builder->addDefinition($name . '.resolver') |
|
208 | 1 | ->setType(ResolveTargetEntityListener::class); |
|
209 | |||
210 | 1 | if ($config['debug'] === true) { |
|
211 | $builder->addDefinition($this->prefix($name . '.diagnosticsPanel')) |
||
212 | ->setType(self::DOCTRINE_SQL_PANEL); |
||
213 | } |
||
214 | |||
215 | // import Doctrine commands into Symfony/Console if exists |
||
216 | 1 | $this->registerCommandsIntoConsole($builder, $name); |
|
217 | 1 | } |
|
218 | |||
219 | /** |
||
220 | * {@inheritdoc} |
||
221 | */ |
||
222 | 1 | public function beforeCompile(): void |
|
223 | { |
||
224 | 1 | $config = $this->getConfig(self::$defaults); |
|
225 | 1 | $name = $config['prefix']; |
|
226 | |||
227 | 1 | $builder = $this->getContainerBuilder(); |
|
228 | |||
229 | 1 | foreach ($this->classMappings as $source => $target) { |
|
230 | $builder->getDefinition($name . '.resolver') |
||
231 | ->addSetup('addResolveTargetEntity', [$source, $target, []]); |
||
232 | } |
||
233 | |||
234 | 1 | $this->processDbalTypes($name, $config['dbal']['types']); |
|
235 | 1 | $this->processDbalTypeOverrides($name, $config['dbal']['type_overrides']); |
|
236 | 1 | $this->processEventSubscribers($name); |
|
237 | 1 | $this->processFilters(); |
|
238 | 1 | } |
|
239 | |||
240 | /** |
||
241 | * {@inheritdoc} |
||
242 | */ |
||
243 | 1 | public function afterCompile(ClassType $classType): void |
|
244 | { |
||
245 | 1 | $config = $this->getConfig(self::$defaults); |
|
246 | 1 | $initialize = $classType->methods['initialize']; |
|
247 | |||
248 | 1 | $initialize->addBody('?::registerUniqueLoader("class_exists");', [new PhpLiteral(AnnotationRegistry::class)]); |
|
249 | |||
250 | 1 | if ($config['debug'] === true) { |
|
251 | $initialize->addBody('$this->getByType(\'' . self::DOCTRINE_SQL_PANEL . '\')->bindToBar();'); |
||
252 | } |
||
253 | |||
254 | 1 | $builder = $this->getContainerBuilder(); |
|
255 | 1 | $filterDefinitions = $builder->findByType(SQLFilter::class); |
|
256 | 1 | if ($filterDefinitions !== []) { |
|
257 | $initialize->addBody( |
||
258 | '$filterCollection = $this->getByType(\'' . EntityManagerInterface::class . '\')->getFilters();' |
||
259 | ); |
||
260 | foreach (array_keys($filterDefinitions) as $name) { |
||
261 | $initialize->addBody('$filterCollection->enable(\'' . $name . '\');'); |
||
262 | } |
||
263 | } |
||
264 | 1 | } |
|
265 | |||
266 | 1 | protected function processSecondLevelCache($name, array $config): void |
|
310 | |||
311 | /** |
||
312 | * @throws AssertionException |
||
313 | */ |
||
314 | 1 | private function parseConfig(): array |
|
340 | |||
341 | 1 | private function getCache(string $prefix, ContainerBuilder $containerBuilder, string $cacheType): string |
|
387 | |||
388 | 1 | private function registerCommandsIntoConsole(ContainerBuilder $containerBuilder, string $name): void |
|
389 | { |
||
390 | 1 | if ($this->hasSymfonyConsole()) { |
|
417 | |||
418 | 1 | private function processDbalTypes(string $name, array $types): void |
|
430 | |||
431 | 1 | private function processDbalTypeOverrides(string $name, array $types): void |
|
440 | |||
441 | 1 | private function processEventSubscribers(string $name): void |
|
458 | |||
459 | 1 | private function processFilters(): void |
|
468 | |||
469 | 1 | private function hasSymfonyConsole(): bool |
|
473 | |||
474 | 1 | private function hasEventManager(ContainerBuilder $containerBuilder): bool |
|
479 | } |
||
480 |