wiki.techinc.nl/includes/Settings/DynamicDefaultValues.php
Doğu Abaris 6dbefa012e Use typed properties in Settings classes
This patch updates several Settings classes within the includes/Settings
directory to use typed properties, thus simplifying the codebase by
removing explicit docblock annotations for class properties.

- DynamicDefaultValues.php: Convert $configSchema property to use
  typed property syntax.
- LocalSettingsLoader.php: Apply typed property syntax to
  $settingsBuilder and $baseDir.
- WikiFarmSettingsLoader.php: Switch $settingsBuilder to typed property.

Change-Id: Iec8ee9c91362d13f49dea65f314d5c1113a19306
2024-01-31 16:47:54 +00:00

96 lines
2.4 KiB
PHP

<?php
namespace MediaWiki\Settings;
use LogicException;
use MediaWiki\Settings\Config\ConfigBuilder;
use MediaWiki\Settings\Config\ConfigSchema;
class DynamicDefaultValues {
private ConfigSchema $configSchema;
/**
* @var array
*/
private $declarations;
/**
* @param ConfigSchema $configSchema
*/
public function __construct( ConfigSchema $configSchema ) {
$this->configSchema = $configSchema;
$this->declarations = $this->configSchema->getDynamicDefaults();
}
/**
* Compute dynamic defaults for settings that have them defined.
*
* @param ConfigBuilder $configBuilder
*
* @return void
*/
public function applyDynamicDefaults( ConfigBuilder $configBuilder ): void {
$alreadyComputed = [];
foreach ( $this->declarations as $key => $unused ) {
$this->computeDefaultFor( $key, $configBuilder, $alreadyComputed );
}
}
/**
* Compute dynamic default for a setting, recursively computing any dependencies.
*
* @param string $key Name of setting
* @param ConfigBuilder $configBuilder
* @param array &$alreadyComputed Map whose keys are the names of settings whose dynamic
* defaults have already been computed
* @param array $currentlyComputing Ordered map whose keys are the names of settings whose
* dynamic defaults are currently being computed, for cycle detection.
*/
private function computeDefaultFor(
string $key,
ConfigBuilder $configBuilder,
array &$alreadyComputed = [],
array $currentlyComputing = []
): void {
if ( !isset( $this->declarations[ $key ] ) || isset( $alreadyComputed[ $key ] ) ) {
return;
}
if ( isset( $currentlyComputing[ $key ] ) ) {
throw new LogicException(
'Cyclic dependency when computing dynamic default: ' .
implode( ' -> ', array_keys( $currentlyComputing ) ) . " -> $key"
);
}
if (
$configBuilder->get( $key ) !==
$this->configSchema->getDefaultFor( $key )
) {
// Default was already overridden, nothing more to do
$alreadyComputed[ $key ] = true;
return;
}
$currentlyComputing[ $key ] = true;
$callback = $this->declarations[ $key ]['callback'];
$argNames = $this->declarations[ $key ]['use'] ?? [];
$args = [];
foreach ( $argNames as $argName ) {
$this->computeDefaultFor(
$argName,
$configBuilder,
$alreadyComputed,
$currentlyComputing
);
$args[] = $configBuilder->get( $argName );
}
$configBuilder->set( $key, $callback( ...$args ) );
$alreadyComputed[ $key ] = true;
}
}