MediaWiki master
ConfigRepository.php
Go to the documentation of this file.
1<?php
9namespace MediaWiki\Config;
10
11use Wikimedia\Assert\Assert;
12use Wikimedia\Services\SalvageableService;
13
20class ConfigRepository implements SalvageableService {
22 private $configItems = [
23 'private' => [],
24 'public' => [],
25 ];
26
27 public function __construct(
28 private readonly ConfigFactory $configFactory,
29 ) {
30 }
31
39 public function has( $name, $alsoPrivate = false ) {
40 return isset( $this->configItems['public'][$name] ) ||
41 ( $alsoPrivate && isset( $this->configItems['private'][$name] ) );
42 }
43
52 public function get( $name ) {
53 if ( !$this->has( $name, true ) ) {
54 throw new ConfigException( 'The configuration option ' . $name . ' does not exist.' );
55 }
56
57 return $this->configItems['public'][$name] ?? $this->configItems['private'][$name];
58 }
59
70 public function getAll() {
71 return array_merge( $this->configItems['private'], $this->configItems['public'] );
72 }
73
79 public function getPublic() {
80 return $this->configItems['public'];
81 }
82
91 public function getDescriptionOf( $name ) {
92 $config = $this->get( $name );
93 if ( isset( $config['descriptionmsg'] ) ) {
94 return wfMessage( $config['descriptionmsg'] )->escaped();
95 }
96 if ( isset( $config['description'] ) ) {
97 return htmlspecialchars( $config['description'] );
98 }
99 return '';
100 }
101
115 public function add( $name, array $config ) {
116 if ( $this->has( $name ) ) {
117 throw new ConfigException( 'A configuration with the name ' . $name .
118 'does already exist.' );
119 }
120 if ( isset( $config['public'] ) && $config['public'] ) {
121 $this->configItems['public'][$name] = $config;
122 } else {
123 $this->configItems['private'][$name] = $config;
124 }
125 }
126
134 public function isEmpty( $includePrivate = false ) {
135 if ( $includePrivate ) {
136 return empty( $this->configItems['private'] ) &&
137 empty( $this->configItems[ 'public'] );
138 }
139 return empty( $this->configItems['public'] );
140 }
141
151 public function salvage( SalvageableService $other ) {
152 Assert::parameterType( self::class, $other, '$other' );
154 '@phan-var self $other';
155
156 foreach ( $other->configItems['public'] as $name => $otherConfig ) {
157 if ( isset( $this->configItems['public'][$name] ) ) {
158 continue;
159 }
160
161 $this->add( $name, $otherConfig );
162 }
163 foreach ( $other->configItems['private'] as $name => $otherConfig ) {
164 if ( isset( $this->configItems['private'][$name] ) ) {
165 continue;
166 }
167
168 $this->add( $name, $otherConfig );
169 }
170
171 // disable $other
172 $other->configItems = [];
173 }
174}
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.
getDescriptionOf( $name)
Returns the description of the given config option, This can be either a localized description,...
__construct(private readonly ConfigFactory $configFactory,)
getAll()
Returns an array of all configuration items saved in this ConfigRepository.