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