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