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