wiki.techinc.nl/includes/Settings/LocalSettingsLoader.php
daniel 33a3c3c687 Allow LocalSettings.php to be loaded in function scope.
This adds an experimental mode that loads LocalSettings.php in a
function, rather than in top level global scope. This is controlled
using the MW_USE_LOCAL_SETTINGS_LOADER environment variable.

This is not intended for use in production as this time.
The experimental mode should be available in the 1.38 release,
so people can try it out with different setups.

Bug: T304183
Depends-On: Ie6245ff8cd2bc7bab2af3b1450070185dbc2d824
Change-Id: I0f20024803336064ecb07b51fa56581af7b67a85
2022-03-28 14:10:42 +00:00

84 lines
2.5 KiB
PHP

<?php
namespace MediaWiki\Settings;
use MediaWiki\Settings\Source\SettingsFileUtils;
/**
* Utility for loading LocalSettings files.
*
* @since 1.38
*/
class LocalSettingsLoader {
/** @var SettingsBuilder */
private $settingsBuilder;
/** @var string */
private $baseDir;
/**
* @param SettingsBuilder $settingsBuilder
* @param string $baseDir
*/
public function __construct( SettingsBuilder $settingsBuilder, string $baseDir ) {
$this->settingsBuilder = $settingsBuilder;
$this->baseDir = $baseDir;
}
/**
* Loads a settings file into the SettingsBuilder provided to the constructor.
*
* This supports JSON and YAML files, PHP files that return a settings array, as well as
* traditional LocalSettings.php files that set variables, for backwards compatibility.
*
* @warning This does not support setting configuration variables that use a prefix other
* than "wg"!
*
* @param string $file
*/
public function loadLocalSettingsFile( string $file ) {
// If $file is not a PHP file, just load it as a data file.
if ( !str_ends_with( $file, '.php' ) ) {
$this->settingsBuilder->loadFile( $file );
return;
}
// Make config settings available in local scope.
$config = $this->settingsBuilder->getConfig();
foreach ( $this->settingsBuilder->getDefinedConfigKeys() as $key ) {
$var = "wg$key"; // NOTE: broken for extensions that use prefixes other than "wg"!
$$var = $config->get( $key ); // XXX: slow?! Can we get the entire array in one go?
}
// make available some non-config globals available
// phpcs:ignore MediaWiki.VariableAnalysis.UnusedGlobalVariables.UnusedGlobal$wgCommandLineMode
global $wgCommandLineMode;
// make additional variables available
// phpcs:ignore MediaWiki.VariableAnalysis.MisleadingGlobalNames.Misleading$wgSettings
$wgSettings = $this->settingsBuilder;
$IP = $this->baseDir;
// pull in the actual settings file
$file = SettingsFileUtils::resolveRelativeLocation( $file, $this->baseDir );
$settings = require $file;
// Capture config variables.
$overrides = [];
foreach ( get_defined_vars() as $name => $value ) {
if ( str_starts_with( $name, 'wg' ) ) {
$key = substr( $name, 2 );
$overrides[$key] = $value;
}
}
$this->settingsBuilder->overrideConfigValues( $overrides );
// If the file returned a settings array, use it.
// This is especially useful for generated PHP files.
if ( is_array( $settings ) ) {
$this->settingsBuilder->loadArray( $settings );
}
}
}