Total Complexity | 48 |
Total Lines | 346 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ServiceProvider 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.
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 ServiceProvider, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class ServiceProvider extends AbstractServiceProvider |
||
33 | { |
||
34 | protected static function getPackagePath(): string |
||
35 | { |
||
36 | return __DIR__ . '/../'; |
||
37 | } |
||
38 | |||
39 | public function getFactories(): array |
||
40 | { |
||
41 | return [ |
||
42 | SymfonyEventDispatcher::class => [ static::class, 'getSymfonyEventDispatcher' ], |
||
43 | Cache\CacheManager::class => [ static::class, 'getCacheManager' ], |
||
44 | Charset\CharsetConverter::class => [ static::class, 'getCharsetConverter' ], |
||
45 | Configuration\SiteConfiguration::class => [ static::class, 'getSiteConfiguration' ], |
||
46 | Command\ListCommand::class => [ static::class, 'getListCommand' ], |
||
47 | HelpCommand::class => [ static::class, 'getHelpCommand' ], |
||
48 | Command\DumpAutoloadCommand::class => [ static::class, 'getDumpAutoloadCommand' ], |
||
49 | Console\CommandApplication::class => [ static::class, 'getConsoleCommandApplication' ], |
||
50 | Console\CommandRegistry::class => [ static::class, 'getConsoleCommandRegistry' ], |
||
51 | Context\Context::class => [ static::class, 'getContext' ], |
||
52 | Crypto\PasswordHashing\PasswordHashFactory::class => [ static::class, 'getPasswordHashFactory' ], |
||
53 | EventDispatcher\EventDispatcher::class => [ static::class, 'getEventDispatcher' ], |
||
54 | EventDispatcher\ListenerProvider::class => [ static::class, 'getEventListenerProvider' ], |
||
55 | Http\MiddlewareStackResolver::class => [ static::class, 'getMiddlewareStackResolver' ], |
||
56 | Http\RequestFactory::class => [ static::class, 'getRequestFactory' ], |
||
57 | Imaging\IconFactory::class => [ static::class, 'getIconFactory' ], |
||
58 | Imaging\IconRegistry::class => [ static::class, 'getIconRegistry' ], |
||
59 | Localization\LanguageServiceFactory::class => [ static::class, 'getLanguageServiceFactory' ], |
||
60 | Localization\LanguageStore::class => [ static::class, 'getLanguageStore' ], |
||
61 | Localization\Locales::class => [ static::class, 'getLocales' ], |
||
62 | Localization\LocalizationFactory::class => [ static::class, 'getLocalizationFactory' ], |
||
63 | Mail\TransportFactory::class => [ static::class, 'getMailTransportFactory' ], |
||
64 | Messaging\FlashMessageService::class => [ static::class, 'getFlashMessageService' ], |
||
65 | Middleware\ResponsePropagation::class => [ static::class, 'getResponsePropagationMiddleware' ], |
||
66 | Package\FailsafePackageManager::class => [ static::class, 'getFailsafePackageManager' ], |
||
67 | Registry::class => [ static::class, 'getRegistry' ], |
||
68 | Resource\Index\FileIndexRepository::class => [ static::class, 'getFileIndexRepository' ], |
||
69 | Resource\Index\MetaDataRepository::class => [ static::class, 'getMetaDataRepository' ], |
||
70 | Resource\Driver\DriverRegistry::class => [ static::class, 'getDriverRegistry' ], |
||
71 | Resource\ProcessedFileRepository::class => [ static::class, 'getProcessedFileRepository' ], |
||
72 | Resource\ResourceFactory::class => [ static::class, 'getResourceFactory' ], |
||
73 | Resource\StorageRepository::class => [ static::class, 'getStorageRepository' ], |
||
74 | Service\DependencyOrderingService::class => [ static::class, 'getDependencyOrderingService' ], |
||
75 | Service\FlexFormService::class => [ static::class, 'getFlexFormService' ], |
||
76 | Service\OpcodeCacheService::class => [ static::class, 'getOpcodeCacheService' ], |
||
77 | TimeTracker\TimeTracker::class => [ static::class, 'getTimeTracker' ], |
||
78 | TypoScript\Parser\ConstantConfigurationParser::class => [ static::class, 'getTypoScriptConstantConfigurationParser' ], |
||
79 | TypoScript\TypoScriptService::class => [ static::class, 'getTypoScriptService' ], |
||
80 | 'middlewares' => [ static::class, 'getMiddlewares' ], |
||
81 | ]; |
||
82 | } |
||
83 | |||
84 | public function getExtensions(): array |
||
85 | { |
||
86 | return [ |
||
87 | Console\CommandRegistry::class => [ static::class, 'configureCommands' ], |
||
88 | EventDispatcherInterface::class => [ static::class, 'provideFallbackEventDispatcher' ], |
||
89 | EventDispatcher\ListenerProvider::class => [ static::class, 'extendEventListenerProvider' ], |
||
90 | ] + parent::getExtensions(); |
||
91 | } |
||
92 | |||
93 | public static function getSymfonyEventDispatcher(ContainerInterface $container): SymfonyEventDispatcherInterface |
||
94 | { |
||
95 | return self::new($container, SymfonyEventDispatcher::class, [ |
||
96 | $container->get(EventDispatcherInterface::class) |
||
97 | ]); |
||
98 | } |
||
99 | |||
100 | public static function getCacheManager(ContainerInterface $container): Cache\CacheManager |
||
101 | { |
||
102 | if (!$container->get('boot.state')->done) { |
||
103 | throw new \LogicException(Cache\CacheManager::class . ' can not be injected/instantiated during ext_localconf.php loading. Use lazy loading instead.', 1549446998); |
||
104 | } |
||
105 | |||
106 | $cacheConfigurations = $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'] ?? []; |
||
107 | $disableCaching = $container->get('boot.state')->cacheDisabled; |
||
108 | $defaultCaches = [ |
||
109 | $container->get('cache.core'), |
||
110 | $container->get('cache.assets'), |
||
111 | $container->get('cache.di'), |
||
112 | ]; |
||
113 | |||
114 | $cacheManager = self::new($container, Cache\CacheManager::class, [$disableCaching]); |
||
115 | $cacheManager->setCacheConfigurations($cacheConfigurations); |
||
116 | $cacheConfigurations['di']['groups'] = ['system']; |
||
117 | foreach ($defaultCaches as $cache) { |
||
118 | $cacheManager->registerCache($cache, $cacheConfigurations[$cache->getIdentifier()]['groups'] ?? ['all']); |
||
119 | } |
||
120 | |||
121 | return $cacheManager; |
||
122 | } |
||
123 | |||
124 | public static function getCharsetConverter(ContainerInterface $container): Charset\CharsetConverter |
||
125 | { |
||
126 | return self::new($container, Charset\CharsetConverter::class); |
||
127 | } |
||
128 | |||
129 | public static function getSiteConfiguration(ContainerInterface $container): Configuration\SiteConfiguration |
||
130 | { |
||
131 | return self::new($container, Configuration\SiteConfiguration::class, [Environment::getConfigPath() . '/sites']); |
||
132 | } |
||
133 | |||
134 | public static function getListCommand(ContainerInterface $container): Command\ListCommand |
||
135 | { |
||
136 | return new Command\ListCommand( |
||
137 | $container->get(Console\CommandRegistry::class) |
||
138 | ); |
||
139 | } |
||
140 | |||
141 | public static function getHelpCommand(ContainerInterface $container): HelpCommand |
||
142 | { |
||
143 | return new HelpCommand(); |
||
144 | } |
||
145 | |||
146 | public static function getDumpAutoloadCommand(ContainerInterface $container): Command\DumpAutoloadCommand |
||
147 | { |
||
148 | return new Command\DumpAutoloadCommand(); |
||
149 | } |
||
150 | |||
151 | public static function getConsoleCommandApplication(ContainerInterface $container): Console\CommandApplication |
||
152 | { |
||
153 | return new Console\CommandApplication( |
||
154 | $container->get(Context\Context::class), |
||
155 | $container->get(Console\CommandRegistry::class) |
||
156 | ); |
||
157 | } |
||
158 | |||
159 | public static function getConsoleCommandRegistry(ContainerInterface $container): Console\CommandRegistry |
||
160 | { |
||
161 | return new Console\CommandRegistry($container); |
||
162 | } |
||
163 | |||
164 | public static function getEventDispatcher(ContainerInterface $container): EventDispatcher\EventDispatcher |
||
165 | { |
||
166 | return new EventDispatcher\EventDispatcher( |
||
167 | $container->get(EventDispatcher\ListenerProvider::class) |
||
168 | ); |
||
169 | } |
||
170 | |||
171 | public static function getEventListenerProvider(ContainerInterface $container): EventDispatcher\ListenerProvider |
||
172 | { |
||
173 | return new EventDispatcher\ListenerProvider($container); |
||
174 | } |
||
175 | |||
176 | public static function extendEventListenerProvider( |
||
177 | ContainerInterface $container, |
||
178 | EventDispatcher\ListenerProvider $listenerProvider |
||
179 | ): EventDispatcher\ListenerProvider { |
||
180 | $listenerProvider->addListener( |
||
181 | Package\Event\PackagesMayHaveChangedEvent::class, |
||
182 | Package\PackageManager::class, |
||
183 | 'packagesMayHaveChanged' |
||
184 | ); |
||
185 | return $listenerProvider; |
||
186 | } |
||
187 | |||
188 | public static function getContext(ContainerInterface $container): Context\Context |
||
189 | { |
||
190 | return new Context\Context(); |
||
191 | } |
||
192 | |||
193 | public static function getPasswordHashFactory(ContainerInterface $container): Crypto\PasswordHashing\PasswordHashFactory |
||
194 | { |
||
195 | return new Crypto\PasswordHashing\PasswordHashFactory(); |
||
196 | } |
||
197 | |||
198 | public static function getIconFactory(ContainerInterface $container): Imaging\IconFactory |
||
199 | { |
||
200 | return self::new($container, Imaging\IconFactory::class, [ |
||
201 | $container->get(EventDispatcherInterface::class), |
||
202 | $container->get(Imaging\IconRegistry::class) |
||
203 | ]); |
||
204 | } |
||
205 | |||
206 | public static function getIconRegistry(ContainerInterface $container): Imaging\IconRegistry |
||
207 | { |
||
208 | return self::new($container, Imaging\IconRegistry::class); |
||
209 | } |
||
210 | |||
211 | public static function getLanguageServiceFactory(ContainerInterface $container): Localization\LanguageServiceFactory |
||
212 | { |
||
213 | return self::new($container, Localization\LanguageServiceFactory::class, [ |
||
214 | $container->get(Localization\Locales::class), |
||
215 | $container->get(Localization\LocalizationFactory::class) |
||
216 | ]); |
||
217 | } |
||
218 | |||
219 | public static function getLanguageStore(ContainerInterface $container): Localization\LanguageStore |
||
220 | { |
||
221 | return self::new($container, Localization\LanguageStore::class); |
||
222 | } |
||
223 | |||
224 | public static function getLocales(ContainerInterface $container): Localization\Locales |
||
225 | { |
||
226 | return self::new($container, Localization\Locales::class); |
||
227 | } |
||
228 | |||
229 | public static function getLocalizationFactory(ContainerInterface $container): Localization\LocalizationFactory |
||
230 | { |
||
231 | return self::new($container, Localization\LocalizationFactory::class, [ |
||
232 | $container->get(Localization\LanguageStore::class), |
||
233 | $container->get(Cache\CacheManager::class) |
||
234 | ]); |
||
235 | } |
||
236 | |||
237 | public static function getMailTransportFactory(ContainerInterface $container): Mail\TransportFactory |
||
238 | { |
||
239 | return self::new($container, Mail\TransportFactory::class, [ |
||
240 | $container->get(SymfonyEventDispatcher::class), |
||
241 | $container->get(Log\LogManager::class) |
||
242 | ]); |
||
243 | } |
||
244 | |||
245 | public static function getFlashMessageService(ContainerInterface $container): Messaging\FlashMessageService |
||
246 | { |
||
247 | return self::new($container, Messaging\FlashMessageService::class); |
||
248 | } |
||
249 | |||
250 | public static function getResponsePropagationMiddleware(ContainerInterface $container): Middleware\ResponsePropagation |
||
251 | { |
||
252 | return self::new($container, Middleware\ResponsePropagation::class); |
||
253 | } |
||
254 | |||
255 | public static function getFailsafePackageManager(ContainerInterface $container): Package\FailsafePackageManager |
||
256 | { |
||
257 | $packageManager = $container->get(Package\PackageManager::class); |
||
258 | if ($packageManager instanceof Package\FailsafePackageManager) { |
||
259 | return $packageManager; |
||
260 | } |
||
261 | throw new \RuntimeException('FailsafePackageManager can only be instantiated in failsafe (maintenance tool) mode.', 1586861816); |
||
262 | } |
||
263 | |||
264 | public static function getRegistry(ContainerInterface $container): Registry |
||
265 | { |
||
266 | return self::new($container, Registry::class); |
||
267 | } |
||
268 | |||
269 | public static function getFileIndexRepository(ContainerInterface $container): Resource\Index\FileIndexRepository |
||
270 | { |
||
271 | return self::new($container, Resource\Index\FileIndexRepository::class, [ |
||
272 | $container->get(EventDispatcherInterface::class) |
||
273 | ]); |
||
274 | } |
||
275 | |||
276 | public static function getMetaDataRepository(ContainerInterface $container): Resource\Index\MetaDataRepository |
||
277 | { |
||
278 | return self::new($container, Resource\Index\MetaDataRepository::class, [ |
||
279 | $container->get(EventDispatcherInterface::class) |
||
280 | ]); |
||
281 | } |
||
282 | |||
283 | public static function getDriverRegistry(ContainerInterface $container): Resource\Driver\DriverRegistry |
||
284 | { |
||
285 | return self::new($container, Resource\Driver\DriverRegistry::class); |
||
286 | } |
||
287 | |||
288 | public static function getProcessedFileRepository(ContainerInterface $container): Resource\ProcessedFileRepository |
||
289 | { |
||
290 | return self::new($container, Resource\ProcessedFileRepository::class); |
||
291 | } |
||
292 | |||
293 | public static function getResourceFactory(ContainerInterface $container): Resource\ResourceFactory |
||
294 | { |
||
295 | return self::new($container, Resource\ResourceFactory::class, [ |
||
296 | $container->get(Resource\StorageRepository::class) |
||
297 | ]); |
||
298 | } |
||
299 | |||
300 | public static function getStorageRepository(ContainerInterface $container): Resource\StorageRepository |
||
301 | { |
||
302 | return self::new($container, Resource\StorageRepository::class, [ |
||
303 | $container->get(EventDispatcherInterface::class), |
||
304 | $container->get(Resource\Driver\DriverRegistry::class), |
||
305 | ]); |
||
306 | } |
||
307 | |||
308 | public static function getDependencyOrderingService(ContainerInterface $container): Service\DependencyOrderingService |
||
309 | { |
||
310 | return new Service\DependencyOrderingService(); |
||
311 | } |
||
312 | |||
313 | public static function getFlexFormService(ContainerInterface $container): Service\FlexFormService |
||
314 | { |
||
315 | return self::new($container, Service\FlexFormService::class); |
||
316 | } |
||
317 | |||
318 | public static function getOpcodeCacheService(ContainerInterface $container): Service\OpcodeCacheService |
||
319 | { |
||
320 | return self::new($container, Service\OpcodeCacheService::class); |
||
321 | } |
||
322 | |||
323 | public static function getTimeTracker(ContainerInterface $container): TimeTracker\TimeTracker |
||
326 | } |
||
327 | |||
328 | public static function getTypoScriptConstantConfigurationParser(ContainerInterface $container): TypoScript\Parser\ConstantConfigurationParser |
||
329 | { |
||
330 | return self::new($container, TypoScript\Parser\ConstantConfigurationParser::class); |
||
331 | } |
||
332 | |||
333 | public static function getTypoScriptService(ContainerInterface $container): TypoScript\TypoScriptService |
||
334 | { |
||
335 | return self::new($container, TypoScript\TypoScriptService::class); |
||
336 | } |
||
337 | |||
338 | public static function getRequestFactory(ContainerInterface $container): Http\RequestFactory |
||
339 | { |
||
340 | return new Http\RequestFactory(); |
||
341 | } |
||
342 | |||
343 | public static function getMiddlewareStackResolver(ContainerInterface $container): Http\MiddlewareStackResolver |
||
344 | { |
||
345 | return new Http\MiddlewareStackResolver( |
||
346 | $container, |
||
347 | $container->get(Service\DependencyOrderingService::class), |
||
348 | $container->get('cache.core') |
||
349 | ); |
||
350 | } |
||
351 | |||
352 | public static function getMiddlewares(ContainerInterface $container): ArrayObject |
||
353 | { |
||
354 | return new ArrayObject(); |
||
355 | } |
||
356 | |||
357 | public static function provideFallbackEventDispatcher( |
||
364 | ); |
||
365 | } |
||
366 | |||
367 | public static function configureCommands(ContainerInterface $container, Console\CommandRegistry $commandRegistry): Console\CommandRegistry |
||
368 | { |
||
369 | $commandRegistry->addLazyCommand('list', Command\ListCommand::class, 'Lists commands'); |
||
370 | |||
371 | $commandRegistry->addLazyCommand('help', HelpCommand::class, 'Displays help for a command'); |
||
372 | |||
373 | $commandRegistry->addLazyCommand('dumpautoload', Command\DumpAutoloadCommand::class, 'Updates class loading information in non-composer mode.', Environment::isComposerMode()); |
||
374 | $commandRegistry->addLazyCommand('extensionmanager:extension:dumpclassloadinginformation', Command\DumpAutoloadCommand::class, null, Environment::isComposerMode(), false, 'dumpautoload'); |
||
375 | $commandRegistry->addLazyCommand('extension:dumpclassloadinginformation', Command\DumpAutoloadCommand::class, null, Environment::isComposerMode(), false, 'dumpautoload'); |
||
376 | |||
378 | } |
||
379 | } |
||
380 |