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