MediaWiki REL1_39
ConfigRepository.php
Go to the documentation of this file.
1<?php
24
25use Wikimedia\Assert\Assert;
26use Wikimedia\Services\SalvageableService;
27
33class ConfigRepository implements SalvageableService {
35 private $configFactory;
36
38 private $configItems = [
39 'private' => [],
40 'public' => [],
41 ];
42
46 public function __construct( \ConfigFactory $configFactory ) {
47 $this->configFactory = $configFactory;
48 }
49
57 public function has( $name, $alsoPrivate = false ) {
58 return isset( $this->configItems['public'][$name] ) ||
59 ( $alsoPrivate && isset( $this->configItems['private'][$name] ) );
60 }
61
70 public function get( $name ) {
71 if ( !$this->has( $name, true ) ) {
72 throw new \ConfigException( 'The configuration option ' . $name . ' does not exist.' );
73 }
74
75 return $this->configItems['public'][$name] ?? $this->configItems['private'][$name];
76 }
77
88 public function getAll() {
89 return array_merge( $this->configItems['private'], $this->configItems['public'] );
90 }
91
97 public function getPublic() {
98 return $this->configItems['public'];
99 }
100
109 public function getValueOf( $name ) {
110 $config = $this->get( $name );
111 if ( !isset( $config['configregistry'] ) ) {
112 return $config['value'];
113 }
114
115 return $this->configFactory->makeConfig( $config['configregistry'] )->get( $name );
116 }
117
126 public function getDescriptionOf( $name ) {
127 $config = $this->get( $name );
128 if ( isset( $config['descriptionmsg'] ) ) {
129 return wfMessage( $config['descriptionmsg'] )->escaped();
130 }
131 if ( isset( $config['description'] ) ) {
132 return htmlspecialchars( $config['description'] );
133 }
134 return '';
135 }
136
152 public function add( $name, array $config ) {
153 if ( $this->has( $name ) ) {
154 throw new \ConfigException( 'A configuration with the name ' . $name .
155 'does already exist. It is provided by: ' .
156 $this->get( $name )['providedby'] );
157 }
158 if ( isset( $config['public'] ) && $config['public'] ) {
159 $this->configItems['public'][$name] = $config;
160 } else {
161 $this->configItems['private'][$name] = $config;
162 }
163 }
164
172 public function isEmpty( $includePrivate = false ) {
173 if ( $includePrivate ) {
174 return empty( $this->configItems['private'] ) &&
175 empty( $this->configItems[ 'public'] );
176 }
177 return empty( $this->configItems['public'] );
178 }
179
189 public function salvage( SalvageableService $other ) {
190 Assert::parameterType( self::class, $other, '$other' );
192 '@phan-var self $other';
193
194 foreach ( $other->configItems['public'] as $name => $otherConfig ) {
195 if ( isset( $this->configItems['public'][$name] ) ) {
196 continue;
197 }
198
199 $this->add( $name, $otherConfig );
200 }
201 foreach ( $other->configItems['private'] as $name => $otherConfig ) {
202 if ( isset( $this->configItems['private'][$name] ) ) {
203 continue;
204 }
205
206 $this->add( $name, $otherConfig );
207 }
208
209 // disable $other
210 $other->configItems = [];
211 }
212}
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Factory class to create Config objects.
Object which holds currently registered configuration options.
add( $name, array $config)
Adds the definition of a configuration to this repository.
__construct(\ConfigFactory $configFactory)
getPublic()
Returns an array of all public configuration options saved in this ConfigRepository.
isEmpty( $includePrivate=false)
Returns true, if there're no elements in this instance, otherwise false.
getValueOf( $name)
Returns the current value of the configuration option.
has( $name, $alsoPrivate=false)
Returns true, if this repository contains a configuration with a specific name.
salvage(SalvageableService $other)
Re-uses existing Cache objects from $other.
getDescriptionOf( $name)
Returns the description of the given config option, This can be either a localized description,...
getAll()
Returns an array of all configuration items saved in this ConfigRepository.