ConfigRepository.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 MediaWiki\Services\SalvageableService;
  24. use Wikimedia\Assert\Assert;
  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. if ( isset( $this->configItems['public'][$name] ) ) {
  68. return $this->configItems['public'][$name];
  69. }
  70. return $this->configItems['private'][$name];
  71. }
  72. /**
  73. * Returns an array of all configuration items saved in this ConfigRepository. This includes
  74. * all configuration options, including the ones marked as private and public.
  75. *
  76. * Note: This function does not do any permission checks or something similar. You should not
  77. * use this function, if the output will be available to the public audience! Use
  78. * ConfigRepository::getPublic() instead.
  79. *
  80. * @return array
  81. */
  82. public function getAll() {
  83. return array_merge( $this->configItems['private'], $this->configItems['public'] );
  84. }
  85. /**
  86. * Returns an array of all public configuration options saved in this ConfigRepository.
  87. *
  88. * @return array
  89. */
  90. public function getPublic() {
  91. return $this->configItems['public'];
  92. }
  93. /**
  94. * Returns the current value of the configuration option. If no ConfigRegistry was provided
  95. * when the config was added to the repository, the default value will be returned.
  96. *
  97. * @param string $name The name of the configuration option to get the value of
  98. * @return mixed
  99. * @throws \ConfigException
  100. */
  101. public function getValueOf( $name ) {
  102. $config = $this->get( $name );
  103. if ( !isset( $config['configregistry'] ) ) {
  104. return $config['value'];
  105. }
  106. return $this->configFactory->makeConfig( $config['configregistry'] )->get( $name );
  107. }
  108. /**
  109. * Returns the description of the given config option, This can be either a localized
  110. * description, if one such, or the (maybe english only) description provided in the
  111. * definition of the configuration. If both is not provided an empty string is returned.
  112. *
  113. * @param string $name The name of the configuration option to get the description of
  114. * @return string HTML-escaped string
  115. */
  116. public function getDescriptionOf( $name ) {
  117. $config = $this->get( $name );
  118. if ( isset( $config['descriptionmsg'] ) ) {
  119. return wfMessage( $config['descriptionmsg'] )->escaped();
  120. }
  121. if ( isset( $config['description'] ) ) {
  122. return htmlspecialchars( $config['description'] );
  123. }
  124. return '';
  125. }
  126. /**
  127. * Adds the definition of a configuration to this repository.
  128. *
  129. * @param string $name the name of the config
  130. * @param array $config Options of this config. Values are:
  131. * - value: The default value of this configuration, required
  132. * - providedby: The name of the provider of this config (an extension, core, ...), required
  133. * - configregistry: The name of the config to retrieve the value with, required
  134. * - public: whether this option is public or not, if not set, the option is considered as
  135. * "private", optional
  136. * - description: the not localized description of this config option, optional
  137. * - descriptionmsg: The message key of the localized description of this configuration
  138. * option, optional
  139. * @throws \ConfigException
  140. */
  141. public function add( $name, array $config ) {
  142. if ( $this->has( $name ) ) {
  143. throw new \ConfigException( 'A configuration with the name ' . $name .
  144. 'does already exist. It is provided by: ' .
  145. $this->get( $name )['providedby'] );
  146. }
  147. if ( isset( $config['public'] ) && $config['public'] ) {
  148. $this->configItems['public'][$name] = $config;
  149. } else {
  150. $this->configItems['private'][$name] = $config;
  151. }
  152. }
  153. /**
  154. * Returns true, if there're no elements in this instance, otherwise false.
  155. *
  156. * @param bool $includePrivate Whether configuration options, that are marked as private
  157. * should be included in this check.
  158. * @return bool
  159. */
  160. public function isEmpty( $includePrivate = false ) {
  161. if ( $includePrivate ) {
  162. return empty( $this->configItems['private'] ) &&
  163. empty( $this->configItems[ 'public'] );
  164. }
  165. return empty( $this->configItems['public'] );
  166. }
  167. /**
  168. * Re-uses existing Cache objects from $other. Cache objects are only re-used if the
  169. * registered factory function for both is the same.
  170. *
  171. * @see SalvageableService::salvage()
  172. *
  173. * @param SalvageableService $other The object to salvage state from. $other must have the
  174. * exact same type as $this.
  175. */
  176. public function salvage( SalvageableService $other ) {
  177. Assert::parameterType( self::class, $other, '$other' );
  178. /** @var ConfigRepository $other */
  179. $otherCurrentObj = $other->current();
  180. foreach ( $other->configItems['public'] as $name => $otherConfig ) {
  181. if ( isset( $this->configItems['public'][$name] ) ) {
  182. continue;
  183. }
  184. $this->add( $name, $otherConfig );
  185. // recover the pointer of the other config repository
  186. if ( $otherCurrentObj === $otherConfig ) {
  187. end( $this->configItems['public'] );
  188. }
  189. }
  190. foreach ( $other->configItems['private'] as $name => $otherConfig ) {
  191. if ( isset( $this->configItems['private'][$name] ) ) {
  192. continue;
  193. }
  194. $this->add( $name, $otherConfig );
  195. // recover the pointer of the other config repository
  196. if ( $otherCurrentObj === $otherConfig ) {
  197. end( $this->configItems['private'] );
  198. }
  199. }
  200. // disable $other
  201. $other->configItems = [];
  202. }
  203. }