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 |
||
| 66 | class MediaWikiServices extends ServiceContainer { |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var MediaWikiServices|null |
||
| 70 | */ |
||
| 71 | private static $instance = null; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Returns the global default instance of the top level service locator. |
||
| 75 | * |
||
| 76 | * @since 1.27 |
||
| 77 | * |
||
| 78 | * The default instance is initialized using the service instantiator functions |
||
| 79 | * defined in ServiceWiring.php. |
||
| 80 | * |
||
| 81 | * @note This should only be called by static functions! The instance returned here |
||
| 82 | * should not be passed around! Objects that need access to a service should have |
||
| 83 | * that service injected into the constructor, never a service locator! |
||
| 84 | * |
||
| 85 | * @return MediaWikiServices |
||
| 86 | */ |
||
| 87 | public static function getInstance() { |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Replaces the global MediaWikiServices instance. |
||
| 102 | * |
||
| 103 | * @since 1.28 |
||
| 104 | * |
||
| 105 | * @note This is for use in PHPUnit tests only! |
||
| 106 | * |
||
| 107 | * @throws MWException if called outside of PHPUnit tests. |
||
| 108 | * |
||
| 109 | * @param MediaWikiServices $services The new MediaWikiServices object. |
||
| 110 | * |
||
| 111 | * @return MediaWikiServices The old MediaWikiServices object, so it can be restored later. |
||
| 112 | */ |
||
| 113 | public static function forceGlobalInstance( MediaWikiServices $services ) { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Creates a new instance of MediaWikiServices and sets it as the global default |
||
| 126 | * instance. getInstance() will return a different MediaWikiServices object |
||
| 127 | * after every call to resetGlobalInstance(). |
||
| 128 | * |
||
| 129 | * @since 1.28 |
||
| 130 | * |
||
| 131 | * @warning This should not be used during normal operation. It is intended for use |
||
| 132 | * when the configuration has changed significantly since bootstrap time, e.g. |
||
| 133 | * during the installation process or during testing. |
||
| 134 | * |
||
| 135 | * @warning Calling resetGlobalInstance() may leave the application in an inconsistent |
||
| 136 | * state. Calling this is only safe under the ASSUMPTION that NO REFERENCE to |
||
| 137 | * any of the services managed by MediaWikiServices exist. If any service objects |
||
| 138 | * managed by the old MediaWikiServices instance remain in use, they may INTERFERE |
||
| 139 | * with the operation of the services managed by the new MediaWikiServices. |
||
| 140 | * Operating with a mix of services created by the old and the new |
||
| 141 | * MediaWikiServices instance may lead to INCONSISTENCIES and even DATA LOSS! |
||
| 142 | * Any class implementing LAZY LOADING is especially prone to this problem, |
||
| 143 | * since instances would typically retain a reference to a storage layer service. |
||
| 144 | * |
||
| 145 | * @see forceGlobalInstance() |
||
| 146 | * @see resetGlobalInstance() |
||
| 147 | * @see resetBetweenTest() |
||
| 148 | * |
||
| 149 | * @param Config|null $bootstrapConfig The Config object to be registered as the |
||
| 150 | * 'BootstrapConfig' service. This has to contain at least the information |
||
| 151 | * needed to set up the 'ConfigFactory' service. If not given, the bootstrap |
||
| 152 | * config of the old instance of MediaWikiServices will be re-used. If there |
||
| 153 | * was no previous instance, a new GlobalVarConfig object will be used to |
||
| 154 | * bootstrap the services. |
||
| 155 | * |
||
| 156 | * @param string $quick Set this to "quick" to allow expensive resources to be re-used. |
||
| 157 | * See SalvageableService for details. |
||
| 158 | * |
||
| 159 | * @throws MWException If called after MW_SERVICE_BOOTSTRAP_COMPLETE has been defined in |
||
| 160 | * Setup.php (unless MW_PHPUNIT_TEST or MEDIAWIKI_INSTALL or RUN_MAINTENANCE_IF_MAIN |
||
| 161 | * is defined). |
||
| 162 | */ |
||
| 163 | public static function resetGlobalInstance( Config $bootstrapConfig = null, $quick = '' ) { |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Salvages the state of any salvageable service instances in $other. |
||
| 190 | * |
||
| 191 | * @note $other will have been destroyed when salvage() returns. |
||
| 192 | * |
||
| 193 | * @param MediaWikiServices $other |
||
| 194 | */ |
||
| 195 | private function salvage( self $other ) { |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Creates a new MediaWikiServices instance and initializes it according to the |
||
| 211 | * given $bootstrapConfig. In particular, all wiring files defined in the |
||
| 212 | * ServiceWiringFiles setting are loaded, and the MediaWikiServices hook is called. |
||
| 213 | * |
||
| 214 | * @param Config|null $bootstrapConfig The Config object to be registered as the |
||
| 215 | * 'BootstrapConfig' service. |
||
| 216 | * |
||
| 217 | * @param string $loadWiring set this to 'load' to load the wiring files specified |
||
| 218 | * in the 'ServiceWiringFiles' setting in $bootstrapConfig. |
||
| 219 | * |
||
| 220 | * @return MediaWikiServices |
||
| 221 | * @throws MWException |
||
| 222 | * @throws \FatalError |
||
| 223 | */ |
||
| 224 | private static function newInstance( Config $bootstrapConfig, $loadWiring = '' ) { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Disables all storage layer services. After calling this, any attempt to access the |
||
| 241 | * storage layer will result in an error. Use resetGlobalInstance() to restore normal |
||
| 242 | * operation. |
||
| 243 | * |
||
| 244 | * @since 1.28 |
||
| 245 | * |
||
| 246 | * @warning This is intended for extreme situations only and should never be used |
||
| 247 | * while serving normal web requests. Legitimate use cases for this method include |
||
| 248 | * the installation process. Test fixtures may also use this, if the fixture relies |
||
| 249 | * on globalState. |
||
| 250 | * |
||
| 251 | * @see resetGlobalInstance() |
||
| 252 | * @see resetChildProcessServices() |
||
| 253 | */ |
||
| 254 | public static function disableStorageBackend() { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Resets any services that may have become stale after a child process |
||
| 268 | * returns from after pcntl_fork(). It's also safe, but generally unnecessary, |
||
| 269 | * to call this method from the parent process. |
||
| 270 | * |
||
| 271 | * @since 1.28 |
||
| 272 | * |
||
| 273 | * @note This is intended for use in the context of process forking only! |
||
| 274 | * |
||
| 275 | * @see resetGlobalInstance() |
||
| 276 | * @see disableStorageBackend() |
||
| 277 | */ |
||
| 278 | public static function resetChildProcessServices() { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Resets the given service for testing purposes. |
||
| 290 | * |
||
| 291 | * @since 1.28 |
||
| 292 | * |
||
| 293 | * @warning This is generally unsafe! Other services may still retain references |
||
| 294 | * to the stale service instance, leading to failures and inconsistencies. Subclasses |
||
| 295 | * may use this method to reset specific services under specific instances, but |
||
| 296 | * it should not be exposed to application logic. |
||
| 297 | * |
||
| 298 | * @note With proper dependency injection used throughout the codebase, this method |
||
| 299 | * should not be needed. It is provided to allow tests that pollute global service |
||
| 300 | * instances to clean up. |
||
| 301 | * |
||
| 302 | * @param string $name |
||
| 303 | * @param bool $destroy Whether the service instance should be destroyed if it exists. |
||
| 304 | * When set to false, any existing service instance will effectively be detached |
||
| 305 | * from the container. |
||
| 306 | * |
||
| 307 | * @throws MWException if called outside of PHPUnit tests. |
||
| 308 | */ |
||
| 309 | public function resetServiceForTesting( $name, $destroy = true ) { |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Convenience method that throws an exception unless it is called during a phase in which |
||
| 319 | * resetting of global services is allowed. In general, services should not be reset |
||
| 320 | * individually, since that may introduce inconsistencies. |
||
| 321 | * |
||
| 322 | * @since 1.28 |
||
| 323 | * |
||
| 324 | * This method will throw an exception if: |
||
| 325 | * |
||
| 326 | * - self::$resetInProgress is false (to allow all services to be reset together |
||
| 327 | * via resetGlobalInstance) |
||
| 328 | * - and MEDIAWIKI_INSTALL is not defined (to allow services to be reset during installation) |
||
| 329 | * - and MW_PHPUNIT_TEST is not defined (to allow services to be reset during testing) |
||
| 330 | * |
||
| 331 | * This method is intended to be used to safeguard against accidentally resetting |
||
| 332 | * global service instances that are not yet managed by MediaWikiServices. It is |
||
| 333 | * defined here in the MediaWikiServices services class to have a central place |
||
| 334 | * for managing service bootstrapping and resetting. |
||
| 335 | * |
||
| 336 | * @param string $method the name of the caller method, as given by __METHOD__. |
||
| 337 | * |
||
| 338 | * @throws MWException if called outside bootstrap mode. |
||
| 339 | * |
||
| 340 | * @see resetGlobalInstance() |
||
| 341 | * @see forceGlobalInstance() |
||
| 342 | * @see disableStorageBackend() |
||
| 343 | */ |
||
| 344 | public static function failIfResetNotAllowed( $method ) { |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @param Config $config The Config object to be registered as the 'BootstrapConfig' service. |
||
| 357 | * This has to contain at least the information needed to set up the 'ConfigFactory' |
||
| 358 | * service. |
||
| 359 | */ |
||
| 360 | public function __construct( Config $config ) { |
||
| 368 | |||
| 369 | // CONVENIENCE GETTERS //////////////////////////////////////////////////// |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Returns the Config object containing the bootstrap configuration. |
||
| 373 | * Bootstrap configuration would typically include database credentials |
||
| 374 | * and other information that may be needed before the ConfigFactory |
||
| 375 | * service can be instantiated. |
||
| 376 | * |
||
| 377 | * @note This should only be used during bootstrapping, in particular |
||
| 378 | * when creating the MainConfig service. Application logic should |
||
| 379 | * use getMainConfig() to get a Config instances. |
||
| 380 | * |
||
| 381 | * @since 1.27 |
||
| 382 | * @return Config |
||
| 383 | */ |
||
| 384 | public function getBootstrapConfig() { |
||
| 387 | |||
| 388 | /** |
||
| 389 | * @since 1.27 |
||
| 390 | * @return ConfigFactory |
||
| 391 | */ |
||
| 392 | public function getConfigFactory() { |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Returns the Config object that provides configuration for MediaWiki core. |
||
| 398 | * This may or may not be the same object that is returned by getBootstrapConfig(). |
||
| 399 | * |
||
| 400 | * @since 1.27 |
||
| 401 | * @return Config |
||
| 402 | */ |
||
| 403 | public function getMainConfig() { |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @since 1.27 |
||
| 409 | * @return SiteLookup |
||
| 410 | */ |
||
| 411 | public function getSiteLookup() { |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @since 1.27 |
||
| 417 | * @return SiteStore |
||
| 418 | */ |
||
| 419 | public function getSiteStore() { |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @since 1.28 |
||
| 425 | * @return InterwikiLookup |
||
| 426 | */ |
||
| 427 | public function getInterwikiLookup() { |
||
| 430 | |||
| 431 | /** |
||
| 432 | * @since 1.27 |
||
| 433 | * @return StatsdDataFactory |
||
| 434 | */ |
||
| 435 | public function getStatsdDataFactory() { |
||
| 438 | |||
| 439 | /** |
||
| 440 | * @since 1.27 |
||
| 441 | * @return EventRelayerGroup |
||
| 442 | */ |
||
| 443 | public function getEventRelayerGroup() { |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @since 1.27 |
||
| 449 | * @return SearchEngine |
||
| 450 | */ |
||
| 451 | public function newSearchEngine() { |
||
| 455 | |||
| 456 | /** |
||
| 457 | * @since 1.27 |
||
| 458 | * @return SearchEngineFactory |
||
| 459 | */ |
||
| 460 | public function getSearchEngineFactory() { |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @since 1.27 |
||
| 466 | * @return SearchEngineConfig |
||
| 467 | */ |
||
| 468 | public function getSearchEngineConfig() { |
||
| 471 | |||
| 472 | /** |
||
| 473 | * @since 1.27 |
||
| 474 | * @return SkinFactory |
||
| 475 | */ |
||
| 476 | public function getSkinFactory() { |
||
| 479 | |||
| 480 | /** |
||
| 481 | * @since 1.28 |
||
| 482 | * @return LBFactory |
||
| 483 | */ |
||
| 484 | public function getDBLoadBalancerFactory() { |
||
| 487 | |||
| 488 | /** |
||
| 489 | * @since 1.28 |
||
| 490 | * @return LoadBalancer The main DB load balancer for the local wiki. |
||
| 491 | */ |
||
| 492 | public function getDBLoadBalancer() { |
||
| 495 | |||
| 496 | /** |
||
| 497 | * @since 1.28 |
||
| 498 | * @return WatchedItemStore |
||
| 499 | */ |
||
| 500 | public function getWatchedItemStore() { |
||
| 503 | |||
| 504 | /** |
||
| 505 | * @since 1.28 |
||
| 506 | * @return GenderCache |
||
| 507 | */ |
||
| 508 | public function getGenderCache() { |
||
| 511 | |||
| 512 | /** |
||
| 513 | * @since 1.28 |
||
| 514 | * @return LinkCache |
||
| 515 | */ |
||
| 516 | public function getLinkCache() { |
||
| 519 | |||
| 520 | /** |
||
| 521 | * @since 1.28 |
||
| 522 | * @return TitleFormatter |
||
| 523 | */ |
||
| 524 | public function getTitleFormatter() { |
||
| 527 | |||
| 528 | /** |
||
| 529 | * @since 1.28 |
||
| 530 | * @return TitleParser |
||
| 531 | */ |
||
| 532 | public function getTitleParser() { |
||
| 535 | |||
| 536 | /////////////////////////////////////////////////////////////////////////// |
||
| 537 | // NOTE: When adding a service getter here, don't forget to add a test |
||
| 538 | // case for it in MediaWikiServicesTest::provideGetters() and in |
||
| 539 | // MediaWikiServicesTest::provideGetService()! |
||
| 540 | /////////////////////////////////////////////////////////////////////////// |
||
| 541 | |||
| 542 | } |
||
| 543 |