Complex classes like MediaWikiServices 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 MediaWikiServices, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 70 | class MediaWikiServices extends ServiceContainer { |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var MediaWikiServices|null |
||
| 74 | */ |
||
| 75 | private static $instance = null; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Returns the global default instance of the top level service locator. |
||
| 79 | * |
||
| 80 | * @since 1.27 |
||
| 81 | * |
||
| 82 | * The default instance is initialized using the service instantiator functions |
||
| 83 | * defined in ServiceWiring.php. |
||
| 84 | * |
||
| 85 | * @note This should only be called by static functions! The instance returned here |
||
| 86 | * should not be passed around! Objects that need access to a service should have |
||
| 87 | * that service injected into the constructor, never a service locator! |
||
| 88 | * |
||
| 89 | * @return MediaWikiServices |
||
| 90 | */ |
||
| 91 | public static function getInstance() { |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Replaces the global MediaWikiServices instance. |
||
| 106 | * |
||
| 107 | * @since 1.28 |
||
| 108 | * |
||
| 109 | * @note This is for use in PHPUnit tests only! |
||
| 110 | * |
||
| 111 | * @throws MWException if called outside of PHPUnit tests. |
||
| 112 | * |
||
| 113 | * @param MediaWikiServices $services The new MediaWikiServices object. |
||
| 114 | * |
||
| 115 | * @return MediaWikiServices The old MediaWikiServices object, so it can be restored later. |
||
| 116 | */ |
||
| 117 | public static function forceGlobalInstance( MediaWikiServices $services ) { |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Creates a new instance of MediaWikiServices and sets it as the global default |
||
| 130 | * instance. getInstance() will return a different MediaWikiServices object |
||
| 131 | * after every call to resetGlobalInstance(). |
||
| 132 | * |
||
| 133 | * @since 1.28 |
||
| 134 | * |
||
| 135 | * @warning This should not be used during normal operation. It is intended for use |
||
| 136 | * when the configuration has changed significantly since bootstrap time, e.g. |
||
| 137 | * during the installation process or during testing. |
||
| 138 | * |
||
| 139 | * @warning Calling resetGlobalInstance() may leave the application in an inconsistent |
||
| 140 | * state. Calling this is only safe under the ASSUMPTION that NO REFERENCE to |
||
| 141 | * any of the services managed by MediaWikiServices exist. If any service objects |
||
| 142 | * managed by the old MediaWikiServices instance remain in use, they may INTERFERE |
||
| 143 | * with the operation of the services managed by the new MediaWikiServices. |
||
| 144 | * Operating with a mix of services created by the old and the new |
||
| 145 | * MediaWikiServices instance may lead to INCONSISTENCIES and even DATA LOSS! |
||
| 146 | * Any class implementing LAZY LOADING is especially prone to this problem, |
||
| 147 | * since instances would typically retain a reference to a storage layer service. |
||
| 148 | * |
||
| 149 | * @see forceGlobalInstance() |
||
| 150 | * @see resetGlobalInstance() |
||
| 151 | * @see resetBetweenTest() |
||
| 152 | * |
||
| 153 | * @param Config|null $bootstrapConfig The Config object to be registered as the |
||
| 154 | * 'BootstrapConfig' service. This has to contain at least the information |
||
| 155 | * needed to set up the 'ConfigFactory' service. If not given, the bootstrap |
||
| 156 | * config of the old instance of MediaWikiServices will be re-used. If there |
||
| 157 | * was no previous instance, a new GlobalVarConfig object will be used to |
||
| 158 | * bootstrap the services. |
||
| 159 | * |
||
| 160 | * @param string $quick Set this to "quick" to allow expensive resources to be re-used. |
||
| 161 | * See SalvageableService for details. |
||
| 162 | * |
||
| 163 | * @throws MWException If called after MW_SERVICE_BOOTSTRAP_COMPLETE has been defined in |
||
| 164 | * Setup.php (unless MW_PHPUNIT_TEST or MEDIAWIKI_INSTALL or RUN_MAINTENANCE_IF_MAIN |
||
| 165 | * is defined). |
||
| 166 | */ |
||
| 167 | public static function resetGlobalInstance( Config $bootstrapConfig = null, $quick = '' ) { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Salvages the state of any salvageable service instances in $other. |
||
| 194 | * |
||
| 195 | * @note $other will have been destroyed when salvage() returns. |
||
| 196 | * |
||
| 197 | * @param MediaWikiServices $other |
||
| 198 | */ |
||
| 199 | private function salvage( self $other ) { |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Creates a new MediaWikiServices instance and initializes it according to the |
||
| 215 | * given $bootstrapConfig. In particular, all wiring files defined in the |
||
| 216 | * ServiceWiringFiles setting are loaded, and the MediaWikiServices hook is called. |
||
| 217 | * |
||
| 218 | * @param Config|null $bootstrapConfig The Config object to be registered as the |
||
| 219 | * 'BootstrapConfig' service. |
||
| 220 | * |
||
| 221 | * @param string $loadWiring set this to 'load' to load the wiring files specified |
||
| 222 | * in the 'ServiceWiringFiles' setting in $bootstrapConfig. |
||
| 223 | * |
||
| 224 | * @return MediaWikiServices |
||
| 225 | * @throws MWException |
||
| 226 | * @throws \FatalError |
||
| 227 | */ |
||
| 228 | private static function newInstance( Config $bootstrapConfig, $loadWiring = '' ) { |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Disables all storage layer services. After calling this, any attempt to access the |
||
| 245 | * storage layer will result in an error. Use resetGlobalInstance() to restore normal |
||
| 246 | * operation. |
||
| 247 | * |
||
| 248 | * @since 1.28 |
||
| 249 | * |
||
| 250 | * @warning This is intended for extreme situations only and should never be used |
||
| 251 | * while serving normal web requests. Legitimate use cases for this method include |
||
| 252 | * the installation process. Test fixtures may also use this, if the fixture relies |
||
| 253 | * on globalState. |
||
| 254 | * |
||
| 255 | * @see resetGlobalInstance() |
||
| 256 | * @see resetChildProcessServices() |
||
| 257 | */ |
||
| 258 | public static function disableStorageBackend() { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Resets any services that may have become stale after a child process |
||
| 272 | * returns from after pcntl_fork(). It's also safe, but generally unnecessary, |
||
| 273 | * to call this method from the parent process. |
||
| 274 | * |
||
| 275 | * @since 1.28 |
||
| 276 | * |
||
| 277 | * @note This is intended for use in the context of process forking only! |
||
| 278 | * |
||
| 279 | * @see resetGlobalInstance() |
||
| 280 | * @see disableStorageBackend() |
||
| 281 | */ |
||
| 282 | public static function resetChildProcessServices() { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Resets the given service for testing purposes. |
||
| 294 | * |
||
| 295 | * @since 1.28 |
||
| 296 | * |
||
| 297 | * @warning This is generally unsafe! Other services may still retain references |
||
| 298 | * to the stale service instance, leading to failures and inconsistencies. Subclasses |
||
| 299 | * may use this method to reset specific services under specific instances, but |
||
| 300 | * it should not be exposed to application logic. |
||
| 301 | * |
||
| 302 | * @note With proper dependency injection used throughout the codebase, this method |
||
| 303 | * should not be needed. It is provided to allow tests that pollute global service |
||
| 304 | * instances to clean up. |
||
| 305 | * |
||
| 306 | * @param string $name |
||
| 307 | * @param bool $destroy Whether the service instance should be destroyed if it exists. |
||
| 308 | * When set to false, any existing service instance will effectively be detached |
||
| 309 | * from the container. |
||
| 310 | * |
||
| 311 | * @throws MWException if called outside of PHPUnit tests. |
||
| 312 | */ |
||
| 313 | public function resetServiceForTesting( $name, $destroy = true ) { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Convenience method that throws an exception unless it is called during a phase in which |
||
| 323 | * resetting of global services is allowed. In general, services should not be reset |
||
| 324 | * individually, since that may introduce inconsistencies. |
||
| 325 | * |
||
| 326 | * @since 1.28 |
||
| 327 | * |
||
| 328 | * This method will throw an exception if: |
||
| 329 | * |
||
| 330 | * - self::$resetInProgress is false (to allow all services to be reset together |
||
| 331 | * via resetGlobalInstance) |
||
| 332 | * - and MEDIAWIKI_INSTALL is not defined (to allow services to be reset during installation) |
||
| 333 | * - and MW_PHPUNIT_TEST is not defined (to allow services to be reset during testing) |
||
| 334 | * |
||
| 335 | * This method is intended to be used to safeguard against accidentally resetting |
||
| 336 | * global service instances that are not yet managed by MediaWikiServices. It is |
||
| 337 | * defined here in the MediaWikiServices services class to have a central place |
||
| 338 | * for managing service bootstrapping and resetting. |
||
| 339 | * |
||
| 340 | * @param string $method the name of the caller method, as given by __METHOD__. |
||
| 341 | * |
||
| 342 | * @throws MWException if called outside bootstrap mode. |
||
| 343 | * |
||
| 344 | * @see resetGlobalInstance() |
||
| 345 | * @see forceGlobalInstance() |
||
| 346 | * @see disableStorageBackend() |
||
| 347 | */ |
||
| 348 | public static function failIfResetNotAllowed( $method ) { |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @param Config $config The Config object to be registered as the 'BootstrapConfig' service. |
||
| 361 | * This has to contain at least the information needed to set up the 'ConfigFactory' |
||
| 362 | * service. |
||
| 363 | */ |
||
| 364 | public function __construct( Config $config ) { |
||
| 372 | |||
| 373 | // CONVENIENCE GETTERS //////////////////////////////////////////////////// |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Returns the Config object containing the bootstrap configuration. |
||
| 377 | * Bootstrap configuration would typically include database credentials |
||
| 378 | * and other information that may be needed before the ConfigFactory |
||
| 379 | * service can be instantiated. |
||
| 380 | * |
||
| 381 | * @note This should only be used during bootstrapping, in particular |
||
| 382 | * when creating the MainConfig service. Application logic should |
||
| 383 | * use getMainConfig() to get a Config instances. |
||
| 384 | * |
||
| 385 | * @since 1.27 |
||
| 386 | * @return Config |
||
| 387 | */ |
||
| 388 | public function getBootstrapConfig() { |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @since 1.27 |
||
| 394 | * @return ConfigFactory |
||
| 395 | */ |
||
| 396 | public function getConfigFactory() { |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Returns the Config object that provides configuration for MediaWiki core. |
||
| 402 | * This may or may not be the same object that is returned by getBootstrapConfig(). |
||
| 403 | * |
||
| 404 | * @since 1.27 |
||
| 405 | * @return Config |
||
| 406 | */ |
||
| 407 | public function getMainConfig() { |
||
| 410 | |||
| 411 | /** |
||
| 412 | * @since 1.27 |
||
| 413 | * @return SiteLookup |
||
| 414 | */ |
||
| 415 | public function getSiteLookup() { |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @since 1.27 |
||
| 421 | * @return SiteStore |
||
| 422 | */ |
||
| 423 | public function getSiteStore() { |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @since 1.28 |
||
| 429 | * @return InterwikiLookup |
||
| 430 | */ |
||
| 431 | public function getInterwikiLookup() { |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @since 1.27 |
||
| 437 | * @return StatsdDataFactory |
||
| 438 | */ |
||
| 439 | public function getStatsdDataFactory() { |
||
| 442 | |||
| 443 | /** |
||
| 444 | * @since 1.27 |
||
| 445 | * @return EventRelayerGroup |
||
| 446 | */ |
||
| 447 | public function getEventRelayerGroup() { |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @since 1.27 |
||
| 453 | * @return SearchEngine |
||
| 454 | */ |
||
| 455 | public function newSearchEngine() { |
||
| 459 | |||
| 460 | /** |
||
| 461 | * @since 1.27 |
||
| 462 | * @return SearchEngineFactory |
||
| 463 | */ |
||
| 464 | public function getSearchEngineFactory() { |
||
| 467 | |||
| 468 | /** |
||
| 469 | * @since 1.27 |
||
| 470 | * @return SearchEngineConfig |
||
| 471 | */ |
||
| 472 | public function getSearchEngineConfig() { |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @since 1.27 |
||
| 478 | * @return SkinFactory |
||
| 479 | */ |
||
| 480 | public function getSkinFactory() { |
||
| 483 | |||
| 484 | /** |
||
| 485 | * @since 1.28 |
||
| 486 | * @return LBFactory |
||
| 487 | */ |
||
| 488 | public function getDBLoadBalancerFactory() { |
||
| 491 | |||
| 492 | /** |
||
| 493 | * @since 1.28 |
||
| 494 | * @return LoadBalancer The main DB load balancer for the local wiki. |
||
| 495 | */ |
||
| 496 | public function getDBLoadBalancer() { |
||
| 499 | |||
| 500 | /** |
||
| 501 | * @since 1.28 |
||
| 502 | * @return WatchedItemStore |
||
| 503 | */ |
||
| 504 | public function getWatchedItemStore() { |
||
| 507 | |||
| 508 | /** |
||
| 509 | * @since 1.28 |
||
| 510 | * @return WatchedItemQueryService |
||
| 511 | */ |
||
| 512 | public function getWatchedItemQueryService() { |
||
| 515 | |||
| 516 | /** |
||
| 517 | * @since 1.28 |
||
| 518 | * @return MediaHandlerFactory |
||
| 519 | */ |
||
| 520 | public function getMediaHandlerFactory() { |
||
| 523 | |||
| 524 | /** |
||
| 525 | * @since 1.28 |
||
| 526 | * @return GenderCache |
||
| 527 | */ |
||
| 528 | public function getGenderCache() { |
||
| 531 | |||
| 532 | /** |
||
| 533 | * @since 1.28 |
||
| 534 | * @return LinkCache |
||
| 535 | */ |
||
| 536 | public function getLinkCache() { |
||
| 539 | |||
| 540 | /** |
||
| 541 | * @since 1.28 |
||
| 542 | * @return LinkRendererFactory |
||
| 543 | */ |
||
| 544 | public function getLinkRendererFactory() { |
||
| 547 | |||
| 548 | /** |
||
| 549 | * LinkRenderer instance that can be used |
||
| 550 | * if no custom options are needed |
||
| 551 | * |
||
| 552 | * @since 1.28 |
||
| 553 | * @return LinkRenderer |
||
| 554 | */ |
||
| 555 | public function getLinkRenderer() { |
||
| 558 | |||
| 559 | /** |
||
| 560 | * @since 1.28 |
||
| 561 | * @return TitleFormatter |
||
| 562 | */ |
||
| 563 | public function getTitleFormatter() { |
||
| 566 | |||
| 567 | /** |
||
| 568 | * @since 1.28 |
||
| 569 | * @return TitleParser |
||
| 570 | */ |
||
| 571 | public function getTitleParser() { |
||
| 574 | |||
| 575 | /** |
||
| 576 | * @since 1.28 |
||
| 577 | * @return VirtualRESTServiceClient |
||
| 578 | */ |
||
| 579 | public function getVirtualRESTServiceClient() { |
||
| 582 | |||
| 583 | /////////////////////////////////////////////////////////////////////////// |
||
| 584 | // NOTE: When adding a service getter here, don't forget to add a test |
||
| 585 | // case for it in MediaWikiServicesTest::provideGetters() and in |
||
| 586 | // MediaWikiServicesTest::provideGetService()! |
||
| 587 | /////////////////////////////////////////////////////////////////////////// |
||
| 588 | |||
| 589 | } |
||
| 590 |