Add missing imports to MediaWikiServices
[lhc/web/wiklou.git] / includes / MediaWikiServices.php
1 <?php
2 namespace MediaWiki;
3
4 use ConfigFactory;
5 use EventRelayerGroup;
6 use GlobalVarConfig;
7 use Config;
8 use Hooks;
9 use LBFactory;
10 use Liuggio\StatsdClient\Factory\StatsdDataFactory;
11 use LoadBalancer;
12 use MediaWiki\Services\ServiceContainer;
13 use SearchEngine;
14 use SearchEngineConfig;
15 use SearchEngineFactory;
16 use SiteLookup;
17 use SiteStore;
18
19 /**
20 * Service locator for MediaWiki core services.
21 *
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.
26 *
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.
31 *
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
36 *
37 * @file
38 *
39 * @since 1.27
40 */
41
42 /**
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.
48 *
49 * Services are defined in the "wiring" array passed to the constructor,
50 * or by calling defineService().
51 *
52 * @see docs/injection.txt for an overview of using dependency injection in the
53 * MediaWiki code base.
54 */
55 class MediaWikiServices extends ServiceContainer {
56
57 /**
58 * Returns the global default instance of the top level service locator.
59 *
60 * The default instance is initialized using the service instantiator functions
61 * defined in ServiceWiring.php.
62 *
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!
66 *
67 * @return MediaWikiServices
68 */
69 public static function getInstance() {
70 static $instance = null;
71
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 );
79
80 // Load the default wiring from the specified files.
81 $wiringFiles = $config->get( 'ServiceWiringFiles' );
82 $instance->loadWiringFiles( $wiringFiles );
83
84 // Provide a traditional hook point to allow extensions to configure services.
85 Hooks::run( 'MediaWikiServices', [ $instance ] );
86 }
87
88 return $instance;
89 }
90
91 /**
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'
94 * service.
95 */
96 public function __construct( Config $config ) {
97 parent::__construct();
98
99 // register the given Config object as the bootstrap config service.
100 $this->defineService( 'BootstrapConfig', function() use ( $config ) {
101 return $config;
102 } );
103 }
104
105 /**
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.
110 *
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.
114 *
115 * @return Config
116 */
117 public function getBootstrapConfig() {
118 return $this->getService( 'BootstrapConfig' );
119 }
120
121 /**
122 * @return ConfigFactory
123 */
124 public function getConfigFactory() {
125 return $this->getService( 'ConfigFactory' );
126 }
127
128 /**
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().
131 *
132 * @return Config
133 */
134 public function getMainConfig() {
135 return $this->getService( 'MainConfig' );
136 }
137
138 /**
139 * @return SiteLookup
140 */
141 public function getSiteLookup() {
142 return $this->getService( 'SiteLookup' );
143 }
144
145 /**
146 * @return SiteStore
147 */
148 public function getSiteStore() {
149 return $this->getService( 'SiteStore' );
150 }
151
152 /**
153 * @return StatsdDataFactory
154 */
155 public function getStatsdDataFactory() {
156 return $this->getService( 'StatsdDataFactory' );
157 }
158
159 /**
160 * @return EventRelayerGroup
161 */
162 public function getEventRelayerGroup() {
163 return $this->getService( 'EventRelayerGroup' );
164 }
165
166 /**
167 * @return SearchEngine
168 */
169 public function newSearchEngine() {
170 // New engine object every time, since they keep state
171 return $this->getService( 'SearchEngineFactory' )->create();
172 }
173
174 /**
175 * @return SearchEngineFactory
176 */
177 public function getSearchEngineFactory() {
178 return $this->getService( 'SearchEngineFactory' );
179 }
180
181 /**
182 * @return SearchEngineConfig
183 */
184 public function getSearchEngineConfig() {
185 return $this->getService( 'SearchEngineConfig' );
186 }
187
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 ///////////////////////////////////////////////////////////////////////////
193
194 }