MediaWiki REL1_27
ServiceContainer.php
Go to the documentation of this file.
1<?php
3
4use InvalidArgumentException;
5use RuntimeException;
6use Wikimedia\Assert\Assert;
7
47
51 private $services = [];
52
57
62
69 $this->extraInstantiationParams = $extraInstantiationParams;
70 }
71
77 public function loadWiringFiles( array $wiringFiles ) {
78 foreach ( $wiringFiles as $file ) {
79 // the wiring file is required to return an array of instantiators.
80 $wiring = require $file;
81
82 Assert::postcondition(
83 is_array( $wiring ),
84 "Wiring file $file is expected to return an array!"
85 );
86
87 $this->applyWiring( $wiring );
88 }
89 }
90
98 Assert::parameterElementType( 'callable', $serviceInstantiators, '$serviceInstantiators' );
99
100 foreach ( $serviceInstantiators as $name => $instantiator ) {
101 $this->defineService( $name, $instantiator );
102 }
103 }
104
113 public function hasService( $name ) {
114 return isset( $this->serviceInstantiators[$name] );
115 }
116
120 public function getServiceNames() {
121 return array_keys( $this->serviceInstantiators );
122 }
123
138 public function defineService( $name, callable $instantiator ) {
139 Assert::parameterType( 'string', $name, '$name' );
140
141 if ( $this->hasService( $name ) ) {
142 throw new RuntimeException( 'Service already defined: ' . $name );
143 }
144
145 $this->serviceInstantiators[$name] = $instantiator;
146 }
147
164 public function redefineService( $name, callable $instantiator ) {
165 Assert::parameterType( 'string', $name, '$name' );
166
167 if ( !$this->hasService( $name ) ) {
168 throw new RuntimeException( 'Service not defined: ' . $name );
169 }
170
171 if ( isset( $this->services[$name] ) ) {
172 throw new RuntimeException( 'Cannot redefine a service that is already in use: ' . $name );
173 }
174
175 $this->serviceInstantiators[$name] = $instantiator;
176 }
177
195 public function getService( $name ) {
196 if ( !isset( $this->services[$name] ) ) {
197 $this->services[$name] = $this->createService( $name );
198 }
199
200 return $this->services[$name];
201 }
202
209 private function createService( $name ) {
210 if ( isset( $this->serviceInstantiators[$name] ) ) {
211 $service = call_user_func_array(
212 $this->serviceInstantiators[$name],
213 array_merge( [ $this ], $this->extraInstantiationParams )
214 );
215 } else {
216 throw new InvalidArgumentException( 'Unknown service: ' . $name );
217 }
218
219 return $service;
220 }
221
222}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
ServiceContainer provides a generic service to manage named services using lazy instantiation based o...
redefineService( $name, callable $instantiator)
Replace an already defined service.
getService( $name)
Returns a service object of the kind associated with $name.
applyWiring(array $serviceInstantiators)
Registers multiple services (aka a "wiring").
defineService( $name, callable $instantiator)
Define a new service.
hasService( $name)
Returns true if a service is defined for $name, that is, if a call to getService( $name ) would retur...
__construct(array $extraInstantiationParams=[])
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
the array() calling protocol came about after MediaWiki 1.4rc1.
Allows to change the fields on the form that will be generated $name
Definition hooks.txt:314
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37