10 use Liuggio\StatsdClient\Factory\StatsdDataFactory
;
12 use MediaWiki\Services\ServiceContainer
;
14 use SearchEngineConfig
;
15 use SearchEngineFactory
;
20 * Service locator for MediaWiki core services.
22 * This program is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation; either version 2 of the License, or
25 * (at your option) any later version.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License along
33 * with this program; if not, write to the Free Software Foundation, Inc.,
34 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
35 * http://www.gnu.org/copyleft/gpl.html
43 * MediaWikiServices is the service locator for the application scope of MediaWiki.
44 * Its implemented as a simple configurable DI container.
45 * MediaWikiServices acts as a top level factory/registry for top level services, and builds
46 * the network of service objects that defines MediaWiki's application logic.
47 * It acts as an entry point to MediaWiki's dependency injection mechanism.
49 * Services are defined in the "wiring" array passed to the constructor,
50 * or by calling defineService().
52 * @see docs/injection.txt for an overview of using dependency injection in the
53 * MediaWiki code base.
55 class MediaWikiServices
extends ServiceContainer
{
58 * Returns the global default instance of the top level service locator.
60 * The default instance is initialized using the service instantiator functions
61 * defined in ServiceWiring.php.
63 * @note This should only be called by static functions! The instance returned here
64 * should not be passed around! Objects that need access to a service should have
65 * that service injected into the constructor, never a service locator!
67 * @return MediaWikiServices
69 public static function getInstance() {
70 static $instance = null;
72 if ( $instance === null ) {
73 // NOTE: constructing GlobalVarConfig here is not particularly pretty,
74 // but some information from the global scope has to be injected here,
75 // even if it's just a file name or database credentials to load
76 // configuration from.
77 $config = new GlobalVarConfig();
78 $instance = new self( $config );
80 // Load the default wiring from the specified files.
81 $wiringFiles = $config->get( 'ServiceWiringFiles' );
82 $instance->loadWiringFiles( $wiringFiles );
84 // Provide a traditional hook point to allow extensions to configure services.
85 Hooks
::run( 'MediaWikiServices', [ $instance ] );
92 * @param Config $config The Config object to be registered as the 'BootstrapConfig' service.
93 * This has to contain at least the information needed to set up the 'ConfigFactory'
96 public function __construct( Config
$config ) {
97 parent
::__construct();
99 // register the given Config object as the bootstrap config service.
100 $this->defineService( 'BootstrapConfig', function() use ( $config ) {
106 * Returns the Config object containing the bootstrap configuration.
107 * Bootstrap configuration would typically include database credentials
108 * and other information that may be needed before the ConfigFactory
109 * service can be instantiated.
111 * @note This should only be used during bootstrapping, in particular
112 * when creating the MainConfig service. Application logic should
113 * use getMainConfig() to get a Config instances.
117 public function getBootstrapConfig() {
118 return $this->getService( 'BootstrapConfig' );
122 * @return ConfigFactory
124 public function getConfigFactory() {
125 return $this->getService( 'ConfigFactory' );
129 * Returns the Config object that provides configuration for MediaWiki core.
130 * This may or may not be the same object that is returned by getBootstrapConfig().
134 public function getMainConfig() {
135 return $this->getService( 'MainConfig' );
141 public function getSiteLookup() {
142 return $this->getService( 'SiteLookup' );
148 public function getSiteStore() {
149 return $this->getService( 'SiteStore' );
153 * @return StatsdDataFactory
155 public function getStatsdDataFactory() {
156 return $this->getService( 'StatsdDataFactory' );
160 * @return EventRelayerGroup
162 public function getEventRelayerGroup() {
163 return $this->getService( 'EventRelayerGroup' );
167 * @return SearchEngine
169 public function newSearchEngine() {
170 // New engine object every time, since they keep state
171 return $this->getService( 'SearchEngineFactory' )->create();
175 * @return SearchEngineFactory
177 public function getSearchEngineFactory() {
178 return $this->getService( 'SearchEngineFactory' );
182 * @return SearchEngineConfig
184 public function getSearchEngineConfig() {
185 return $this->getService( 'SearchEngineConfig' );
188 ///////////////////////////////////////////////////////////////////////////
189 // NOTE: When adding a service getter here, don't forget to add a test
190 // case for it in MediaWikiServicesTest::provideGetters() and in
191 // MediaWikiServicesTest::provideGetService()!
192 ///////////////////////////////////////////////////////////////////////////