10 use Liuggio\StatsdClient\Factory\StatsdDataFactory
;
12 use MediaWiki\Services\ServiceContainer
;
18 * Service locator for MediaWiki core services.
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License along
31 * with this program; if not, write to the Free Software Foundation, Inc.,
32 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
33 * http://www.gnu.org/copyleft/gpl.html
41 * MediaWikiServices is the service locator for the application scope of MediaWiki.
42 * Its implemented as a simple configurable DI container.
43 * MediaWikiServices acts as a top level factory/registry for top level services, and builds
44 * the network of service objects that defines MediaWiki's application logic.
45 * It acts as an entry point to MediaWiki's dependency injection mechanism.
47 * Services are defined in the "wiring" array passed to the constructor,
48 * or by calling defineService().
50 * @see docs/injection.txt for an overview of using dependency injection in the
51 * MediaWiki code base.
53 class MediaWikiServices
extends ServiceContainer
{
56 * Returns the global default instance of the top level service locator.
58 * The default instance is initialized using the service instantiator functions
59 * defined in ServiceWiring.php.
61 * @note This should only be called by static functions! The instance returned here
62 * should not be passed around! Objects that need access to a service should have
63 * that service injected into the constructor, never a service locator!
65 * @return MediaWikiServices
67 public static function getInstance() {
68 static $instance = null;
70 if ( $instance === null ) {
71 // NOTE: constructing GlobalVarConfig here is not particularly pretty,
72 // but some information from the global scope has to be injected here,
73 // even if it's just a file name or database credentials to load
74 // configuration from.
75 $config = new GlobalVarConfig();
76 $instance = new self( $config );
78 // Load the default wiring from the specified files.
79 $wiringFiles = $config->get( 'ServiceWiringFiles' );
80 $instance->loadWiringFiles( $wiringFiles );
82 // Provide a traditional hook point to allow extensions to configure services.
83 Hooks
::run( 'MediaWikiServices', [ $instance ] );
90 * @param Config $config The Config object to be registered as the 'BootstrapConfig' service.
91 * This has to contain at least the information needed to set up the 'ConfigFactory'
94 public function __construct( Config
$config ) {
95 parent
::__construct();
97 // register the given Config object as the bootstrap config service.
98 $this->defineService( 'BootstrapConfig', function() use ( $config ) {
104 * Returns the Config object containing the bootstrap configuration.
105 * Bootstrap configuration would typically include database credentials
106 * and other information that may be needed before the ConfigFactory
107 * service can be instantiated.
109 * @note This should only be used during bootstrapping, in particular
110 * when creating the MainConfig service. Application logic should
111 * use getMainConfig() to get a Config instances.
115 public function getBootstrapConfig() {
116 return $this->getService( 'BootstrapConfig' );
120 * @return ConfigFactory
122 public function getConfigFactory() {
123 return $this->getService( 'ConfigFactory' );
127 * Returns the Config object that provides configuration for MediaWiki core.
128 * This may or may not be the same object that is returned by getBootstrapConfig().
132 public function getMainConfig() {
133 return $this->getService( 'MainConfig' );
139 public function getSiteLookup() {
140 return $this->getService( 'SiteLookup' );
146 public function getSiteStore() {
147 return $this->getService( 'SiteStore' );
151 * @return StatsdDataFactory
153 public function getStatsdDataFactory() {
154 return $this->getService( 'StatsdDataFactory' );
158 * @return EventRelayerGroup
160 public function getEventRelayerGroup() {
161 return $this->getService( 'EventRelayerGroup' );
165 * @return SearchEngine
167 public function newSearchEngine() {
168 // New engine object every time, since they keep state
169 return $this->getService( 'SearchEngineFactory' )->create();
173 * @return SearchEngineFactory
175 public function getSearchEngineFactory() {
176 return $this->getService( 'SearchEngineFactory' );
180 * @return SearchEngineConfig
182 public function getSearchEngineConfig() {
183 return $this->getService( 'SearchEngineConfig' );
187 * @return SkinFactory
189 public function getSkinFactory() {
190 return $this->getService( 'SkinFactory' );
193 ///////////////////////////////////////////////////////////////////////////
194 // NOTE: When adding a service getter here, don't forget to add a test
195 // case for it in MediaWikiServicesTest::provideGetters() and in
196 // MediaWikiServicesTest::provideGetService()!
197 ///////////////////////////////////////////////////////////////////////////