Allow reset of global services.
[lhc/web/wiklou.git] / includes / ServiceWiring.php
1 <?php
2 /**
3 * Default wiring for MediaWiki services.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 *
22 * This file is loaded by MediaWiki\MediaWikiServices::getInstance() during the
23 * bootstrapping of the dependency injection framework.
24 *
25 * This file returns an array that associates service name with instantiator functions
26 * that create the default instances for the services used by MediaWiki core.
27 * For every service that MediaWiki core requires, an instantiator must be defined in
28 * this file.
29 *
30 * @note As of version 1.27, MediaWiki is only beginning to use dependency injection.
31 * The services defined here do not yet fully represent all services used by core,
32 * much of the code still relies on global state for this accessing services.
33 *
34 * @since 1.27
35 *
36 * @see docs/injection.txt for an overview of using dependency injection in the
37 * MediaWiki code base.
38 */
39
40 use MediaWiki\MediaWikiServices;
41
42 return [
43 'DBLoadBalancerFactory' => function( MediaWikiServices $services ) {
44 // NOTE: Defining the LBFactory class via LBFactoryConf is supported for
45 // backwards compatibility. The preferred way would be to register a
46 // callback for DBLoadBalancerFactory that constructs the desired LBFactory
47 // directly.
48 $config = $services->getMainConfig()->get( 'LBFactoryConf' );
49
50 $class = LBFactory::getLBFactoryClass( $config );
51 if ( !isset( $config['readOnlyReason'] ) ) {
52 // TODO: replace the global wfConfiguredReadOnlyReason() with a service.
53 $config['readOnlyReason'] = wfConfiguredReadOnlyReason();
54 }
55
56 return new $class( $config );
57 },
58
59 'DBLoadBalancer' => function( MediaWikiServices $services ) {
60 // just return the default LB from the DBLoadBalancerFactory service
61 return $services->getDBLoadBalancerFactory()->getMainLB();
62 },
63
64 'SiteStore' => function( MediaWikiServices $services ) {
65 $rawSiteStore = new DBSiteStore( $services->getDBLoadBalancer() );
66
67 // TODO: replace wfGetCache with a CacheFactory service.
68 // TODO: replace wfIsHHVM with a capabilities service.
69 $cache = wfGetCache( wfIsHHVM() ? CACHE_ACCEL : CACHE_ANYTHING );
70
71 return new CachingSiteStore( $rawSiteStore, $cache );
72 },
73
74 'SiteLookup' => function( MediaWikiServices $services ) {
75 // Use the default SiteStore as the SiteLookup implementation for now
76 return $services->getSiteStore();
77 },
78
79 'ConfigFactory' => function( MediaWikiServices $services ) {
80 // Use the bootstrap config to initialize the ConfigFactory.
81 $registry = $services->getBootstrapConfig()->get( 'ConfigRegistry' );
82 $factory = new ConfigFactory();
83
84 foreach ( $registry as $name => $callback ) {
85 $factory->register( $name, $callback );
86 }
87 return $factory;
88 },
89
90 'MainConfig' => function( MediaWikiServices $services ) {
91 // Use the 'main' config from the ConfigFactory service.
92 return $services->getConfigFactory()->makeConfig( 'main' );
93 },
94
95 ///////////////////////////////////////////////////////////////////////////
96 // NOTE: When adding a service here, don't forget to add a getter function
97 // in the MediaWikiServices class. The convenience getter should just call
98 // $this->getService( 'FooBarService' ).
99 ///////////////////////////////////////////////////////////////////////////
100
101 ];