MediaWiki fundraising/REL1_35
ConfigurationReader.php
Go to the documentation of this file.
1<?php
2
3use Symfony\Component\Yaml\Parser;
4
11
15 protected $configDirectories = [];
16
24 public static function createForGateway( $gateway, $variant, Config $mwConfig ) {
25 $extensionBaseDir = $mwConfig->get( 'ExtensionDirectory' ) . DIRECTORY_SEPARATOR
26 . 'DonationInterface';
28 if ( !is_dir( $extensionBaseDir ) ) {
29 $extensionBaseDir = __DIR__ . DIRECTORY_SEPARATOR . '..';
30 }
31 $configurationReader = new ConfigurationReader();
32
33 // Register general config dir (shipped defaults)
34 $generalBaseConfigDir = $extensionBaseDir . DIRECTORY_SEPARATOR . 'config';
35 $configurationReader->registerConfigDirectory( $generalBaseConfigDir );
36
37 // Register gateway base config dir (gateway-specific shipped defaults)
38 $gatewayBaseConfigDir = $extensionBaseDir . DIRECTORY_SEPARATOR . $gateway . '_gateway'
39 . DIRECTORY_SEPARATOR . 'config';
40 $configurationReader->registerConfigDirectory( $gatewayBaseConfigDir );
41
42 // Register local config dir if set as well as gateway-specific subdirectory
43 $localConfigDir = $mwConfig->get( 'DonationInterfaceLocalConfigurationDirectory' );
44 if ( $localConfigDir ) {
45 $configurationReader->registerConfigDirectory( $localConfigDir );
46 $gatewaySpecificSuffix = DIRECTORY_SEPARATOR . $gateway;
47 $configurationReader->registerConfigDirectory( $localConfigDir . $gatewaySpecificSuffix );
48 }
49
50 // Register variant config dir if set (to vary behavior by a querystring param)
51 // Note that we are currently only setting a gateway-specific dir here, but could
52 // easily support gateway-agnostic variant overrides by adding another.
53 $variantConfigDir = $mwConfig->get( 'DonationInterfaceVariantConfigurationDirectory' );
54 if ( $variant !== null
55 && $variantConfigDir
56 && preg_match( '/^[a-zA-Z0-9_]+$/', $variant )
57 ) {
58 $variantConfigDirSuffix = DIRECTORY_SEPARATOR . $variant . DIRECTORY_SEPARATOR . $gateway;
59 $configurationReader->registerConfigDirectory( $variantConfigDir . $variantConfigDirSuffix );
60 }
61
62 return $configurationReader;
63 }
64
72 public function registerConfigDirectory( string $path ): bool {
73 if ( !array_search( $path, $this->configDirectories ) ) {
74 array_push( $this->configDirectories, $path );
75 return true;
76 } else {
77 return false;
78 }
79 }
80
86 public function unregisterConfigDirectory( string $path ): bool {
87 if ( ( $key = array_search( $path, $this->configDirectories ) ) !== false ) {
88 unset( $this->configDirectories[$key] );
89 return true;
90 } else {
91 return false;
92 }
93 }
94
102 public function readConfiguration(): array {
103 if ( count( $this->configDirectories ) > 0 ) {
104 $config = [];
105 foreach ( $this->configDirectories as $configDirectory ) {
106 $config = $this->setConfigurationFromDirectory( $configDirectory, $config );
107 }
108 return $config;
109 } else {
110 throw new UnexpectedValueException( 'Trying to read config directories but no directories registered!' );
111 }
112 }
113
114 protected function setConfigurationFromDirectory( $directory, $config = [] ) {
115 $yaml = new Parser();
116 $globPattern = $directory . DIRECTORY_SEPARATOR . '*.yaml';
117 foreach ( glob( $globPattern ) as $path ) {
118 $pieces = explode( DIRECTORY_SEPARATOR, $path );
119 $key = substr( array_pop( $pieces ), 0, -5 );
120 $config[$key] = $yaml->parse( file_get_contents( $path ) );
121 }
122 return $config;
123 }
124}
Read in yaml-based config files.
readConfiguration()
Reads configuration in from all registered directories, with directories registered later taking prec...
unregisterConfigDirectory(string $path)
setConfigurationFromDirectory( $directory, $config=[])
registerConfigDirectory(string $path)
static createForGateway( $gateway, $variant, Config $mwConfig)
Interface for configuration instances.
Definition Config.php:29
get( $name)
Get a configuration variable such as "Sitename" or "UploadMaintenance.".
if($IP===false)
Definition status.php:5