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