Completed
Branch master (d58858)
by
unknown
28:23
created

MediaWikiServices::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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