1f3d81c22eea5c261347f49493caa50e6af28930
[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 Liuggio\StatsdClient\Factory\StatsdDataFactory;
10 use MediaWiki\Services\ServiceContainer;
11 use SearchEngine;
12 use SearchEngineConfig;
13 use SearchEngineFactory;
14 use SiteLookup;
15 use SiteStore;
16 use SkinFactory;
17
18 /**
19 * Service locator for MediaWiki core services.
20 *
21 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation; either version 2 of the License, or
24 * (at your option) any later version.
25 *
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
30 *
31 * You should have received a copy of the GNU General Public License along
32 * with this program; if not, write to the Free Software Foundation, Inc.,
33 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
34 * http://www.gnu.org/copyleft/gpl.html
35 *
36 * @file
37 *
38 * @since 1.27
39 */
40
41 /**
42 * MediaWikiServices is the service locator for the application scope of MediaWiki.
43 * Its implemented as a simple configurable DI container.
44 * MediaWikiServices acts as a top level factory/registry for top level services, and builds
45 * the network of service objects that defines MediaWiki's application logic.
46 * It acts as an entry point to MediaWiki's dependency injection mechanism.
47 *
48 * Services are defined in the "wiring" array passed to the constructor,
49 * or by calling defineService().
50 *
51 * @see docs/injection.txt for an overview of using dependency injection in the
52 * MediaWiki code base.
53 */
54 class MediaWikiServices extends ServiceContainer {
55
56 /**
57 * Returns the global default instance of the top level service locator.
58 *
59 * The default instance is initialized using the service instantiator functions
60 * defined in ServiceWiring.php.
61 *
62 * @note This should only be called by static functions! The instance returned here
63 * should not be passed around! Objects that need access to a service should have
64 * that service injected into the constructor, never a service locator!
65 *
66 * @return MediaWikiServices
67 */
68 public static function getInstance() {
69 static $instance = null;
70
71 if ( $instance === null ) {
72 // NOTE: constructing GlobalVarConfig here is not particularly pretty,
73 // but some information from the global scope has to be injected here,
74 // even if it's just a file name or database credentials to load
75 // configuration from.
76 $config = new GlobalVarConfig();
77 $instance = new self( $config );
78
79 // Load the default wiring from the specified files.
80 $wiringFiles = $config->get( 'ServiceWiringFiles' );
81 $instance->loadWiringFiles( $wiringFiles );
82
83 // Provide a traditional hook point to allow extensions to configure services.
84 Hooks::run( 'MediaWikiServices', [ $instance ] );
85 }
86
87 return $instance;
88 }
89
90 /**
91 * @param Config $config The Config object to be registered as the 'BootstrapConfig' service.
92 * This has to contain at least the information needed to set up the 'ConfigFactory'
93 * service.
94 */
95 public function __construct( Config $config ) {
96 parent::__construct();
97
98 // register the given Config object as the bootstrap config service.
99 $this->defineService( 'BootstrapConfig', function() use ( $config ) {
100 return $config;
101 } );
102 }
103
104 /**
105 * Returns the Config object containing the bootstrap configuration.
106 * Bootstrap configuration would typically include database credentials
107 * and other information that may be needed before the ConfigFactory
108 * service can be instantiated.
109 *
110 * @note This should only be used during bootstrapping, in particular
111 * when creating the MainConfig service. Application logic should
112 * use getMainConfig() to get a Config instances.
113 *
114 * @return Config
115 */
116 public function getBootstrapConfig() {
117 return $this->getService( 'BootstrapConfig' );
118 }
119
120 /**
121 * @return ConfigFactory
122 */
123 public function getConfigFactory() {
124 return $this->getService( 'ConfigFactory' );
125 }
126
127 /**
128 * Returns the Config object that provides configuration for MediaWiki core.
129 * This may or may not be the same object that is returned by getBootstrapConfig().
130 *
131 * @return Config
132 */
133 public function getMainConfig() {
134 return $this->getService( 'MainConfig' );
135 }
136
137 /**
138 * @return SiteLookup
139 */
140 public function getSiteLookup() {
141 return $this->getService( 'SiteLookup' );
142 }
143
144 /**
145 * @return SiteStore
146 */
147 public function getSiteStore() {
148 return $this->getService( 'SiteStore' );
149 }
150
151 /**
152 * @return StatsdDataFactory
153 */
154 public function getStatsdDataFactory() {
155 return $this->getService( 'StatsdDataFactory' );
156 }
157
158 /**
159 * @return EventRelayerGroup
160 */
161 public function getEventRelayerGroup() {
162 return $this->getService( 'EventRelayerGroup' );
163 }
164
165 /**
166 * @return SearchEngine
167 */
168 public function newSearchEngine() {
169 // New engine object every time, since they keep state
170 return $this->getService( 'SearchEngineFactory' )->create();
171 }
172
173 /**
174 * @return SearchEngineFactory
175 */
176 public function getSearchEngineFactory() {
177 return $this->getService( 'SearchEngineFactory' );
178 }
179
180 /**
181 * @return SearchEngineConfig
182 */
183 public function getSearchEngineConfig() {
184 return $this->getService( 'SearchEngineConfig' );
185 }
186
187 /**
188 * @return SkinFactory
189 */
190 public function getSkinFactory() {
191 return $this->getService( 'SkinFactory' );
192 }
193
194 ///////////////////////////////////////////////////////////////////////////
195 // NOTE: When adding a service getter here, don't forget to add a test
196 // case for it in MediaWikiServicesTest::provideGetters() and in
197 // MediaWikiServicesTest::provideGetService()!
198 ///////////////////////////////////////////////////////////////////////////
199
200 }