Settings: Fix GlobalConfigBuilder use of GLOBALS for PHP 8.1

Follows-up Ibb4fdf83d87ad204da.

Saving a reference to the whole $GLOBALS object is not supported in PHP
8.1 anymore. This change aims to workaround this and still propagate the
new variable setting to the globals.

Bug: T297911
Change-Id: Idafdfae7cbcb353dc5644457d2ebfb1be82aef73
This commit is contained in:
Florian 2021-12-16 22:33:12 +01:00 committed by Krinkle
parent bb1bb4508e
commit 367e8c3423

View file

@ -17,16 +17,28 @@ class GlobalConfigBuilder extends ArrayConfigBuilder {
* @param string $prefix
*/
public function __construct( string $prefix = self::DEFAULT_PREFIX ) {
$this->config = &$GLOBALS;
$this->prefix = $prefix;
}
public function set( string $key, $value, MergeStrategy $mergeStrategy = null ): ConfigBuilder {
return parent::set( $this->prefix . $key, $value, $mergeStrategy );
parent::set( $this->makeKey( $key ), $value, $mergeStrategy );
$this->propagateGlobal( $key );
return $this;
}
public function setDefault( string $key, $value, MergeStrategy $mergeStrategy = null ): ConfigBuilder {
return parent::setDefault( $this->prefix . $key, $value, $mergeStrategy );
parent::setDefault( $this->makeKey( $key ), $value, $mergeStrategy );
$this->propagateGlobal( $key );
return $this;
}
private function propagateGlobal( string $key ): void {
$varKey = $this->makeKey( $key );
$GLOBALS[$varKey] = $this->config[$varKey];
}
private function makeKey( string $key ): string {
return $this->prefix . $key;
}
public function build(): Config {