10 use MediaWiki\Services\ServiceContainer
;
15 * Service locator for MediaWiki core services.
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License along
28 * with this program; if not, write to the Free Software Foundation, Inc.,
29 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 * http://www.gnu.org/copyleft/gpl.html
38 * MediaWikiServices is the service locator for the application scope of MediaWiki.
39 * Its implemented as a simple configurable DI container.
40 * MediaWikiServices acts as a top level factory/registry for top level services, and builds
41 * the network of service objects that defines MediaWiki's application logic.
42 * It acts as an entry point to MediaWiki's dependency injection mechanism.
44 * Services are defined in the "wiring" array passed to the constructor,
45 * or by calling defineService().
47 * @see docs/injection.txt for an overview of using dependency injection in the
48 * MediaWiki code base.
50 class MediaWikiServices
extends ServiceContainer
{
53 * Returns the global default instance of the top level service locator.
55 * The default instance is initialized using the service instantiator functions
56 * defined in ServiceWiring.php.
58 * @note This should only be called by static functions! The instance returned here
59 * should not be passed around! Objects that need access to a service should have
60 * that service injected into the constructor, never a service locator!
62 * @return MediaWikiServices
64 public static function getInstance() {
65 static $instance = null;
67 if ( $instance === null ) {
68 // NOTE: constructing GlobalVarConfig here is not particularly pretty,
69 // but some information from the global scope has to be injected here,
70 // even if it's just a file name or database credentials to load
71 // configuration from.
72 $config = new GlobalVarConfig();
73 $instance = new self( $config );
75 // Load the default wiring from the specified files.
76 $wiringFiles = $config->get( 'ServiceWiringFiles' );
77 $instance->loadWiringFiles( $wiringFiles );
79 // Provide a traditional hook point to allow extensions to configure services.
80 Hooks
::run( 'MediaWikiServices', [ $instance ] );
87 * @param Config $config The Config object to be registered as the 'BootstrapConfig' service.
88 * This has to contain at least the information needed to set up the 'ConfigFactory'
91 public function __construct( Config
$config ) {
92 parent
::__construct();
94 // register the given Config object as the bootstrap config service.
95 $this->defineService( 'BootstrapConfig', function() use ( $config ) {
101 * Returns the Config object containing the bootstrap configuration.
102 * Bootstrap configuration would typically include database credentials
103 * and other information that may be needed before the ConfigFactory
104 * service can be instantiated.
106 * @note This should only be used during bootstrapping, in particular
107 * when creating the MainConfig service. Application logic should
108 * use getMainConfig() to get a Config instances.
112 public function getBootstrapConfig() {
113 return $this->getService( 'BootstrapConfig' );
117 * @return ConfigFactory
119 public function getConfigFactory() {
120 return $this->getService( 'ConfigFactory' );
124 * Returns the Config object that provides configuration for MediaWiki core.
125 * This may or may not be the same object that is returned by getBootstrapConfig().
129 public function getMainConfig() {
130 return $this->getService( 'MainConfig' );
136 public function getSiteLookup() {
137 return $this->getService( 'SiteLookup' );
143 public function getSiteStore() {
144 return $this->getService( 'SiteStore' );
147 ///////////////////////////////////////////////////////////////////////////
148 // NOTE: When adding a service getter here, don't forget to add a test
149 // case for it in MediaWikiServicesTest::provideGetters() and in
150 // MediaWikiServicesTest::provideGetService()!
151 ///////////////////////////////////////////////////////////////////////////