Ejegg has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/376850 )

Change subject: WIP allow alternate configurations with 'variant' option
......................................................................

WIP allow alternate configurations with 'variant' option

TODO: wire up 'variant' option from GatewayPage and API.

Bug: T151769
Change-Id: Id00adb3323c4d0f907920d1ec86dcb75f13100b6
---
M README.txt
M extension.json
A gateway_common/ConfigurationReader.php
M gateway_common/gateway.adapter.php
4 files changed, 90 insertions(+), 9 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/DonationInterface 
refs/changes/50/376850/1

diff --git a/README.txt b/README.txt
index 2b311f0..b7df06f 100644
--- a/README.txt
+++ b/README.txt
@@ -120,6 +120,17 @@
  */
 $wgDonationInterfaceCancelPage = 'Donate-error'
 
+/**
+ * If this is set to a valid directory path, yaml files under this path can
+ * potentially replace built-in config files. For example, if this is set to
+ * /etc/di_alt/, and query string variable 'variant' has value 'nostate',
+ * then /etc/di_alt/nostate/globalcollect/country_fields.yaml will be used
+ * instead of the built-in globalcollect_gateway/config/country_fields.yaml.
+ * Superseded files will be completely ignored, so copy and modify originals.
+ * Only latin letters, numbers, and underscores are allowed in variant.
+ */
+$wgDonationInterfaceVariantConfigurationDirectory = false
+
 ==== Debug and logging ====
 
 $wgDonationInterfaceDisplayDebug = false
diff --git a/extension.json b/extension.json
index e611531..40e057f 100644
--- a/extension.json
+++ b/extension.json
@@ -70,6 +70,7 @@
                "ArrayHelper": "gateway_common/ArrayHelper.php",
                "FiscalNumber": "gateway_common/FiscalNumber.php",
                "ClientSideValidationHelper": 
"gateway_common/ClientSideValidationHelper.php",
+               "ConfigurationReader": "gateway_common/ConfigurationReader.php",
                "ContributionTrackingPlusUnique": 
"gateway_common/ContributionTrackingPlusUnique.php",
                "CountryValidation": "gateway_common/CountryValidation.php",
                "CurrencyRatesModule": "modules/CurrencyRatesModule.php",
@@ -325,6 +326,7 @@
                "DonationInterfaceThankYouPage": "Donate-thanks",
                "DonationInterfaceFailPage": "Donate-error",
                "DonationInterfaceCancelPage": "Donate-cancel",
+               "DonationInterfaceVariantConfigurationDirectory": false,
                "DonationInterfaceRetryLoopCount": 3,
                "DonationInterfaceOrphanCron": {
                        "enable": true,
diff --git a/gateway_common/ConfigurationReader.php 
b/gateway_common/ConfigurationReader.php
new file mode 100644
index 0000000..4718265
--- /dev/null
+++ b/gateway_common/ConfigurationReader.php
@@ -0,0 +1,68 @@
+<?php
+
+use Symfony\Component\Yaml\Parser;
+
+/**
+ * This lets us read variant configurations, but it's a step away from being
+ * able to move all the gateway config files into SmashPig. TODO: reconcile
+ */
+class ConfigurationReader {
+
+       /**
+        * @var string $baseDirectory
+        */
+       protected $baseDirectory;
+
+       /**
+        * @var string
+        */
+       protected $variantBaseDirectory;
+
+       /**
+        * @var string
+        */
+       protected $gatewayIdentifier;
+
+       public function __construct( $baseDirectory, $gatewayIdentifier, 
$variantBaseDirectory = null ) {
+               $this->baseDirectory = $baseDirectory;
+               $this->variantBaseDirectory = $variantBaseDirectory;
+               $this->gatewayIdentifier = $gatewayIdentifier;
+       }
+
+       public function readConfiguration( $variant = null ) {
+               $config = $this->setConfigurationFromDirectory(
+                       $this->baseDirectory . DIRECTORY_SEPARATOR . 'config'
+               );
+               if (
+                       $variant &&
+                       $this->variantBaseDirectory &&
+                       preg_match( '/^[a-zA-Z0-9_]$/', $variant )
+               ) {
+                       $variantDirectory = implode(
+                               DIRECTORY_SEPARATOR,
+                               [
+                                       $this->variantBaseDirectory,
+                                       $variant,
+                                       $this->gatewayIdentifier
+                               ]
+                       );
+                       if ( is_dir( $variantDirectory ) ) {
+                               $config = $this->setConfigurationFromDirectory(
+                                       $variantDirectory, $config
+                               );
+                       }
+               }
+               return $config;
+       }
+
+       protected function setConfigurationFromDirectory( $directory, $config = 
[] ) {
+               $yaml = new Parser();
+               $globPattern = $directory . DIRECTORY_SEPARATOR . '*.yaml';
+               foreach ( glob( $globPattern ) as $path ) {
+                       $pieces = explode( DIRECTORY_SEPARATOR, $path );
+                       $key = substr( array_pop( $pieces ), 0, -5 );
+                       $config[$key] = $yaml->parse( file_get_contents( $path 
) );
+               }
+               return $config;
+       }
+}
diff --git a/gateway_common/gateway.adapter.php 
b/gateway_common/gateway.adapter.php
index 7365e7a..880f6db 100644
--- a/gateway_common/gateway.adapter.php
+++ b/gateway_common/gateway.adapter.php
@@ -24,7 +24,6 @@
 use SmashPig\Core\UtcDate;
 use SmashPig\PaymentData\ReferenceData\CurrencyRates;
 use SmashPig\PaymentData\ReferenceData\NationalCurrencies;
-use Symfony\Component\Yaml\Parser;
 
 /**
  * GatewayAdapter
@@ -217,6 +216,7 @@
 
                $defaults = array(
                        'external_data' => null,
+                       'variant' => null,
                );
                $options = array_merge( $defaults, $options );
                if ( array_key_exists( 'batch_mode', $options ) ) {
@@ -233,7 +233,7 @@
 
                // The following needs to be set up before we initialize 
DonationData.
                // TODO: move the rest of the initialization here
-               $this->loadConfig();
+               $this->loadConfig( $options['variant'] );
                $this->defineOrderIDMeta();
                $this->defineDataConstraints();
                $this->definePaymentMethods();
@@ -285,13 +285,13 @@
         */
        abstract protected function getBasedir();
 
-       public function loadConfig() {
-               $yaml = new Parser();
-               foreach ( glob( $this->getBasedir() . "/config/*.yaml" ) as 
$path ) {
-                       $pieces = explode( "/", $path );
-                       $key = substr( array_pop( $pieces ), 0, -5 );
-                       $this->config[$key] = $yaml->parse( file_get_contents( 
$path ) );
-               }
+       public function loadConfig( $variant = null ) {
+               $configurationReader = new ConfigurationReader(
+                       $this->getBasedir(),
+                       self::IDENTIFIER,
+                       $this->getGlobal( 'VariantConfigurationDirectory' )
+               );
+               $this->config = $configurationReader->readConfiguration( 
$variant );
        }
 
        public function getConfig( $key = null ) {

-- 
To view, visit https://gerrit.wikimedia.org/r/376850
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id00adb3323c4d0f907920d1ec86dcb75f13100b6
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/DonationInterface
Gerrit-Branch: master
Gerrit-Owner: Ejegg <ej...@ejegg.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to