MediaWiki master
DynamicDefaultValues.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Settings;
4
5use LogicException;
8
10
11 private ConfigSchema $configSchema;
12
16 private $declarations;
17
18 public function __construct( ConfigSchema $configSchema ) {
19 $this->configSchema = $configSchema;
20 $this->declarations = $this->configSchema->getDynamicDefaults();
21 }
22
30 public function applyDynamicDefaults( ConfigBuilder $configBuilder ): void {
31 $alreadyComputed = [];
32
33 foreach ( $this->declarations as $key => $unused ) {
34 $this->computeDefaultFor( $key, $configBuilder, $alreadyComputed );
35 }
36 }
37
48 private function computeDefaultFor(
49 string $key,
50 ConfigBuilder $configBuilder,
51 array &$alreadyComputed = [],
52 array $currentlyComputing = []
53 ): void {
54 if ( !isset( $this->declarations[ $key ] ) || isset( $alreadyComputed[ $key ] ) ) {
55 return;
56 }
57 if ( isset( $currentlyComputing[ $key ] ) ) {
58 throw new LogicException(
59 'Cyclic dependency when computing dynamic default: ' .
60 implode( ' -> ', array_keys( $currentlyComputing ) ) . " -> $key"
61 );
62 }
63 if (
64 $configBuilder->get( $key ) !==
65 $this->configSchema->getDefaultFor( $key )
66 ) {
67 // Default was already overridden, nothing more to do
68 $alreadyComputed[ $key ] = true;
69
70 return;
71 }
72
73 $currentlyComputing[ $key ] = true;
74
75 $callback = $this->declarations[ $key ]['callback'];
76 $argNames = $this->declarations[ $key ]['use'] ?? [];
77 $args = [];
78
79 foreach ( $argNames as $argName ) {
80 $this->computeDefaultFor(
81 $argName,
82 $configBuilder,
83 $alreadyComputed,
84 $currentlyComputing
85 );
86 $args[] = $configBuilder->get( $argName );
87 }
88
89 $configBuilder->set( $key, $callback( ...$args ) );
90
91 $alreadyComputed[ $key ] = true;
92 }
93}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
applyDynamicDefaults(ConfigBuilder $configBuilder)
Compute dynamic defaults for settings that have them defined.
get(string $key)
Get the current value for $key.
set(string $key, $value, ?MergeStrategy $mergeStrategy=null)
Set the configuration $key to $value.
Represents a config schema.
getDynamicDefaults()
Get all dynamic default declarations.