2021-11-11 17:50:14 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Settings\Config;
|
|
|
|
|
|
|
|
|
|
use Config;
|
|
|
|
|
use GlobalVarConfig;
|
2022-05-05 02:48:49 +00:00
|
|
|
use function array_key_exists;
|
2021-11-11 17:50:14 +00:00
|
|
|
|
2022-02-10 21:12:10 +00:00
|
|
|
class GlobalConfigBuilder extends ConfigBuilderBase {
|
2021-11-11 17:50:14 +00:00
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
|
public const DEFAULT_PREFIX = 'wg';
|
|
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
|
private $prefix;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $prefix
|
|
|
|
|
*/
|
|
|
|
|
public function __construct( string $prefix = self::DEFAULT_PREFIX ) {
|
|
|
|
|
$this->prefix = $prefix;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-10 21:12:10 +00:00
|
|
|
protected function has( string $key ): bool {
|
2022-01-21 20:07:42 +00:00
|
|
|
$var = $this->getVarName( $key );
|
2022-02-10 21:12:10 +00:00
|
|
|
return array_key_exists( $var, $GLOBALS );
|
2021-11-11 17:50:14 +00:00
|
|
|
}
|
|
|
|
|
|
2022-02-10 21:12:10 +00:00
|
|
|
protected function get( string $key ) {
|
2022-01-21 20:07:42 +00:00
|
|
|
$var = $this->getVarName( $key );
|
2022-02-10 21:12:10 +00:00
|
|
|
return $GLOBALS[ $var ] ?? null;
|
|
|
|
|
}
|
2021-12-16 21:33:12 +00:00
|
|
|
|
2022-02-10 21:12:10 +00:00
|
|
|
protected function update( string $key, $value ) {
|
|
|
|
|
$var = $this->getVarName( $key );
|
|
|
|
|
$GLOBALS[ $var ] = $value;
|
2021-12-16 21:33:12 +00:00
|
|
|
}
|
|
|
|
|
|
2022-05-05 02:48:49 +00:00
|
|
|
public function setMulti( array $values, array $mergeStrategies = [] ): ConfigBuilder {
|
2022-05-04 14:52:01 +00:00
|
|
|
// NOTE: It is tempting to do $GLOBALS = array_merge( $GLOBALS, $values ).
|
|
|
|
|
// But that no longer works in PHP 8.1!
|
|
|
|
|
// See https://wiki.php.net/rfc/restrict_globals_usage
|
2022-05-05 02:48:49 +00:00
|
|
|
|
|
|
|
|
foreach ( $values as $key => $newValue ) {
|
2022-05-04 14:52:01 +00:00
|
|
|
$var = $this->prefix . $key; // inline getVarName() to avoid function call
|
2022-05-05 02:48:49 +00:00
|
|
|
|
|
|
|
|
// Optimization: Inlined logic from set() for performance
|
|
|
|
|
if ( isset( $GLOBALS[$var] ) && array_key_exists( $key, $mergeStrategies ) ) {
|
|
|
|
|
$mergeStrategy = $mergeStrategies[$key];
|
|
|
|
|
if ( $mergeStrategy && is_array( $newValue ) ) {
|
|
|
|
|
$oldValue = $GLOBALS[$var];
|
|
|
|
|
if ( $oldValue && is_array( $oldValue ) ) {
|
|
|
|
|
$newValue = $mergeStrategy->merge( $oldValue, $newValue );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$GLOBALS[$var] = $newValue;
|
2022-05-04 14:52:01 +00:00
|
|
|
}
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-21 20:07:42 +00:00
|
|
|
private function getVarName( string $key ): string {
|
2021-12-16 21:33:12 +00:00
|
|
|
return $this->prefix . $key;
|
2021-11-11 17:50:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function build(): Config {
|
|
|
|
|
return new GlobalVarConfig( $this->prefix );
|
|
|
|
|
}
|
|
|
|
|
}
|