MediaWiki master
ConfigRepository.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Config;
24
25use Wikimedia\Assert\Assert;
26use Wikimedia\Services\SalvageableService;
27
34class ConfigRepository implements SalvageableService {
36 private $configFactory;
37
39 private $configItems = [
40 'private' => [],
41 'public' => [],
42 ];
43
44 public function __construct( ConfigFactory $configFactory ) {
45 $this->configFactory = $configFactory;
46 }
47
55 public function has( $name, $alsoPrivate = false ) {
56 return isset( $this->configItems['public'][$name] ) ||
57 ( $alsoPrivate && isset( $this->configItems['private'][$name] ) );
58 }
59
68 public function get( $name ) {
69 if ( !$this->has( $name, true ) ) {
70 throw new ConfigException( 'The configuration option ' . $name . ' does not exist.' );
71 }
72
73 return $this->configItems['public'][$name] ?? $this->configItems['private'][$name];
74 }
75
86 public function getAll() {
87 return array_merge( $this->configItems['private'], $this->configItems['public'] );
88 }
89
95 public function getPublic() {
96 return $this->configItems['public'];
97 }
98
107 public function getDescriptionOf( $name ) {
108 $config = $this->get( $name );
109 if ( isset( $config['descriptionmsg'] ) ) {
110 return wfMessage( $config['descriptionmsg'] )->escaped();
111 }
112 if ( isset( $config['description'] ) ) {
113 return htmlspecialchars( $config['description'] );
114 }
115 return '';
116 }
117
133 public function add( $name, array $config ) {
134 if ( $this->has( $name ) ) {
135 throw new ConfigException( 'A configuration with the name ' . $name .
136 'does already exist. It is provided by: ' .
137 $this->get( $name )['providedby'] );
138 }
139 if ( isset( $config['public'] ) && $config['public'] ) {
140 $this->configItems['public'][$name] = $config;
141 } else {
142 $this->configItems['private'][$name] = $config;
143 }
144 }
145
153 public function isEmpty( $includePrivate = false ) {
154 if ( $includePrivate ) {
155 return empty( $this->configItems['private'] ) &&
156 empty( $this->configItems[ 'public'] );
157 }
158 return empty( $this->configItems['public'] );
159 }
160
170 public function salvage( SalvageableService $other ) {
171 Assert::parameterType( self::class, $other, '$other' );
173 '@phan-var self $other';
174
175 foreach ( $other->configItems['public'] as $name => $otherConfig ) {
176 if ( isset( $this->configItems['public'][$name] ) ) {
177 continue;
178 }
179
180 $this->add( $name, $otherConfig );
181 }
182 foreach ( $other->configItems['private'] as $name => $otherConfig ) {
183 if ( isset( $this->configItems['private'][$name] ) ) {
184 continue;
185 }
186
187 $this->add( $name, $otherConfig );
188 }
189
190 // disable $other
191 $other->configItems = [];
192 }
193}
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Exceptions for config failures.
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.
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.
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.
__construct(ConfigFactory $configFactory)
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.