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