ConfigRepository.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /**
  3. * Copyright 2016
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. namespace MediaWiki\Config;
  23. use Wikimedia\Assert\Assert;
  24. use Wikimedia\Services\SalvageableService;
  25. /**
  26. * Object which holds currently registered configuration options.
  27. *
  28. * @since 1.32
  29. */
  30. class ConfigRepository implements SalvageableService {
  31. /** @var \ConfigFactory */
  32. private $configFactory;
  33. /** @var array */
  34. private $configItems = [
  35. 'private' => [],
  36. 'public' => [],
  37. ];
  38. /**
  39. * @param \ConfigFactory $configFactory
  40. */
  41. public function __construct( \ConfigFactory $configFactory ) {
  42. $this->configFactory = $configFactory;
  43. }
  44. /**
  45. * Returns true, if this repository contains a configuration with a specific name.
  46. *
  47. * @param string $name The name of the config to check the existence of
  48. * @param bool $alsoPrivate If set to true, will check the private config options, too
  49. * @return bool
  50. */
  51. public function has( $name, $alsoPrivate = false ) {
  52. return isset( $this->configItems['public'][$name] ) ||
  53. ( $alsoPrivate && isset( $this->configItems['private'][$name] ) );
  54. }
  55. /**
  56. * Returns the ConfigItem with the given name, if there's one. Throws a ConfigException
  57. * otherwise.
  58. *
  59. * @param string $name The name of the configuration option to get
  60. * @return array
  61. * @throws \ConfigException
  62. */
  63. public function get( $name ) {
  64. if ( !$this->has( $name, true ) ) {
  65. throw new \ConfigException( 'The configuration option ' . $name . ' does not exist.' );
  66. }
  67. return $this->configItems['public'][$name] ?? $this->configItems['private'][$name];
  68. }
  69. /**
  70. * Returns an array of all configuration items saved in this ConfigRepository. This includes
  71. * all configuration options, including the ones marked as private and public.
  72. *
  73. * Note: This function does not do any permission checks or something similar. You should not
  74. * use this function, if the output will be available to the public audience! Use
  75. * ConfigRepository::getPublic() instead.
  76. *
  77. * @return array
  78. */
  79. public function getAll() {
  80. return array_merge( $this->configItems['private'], $this->configItems['public'] );
  81. }
  82. /**
  83. * Returns an array of all public configuration options saved in this ConfigRepository.
  84. *
  85. * @return array
  86. */
  87. public function getPublic() {
  88. return $this->configItems['public'];
  89. }
  90. /**
  91. * Returns the current value of the configuration option. If no ConfigRegistry was provided
  92. * when the config was added to the repository, the default value will be returned.
  93. *
  94. * @param string $name The name of the configuration option to get the value of
  95. * @return mixed
  96. * @throws \ConfigException
  97. */
  98. public function getValueOf( $name ) {
  99. $config = $this->get( $name );
  100. if ( !isset( $config['configregistry'] ) ) {
  101. return $config['value'];
  102. }
  103. return $this->configFactory->makeConfig( $config['configregistry'] )->get( $name );
  104. }
  105. /**
  106. * Returns the description of the given config option, This can be either a localized
  107. * description, if one such, or the (maybe english only) description provided in the
  108. * definition of the configuration. If both is not provided an empty string is returned.
  109. *
  110. * @param string $name The name of the configuration option to get the description of
  111. * @return string HTML-escaped string
  112. */
  113. public function getDescriptionOf( $name ) {
  114. $config = $this->get( $name );
  115. if ( isset( $config['descriptionmsg'] ) ) {
  116. return wfMessage( $config['descriptionmsg'] )->escaped();
  117. }
  118. if ( isset( $config['description'] ) ) {
  119. return htmlspecialchars( $config['description'] );
  120. }
  121. return '';
  122. }
  123. /**
  124. * Adds the definition of a configuration to this repository.
  125. *
  126. * @param string $name the name of the config
  127. * @param array $config Options of this config. Values are:
  128. * - value: The default value of this configuration, required
  129. * - providedby: The name of the provider of this config (an extension, core, ...), required
  130. * - configregistry: The name of the config to retrieve the value with, required
  131. * - public: whether this option is public or not, if not set, the option is considered as
  132. * "private", optional
  133. * - description: the not localized description of this config option, optional
  134. * - descriptionmsg: The message key of the localized description of this configuration
  135. * option, optional
  136. * @throws \ConfigException
  137. */
  138. public function add( $name, array $config ) {
  139. if ( $this->has( $name ) ) {
  140. throw new \ConfigException( 'A configuration with the name ' . $name .
  141. 'does already exist. It is provided by: ' .
  142. $this->get( $name )['providedby'] );
  143. }
  144. if ( isset( $config['public'] ) && $config['public'] ) {
  145. $this->configItems['public'][$name] = $config;
  146. } else {
  147. $this->configItems['private'][$name] = $config;
  148. }
  149. }
  150. /**
  151. * Returns true, if there're no elements in this instance, otherwise false.
  152. *
  153. * @param bool $includePrivate Whether configuration options, that are marked as private
  154. * should be included in this check.
  155. * @return bool
  156. */
  157. public function isEmpty( $includePrivate = false ) {
  158. if ( $includePrivate ) {
  159. return empty( $this->configItems['private'] ) &&
  160. empty( $this->configItems[ 'public'] );
  161. }
  162. return empty( $this->configItems['public'] );
  163. }
  164. /**
  165. * Re-uses existing Cache objects from $other. Cache objects are only re-used if the
  166. * registered factory function for both is the same.
  167. *
  168. * @see SalvageableService::salvage()
  169. *
  170. * @param SalvageableService $other The object to salvage state from. $other must have the
  171. * exact same type as $this.
  172. */
  173. public function salvage( SalvageableService $other ) {
  174. Assert::parameterType( self::class, $other, '$other' );
  175. /** @var self $other */
  176. '@phan-var self $other';
  177. foreach ( $other->configItems['public'] as $name => $otherConfig ) {
  178. if ( isset( $this->configItems['public'][$name] ) ) {
  179. continue;
  180. }
  181. $this->add( $name, $otherConfig );
  182. }
  183. foreach ( $other->configItems['private'] as $name => $otherConfig ) {
  184. if ( isset( $this->configItems['private'][$name] ) ) {
  185. continue;
  186. }
  187. $this->add( $name, $otherConfig );
  188. }
  189. // disable $other
  190. $other->configItems = [];
  191. }
  192. }