Debian Codegraph @

includes/Settings/LocalSettingsLoader.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Settings;
4
5 use MediaWiki\Settings\Source\SettingsFileUtils;
6
7 /**
8 * Utility for loading LocalSettings files.
9 *
10 * @since 1.38
11 */
12 class LocalSettingsLoader {
13
14 private SettingsBuilder $settingsBuilder;
15
16 private string $baseDir;
17
18 /**
19 * @param SettingsBuilder $settingsBuilder
20 * @param string $baseDir
21 */
22 public function __construct( SettingsBuilder $settingsBuilder, string $baseDir ) {
23 $this->settingsBuilder = $settingsBuilder;
24 $this->baseDir = $baseDir;
25 }
26
27 /**
28 * Loads a settings file into the SettingsBuilder provided to the constructor.
29 *
30 * This supports JSON and YAML files, PHP files that return a settings array, as well as
31 * traditional LocalSettings.php files that set variables, for backwards compatibility.
32 *
33 * @warning This does not support setting configuration variables that use a prefix other
34 * than "wg"!
35 *
36 * @param string $file
37 */
38 public function loadLocalSettingsFile( string $file ) {
39 // If $file is not a PHP file, just load it as a data file.
40 if ( !str_ends_with( $file, '.php' ) ) {
41 $this->settingsBuilder->loadFile( $file );
42 return;
43 }
44
45 // Make config settings available in local scope.
46 $config = $this->settingsBuilder->getConfig();
47 foreach ( $this->settingsBuilder->getDefinedConfigKeys() as $key ) {
48 $var = "wg$key"; // NOTE: broken for extensions that use prefixes other than "wg"!
49 $$var = $config->get( $key ); // XXX: slow?! Can we get the entire array in one go?
50 }
51
52 // make available some non-config globals available
53 // phpcs:ignore MediaWiki.VariableAnalysis.UnusedGlobalVariables.UnusedGlobal$wgCommandLineMode, MediaWiki.Usage.DeprecatedGlobalVariables
54 global $wgCommandLineMode;
55
56 // make additional variables available
57 // phpcs:ignore MediaWiki.VariableAnalysis.MisleadingGlobalNames.Misleading$wgSettings
58 $wgSettings = $this->settingsBuilder;
59 $IP = $this->baseDir;
60
61 // pull in the actual settings file
62 $file = SettingsFileUtils::resolveRelativeLocation( $file, $this->baseDir );
63 $settings = require $file;
64
65 // Capture config variables.
66 $overrides = [];
67 foreach ( get_defined_vars() as $name => $value ) {
68 if ( str_starts_with( $name, 'wg' ) ) {
69 $key = substr( $name, 2 );
70 $overrides[$key] = $value;
71 }
72 }
73
74 $this->settingsBuilder->overrideConfigValues( $overrides );
75
76 // If the file returned a settings array, use it.
77 // This is especially useful for generated PHP files.
78 if ( is_array( $settings ) ) {
79 $this->settingsBuilder->loadArray( $settings );
80 }
81 }
82 }