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 $configFactory;
23
25 private $configItems = [
26 'private' => [],
27 'public' => [],
28 ];
29
30 public function __construct( ConfigFactory $configFactory ) {
31 $this->configFactory = $configFactory;
32 }
33
41 public function has( $name, $alsoPrivate = false ) {
42 return isset( $this->configItems['public'][$name] ) ||
43 ( $alsoPrivate && isset( $this->configItems['private'][$name] ) );
44 }
45
54 public function get( $name ) {
55 if ( !$this->has( $name, true ) ) {
56 throw new ConfigException( 'The configuration option ' . $name . ' does not exist.' );
57 }
58
59 return $this->configItems['public'][$name] ?? $this->configItems['private'][$name];
60 }
61
72 public function getAll() {
73 return array_merge( $this->configItems['private'], $this->configItems['public'] );
74 }
75
81 public function getPublic() {
82 return $this->configItems['public'];
83 }
84
93 public function getDescriptionOf( $name ) {
94 $config = $this->get( $name );
95 if ( isset( $config['descriptionmsg'] ) ) {
96 return wfMessage( $config['descriptionmsg'] )->escaped();
97 }
98 if ( isset( $config['description'] ) ) {
99 return htmlspecialchars( $config['description'] );
100 }
101 return '';
102 }
103
119 public function add( $name, array $config ) {
120 if ( $this->has( $name ) ) {
121 throw new ConfigException( 'A configuration with the name ' . $name .
122 'does already exist. It is provided by: ' .
123 $this->get( $name )['providedby'] );
124 }
125 if ( isset( $config['public'] ) && $config['public'] ) {
126 $this->configItems['public'][$name] = $config;
127 } else {
128 $this->configItems['private'][$name] = $config;
129 }
130 }
131
139 public function isEmpty( $includePrivate = false ) {
140 if ( $includePrivate ) {
141 return empty( $this->configItems['private'] ) &&
142 empty( $this->configItems[ 'public'] );
143 }
144 return empty( $this->configItems['public'] );
145 }
146
156 public function salvage( SalvageableService $other ) {
157 Assert::parameterType( self::class, $other, '$other' );
159 '@phan-var self $other';
160
161 foreach ( $other->configItems['public'] as $name => $otherConfig ) {
162 if ( isset( $this->configItems['public'][$name] ) ) {
163 continue;
164 }
165
166 $this->add( $name, $otherConfig );
167 }
168 foreach ( $other->configItems['private'] as $name => $otherConfig ) {
169 if ( isset( $this->configItems['private'][$name] ) ) {
170 continue;
171 }
172
173 $this->add( $name, $otherConfig );
174 }
175
176 // disable $other
177 $other->configItems = [];
178 }
179}
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.