MediaWiki  master
ConfigRepository.php
Go to the documentation of this file.
1 <?php
23 namespace MediaWiki\Config;
24 
25 use Wikimedia\Assert\Assert;
26 use Wikimedia\Services\SalvageableService;
27 
33 class ConfigRepository implements SalvageableService {
35  private $configFactory;
36 
38  private $configItems = [
39  'private' => [],
40  'public' => [],
41  ];
42 
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 }
MediaWiki\Config\ConfigRepository\getAll
getAll()
Returns an array of all configuration items saved in this ConfigRepository.
Definition: ConfigRepository.php:88
MediaWiki\Config\ConfigRepository\__construct
__construct(\ConfigFactory $configFactory)
Definition: ConfigRepository.php:46
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1220
MediaWiki\Config\ConfigRepository\getValueOf
getValueOf( $name)
Returns the current value of the configuration option.
Definition: ConfigRepository.php:109
MediaWiki\Config\ConfigRepository\salvage
salvage(SalvageableService $other)
Re-uses existing Cache objects from $other.
Definition: ConfigRepository.php:189
Config\get
get( $name)
Get a configuration variable such as "Sitename" or "UploadMaintenance.".
MediaWiki\Config\ConfigRepository\has
has( $name, $alsoPrivate=false)
Returns true, if this repository contains a configuration with a specific name.
Definition: ConfigRepository.php:57
MediaWiki\Config\ConfigRepository\getPublic
getPublic()
Returns an array of all public configuration options saved in this ConfigRepository.
Definition: ConfigRepository.php:97
MediaWiki\Config\ConfigRepository\isEmpty
isEmpty( $includePrivate=false)
Returns true, if there're no elements in this instance, otherwise false.
Definition: ConfigRepository.php:172
MediaWiki\Config\ConfigRepository\$configItems
array $configItems
Definition: ConfigRepository.php:38
MediaWiki\Config
Definition: ConfigRepository.php:23
MediaWiki\Config\ConfigRepository
Object which holds currently registered configuration options.
Definition: ConfigRepository.php:33
MediaWiki\Config\ConfigRepository\getDescriptionOf
getDescriptionOf( $name)
Returns the description of the given config option, This can be either a localized description,...
Definition: ConfigRepository.php:126
MediaWiki\Config\ConfigRepository\$configFactory
ConfigFactory $configFactory
Definition: ConfigRepository.php:35
MediaWiki\$config
Config $config
Definition: MediaWiki.php:42
ConfigFactory
Factory class to create Config objects.
Definition: ConfigFactory.php:31
MediaWiki\Config\ConfigRepository\add
add( $name, array $config)
Adds the definition of a configuration to this repository.
Definition: ConfigRepository.php:152