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
47 public function __construct( ConfigFactory $configFactory ) {
48 $this->configFactory = $configFactory;
49 }
50
58 public function has( $name, $alsoPrivate = false ) {
59 return isset( $this->configItems['public'][$name] ) ||
60 ( $alsoPrivate && isset( $this->configItems['private'][$name] ) );
61 }
62
71 public function get( $name ) {
72 if ( !$this->has( $name, true ) ) {
73 throw new ConfigException( 'The configuration option ' . $name . ' does not exist.' );
74 }
75
76 return $this->configItems['public'][$name] ?? $this->configItems['private'][$name];
77 }
78
89 public function getAll() {
90 return array_merge( $this->configItems['private'], $this->configItems['public'] );
91 }
92
98 public function getPublic() {
99 return $this->configItems['public'];
100 }
101
110 public function getDescriptionOf( $name ) {
111 $config = $this->get( $name );
112 if ( isset( $config['descriptionmsg'] ) ) {
113 return wfMessage( $config['descriptionmsg'] )->escaped();
114 }
115 if ( isset( $config['description'] ) ) {
116 return htmlspecialchars( $config['description'] );
117 }
118 return '';
119 }
120
136 public function add( $name, array $config ) {
137 if ( $this->has( $name ) ) {
138 throw new ConfigException( 'A configuration with the name ' . $name .
139 'does already exist. It is provided by: ' .
140 $this->get( $name )['providedby'] );
141 }
142 if ( isset( $config['public'] ) && $config['public'] ) {
143 $this->configItems['public'][$name] = $config;
144 } else {
145 $this->configItems['private'][$name] = $config;
146 }
147 }
148
156 public function isEmpty( $includePrivate = false ) {
157 if ( $includePrivate ) {
158 return empty( $this->configItems['private'] ) &&
159 empty( $this->configItems[ 'public'] );
160 }
161 return empty( $this->configItems['public'] );
162 }
163
173 public function salvage( SalvageableService $other ) {
174 Assert::parameterType( self::class, $other, '$other' );
176 '@phan-var self $other';
177
178 foreach ( $other->configItems['public'] as $name => $otherConfig ) {
179 if ( isset( $this->configItems['public'][$name] ) ) {
180 continue;
181 }
182
183 $this->add( $name, $otherConfig );
184 }
185 foreach ( $other->configItems['private'] as $name => $otherConfig ) {
186 if ( isset( $this->configItems['private'][$name] ) ) {
187 continue;
188 }
189
190 $this->add( $name, $otherConfig );
191 }
192
193 // disable $other
194 $other->configItems = [];
195 }
196}
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.