wiki.techinc.nl/includes/Settings/Config/ConfigBuilderBase.php
daniel f9b589f556 config-schema: Define types for all arrays.
This patch ensures that we know which arrays are lists (JsonSchema type
"array") and which are maps (JsonSchema type "object"). We can then
default to array_merge for lists and to array_plus for maps. This seems
clearer than requiring an explicit merge strategy to be declared for all
arrays.

This patch specified a mergeTrategy for some config variables that need
behavior different from the default.

This patch also changes the merging behavior to allow non-array values
to replace arrays and vice versa. It also changes the behavior of
defaults to allow falsy values to override non-falsy defaults.

Bug: T300129
Change-Id: Ia7b0c0250af6a957eac1efb554fb47511f5e093f
2022-02-23 14:09:41 +01:00

51 lines
1.1 KiB
PHP

<?php
namespace MediaWiki\Settings\Config;
abstract class ConfigBuilderBase implements ConfigBuilder {
abstract protected function has( string $key ): bool;
abstract protected function get( string $key );
abstract protected function update( string $key, $value );
/**
* @inheritDoc
*/
public function set(
string $key,
$newValue,
MergeStrategy $mergeStrategy = null
): ConfigBuilder {
if ( $mergeStrategy && is_array( $newValue ) ) {
$oldValue = $this->get( $key );
if ( $oldValue && is_array( $oldValue ) ) {
$newValue = $mergeStrategy->merge( $oldValue, $newValue );
}
}
$this->update( $key, $newValue );
return $this;
}
public function setDefault(
string $key,
$defaultValue,
MergeStrategy $mergeStrategy = null
): ConfigBuilder {
if ( $this->has( $key ) ) {
if ( $mergeStrategy && $defaultValue && is_array( $defaultValue ) ) {
$customValue = $this->get( $key );
if ( is_array( $customValue ) ) {
$newValue = $mergeStrategy->merge( $defaultValue, $customValue );
$this->update( $key, $newValue );
}
}
} else {
$this->update( $key, $defaultValue );
}
return $this;
}
}