Add MultiConfig for fallback logic
authorKunal Mehta <legoktm@gmail.com>
Mon, 11 Aug 2014 12:17:37 +0000 (13:17 +0100)
committerKunal Mehta <legoktm@gmail.com>
Wed, 17 Sep 2014 22:55:04 +0000 (15:55 -0700)
This change adds MultiConfig and HashConfig classes,
but does not actually use them anywhere. In a future
change, we can convert DefaultSettings.php into
a HashConfig instance and use MultiConfig as the
'main' config instance.

Bug: 69418
Change-Id: I0ef2fbb86d5c27602d70240219ee08be31e2d09b

includes/AutoLoader.php
includes/config/Config.php
includes/config/GlobalVarConfig.php
includes/config/HashConfig.php [new file with mode: 0644]
includes/config/MultiConfig.php [new file with mode: 0644]
tests/phpunit/includes/config/GlobalVarConfigTest.php
tests/phpunit/includes/config/HashConfigTest.php [new file with mode: 0644]
tests/phpunit/includes/config/MultiConfigTest.php [new file with mode: 0644]

index 4c35bfd..4144325 100644 (file)
@@ -374,6 +374,8 @@ $wgAutoloadLocalClasses = array(
        'ConfigException' => 'includes/config/ConfigException.php',
        'ConfigFactory' => 'includes/config/ConfigFactory.php',
        'GlobalVarConfig' => 'includes/config/GlobalVarConfig.php',
+       'HashConfig' => 'includes/config/HashConfig.php',
+       'MultiConfig' => 'includes/config/MultiConfig.php',
        'MutableConfig' => 'includes/config/MutableConfig.php',
 
        # includes/content
index 03d2cb9..38f589d 100644 (file)
@@ -35,4 +35,13 @@ interface Config {
         * @throws ConfigException
         */
        public function get( $name );
+
+       /**
+        * Check whether a configuration option is set for the given name
+        *
+        * @param string $name Name of configuration option
+        * @return bool
+        * @since 1.24
+        */
+       public function has( $name );
 }
index 1144384..39d6e8e 100644 (file)
@@ -49,9 +49,19 @@ class GlobalVarConfig implements Config {
         * @see Config::get
         */
        public function get( $name ) {
+               if ( !$this->has( $name ) ) {
+                       throw new ConfigException( __METHOD__ . ": undefined option: '$name'" );
+               }
                return $this->getWithPrefix( $this->prefix, $name );
        }
 
+       /**
+        * @see Config::has
+        */
+       public function has( $name ) {
+               return $this->hasWithPrefix( $this->prefix, $name );
+       }
+
        /**
         * @see MutableConfig::set
         * @deprecated since 1.24
@@ -66,15 +76,22 @@ class GlobalVarConfig implements Config {
         *
         * @param string $prefix Prefix to use on the variable, if one.
         * @param string $name Variable name without prefix
-        * @throws ConfigException
         * @return mixed
         */
        protected function getWithPrefix( $prefix, $name ) {
+               return $GLOBALS[$prefix . $name];
+       }
+
+       /**
+        * Check if a variable with a given prefix is set
+        *
+        * @param string $prefix Prefix to use on the variable
+        * @param string $name Variable name without prefix
+        * @return bool
+        */
+       protected function hasWithPrefix( $prefix, $name ) {
                $var = $prefix . $name;
-               if ( !array_key_exists( $var, $GLOBALS ) ) {
-                       throw new ConfigException( __METHOD__ . ": undefined variable: '$var'" );
-               }
-               return $GLOBALS[$var];
+               return array_key_exists( $var, $GLOBALS );
        }
 
        /**
diff --git a/includes/config/HashConfig.php b/includes/config/HashConfig.php
new file mode 100644 (file)
index 0000000..a09a0a4
--- /dev/null
@@ -0,0 +1,75 @@
+<?php
+/**
+ * Copyright 2014
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
+
+/**
+ * A Config instance which stores all settings as a member variable
+ *
+ * @since 1.24
+ */
+class HashConfig implements Config, MutableConfig {
+
+       /**
+        * Array of config settings
+        *
+        * @var array
+        */
+       private $settings;
+
+       /**
+        * @return HashConfig
+        */
+       public static function newInstance() {
+               return new HashConfig;
+       }
+
+       /**
+        * @param array $settings Any current settings to pre-load
+        */
+       public function __construct( array $settings = array() ) {
+               $this->settings = $settings;
+       }
+
+       /**
+        * @see Config::get
+        */
+       public function get( $name ) {
+               if ( !$this->has( $name ) ) {
+                       throw new ConfigException( __METHOD__ . ": undefined option: '$name'" );
+               }
+
+               return $this->settings[$name];
+       }
+
+       /**
+        * @see Config::has
+        */
+       public function has( $name ) {
+               return array_key_exists( $name, $this->settings );
+       }
+
+       /**
+        * @see Config::set
+        */
+       public function set( $name, $value ) {
+               $this->settings[$name] = $value;
+       }
+}
diff --git a/includes/config/MultiConfig.php b/includes/config/MultiConfig.php
new file mode 100644 (file)
index 0000000..cbb65aa
--- /dev/null
@@ -0,0 +1,72 @@
+<?php
+/**
+ * Copyright 2014
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
+
+/**
+ * Provides a fallback sequence for Config objects
+ *
+ * @since 1.24
+ */
+class MultiConfig implements Config {
+
+       /**
+        * Array of Config objects to use
+        * Order matters, the Config objects
+        * will be checked in order to see
+        * whether they have the requested setting
+        *
+        * @var Config[]
+        */
+       private $configs;
+
+       /**
+        * @param Config[] $configs
+        */
+       public function __construct( array $configs ) {
+               $this->configs = $configs;
+       }
+
+       /**
+        * @see Config::get
+        */
+       public function get( $name ) {
+               foreach ( $this->configs as $config ) {
+                       if ( $config->has( $name ) ) {
+                               return $config->get( $name );
+                       }
+               }
+
+               throw new ConfigException( __METHOD__ . ": undefined option: '$name'" );
+       }
+
+       /**
+        * @see Config::has
+        */
+       public function has( $name ) {
+               foreach ( $this->configs as $config ) {
+                       if ( $config->has( $name ) ) {
+                               return true;
+                       }
+               }
+
+               return false;
+       }
+}
index b15ffa7..2750196 100644 (file)
@@ -38,6 +38,18 @@ class GlobalVarConfigTest extends MediaWikiTestCase {
                );
        }
 
+       /**
+        * @covers GlobalVarConfig::has
+        */
+       public function testHas() {
+               $this->maybeStashGlobal( 'wgGlobalVarConfigTestHas' );
+               $GLOBALS['wgGlobalVarConfigTestHas'] = wfRandomString();
+               $this->maybeStashGlobal( 'wgGlobalVarConfigTestNotHas' );
+               $config = new GlobalVarConfig();
+               $this->assertTrue( $config->has( 'GlobalVarConfigTestHas' ) );
+               $this->assertFalse( $config->has( 'GlobalVarConfigTestNotHas' ) );
+       }
+
        public function provideGet() {
                $set = array(
                        'wgSomething' => 'default1',
@@ -70,7 +82,7 @@ class GlobalVarConfigTest extends MediaWikiTestCase {
        public function testGet( $name, $prefix, $expected ) {
                $config = new GlobalVarConfig( $prefix );
                if ( $expected === false ) {
-                       $this->setExpectedException( 'ConfigException', 'GlobalVarConfig::getWithPrefix: undefined variable:' );
+                       $this->setExpectedException( 'ConfigException', 'GlobalVarConfig::get: undefined option:' );
                }
                $this->assertEquals( $config->get( $name ), $expected );
        }
diff --git a/tests/phpunit/includes/config/HashConfigTest.php b/tests/phpunit/includes/config/HashConfigTest.php
new file mode 100644 (file)
index 0000000..3ad3bfb
--- /dev/null
@@ -0,0 +1,63 @@
+<?php
+
+class HashConfigTest extends MediaWikiTestCase {
+
+       /**
+        * @covers HashConfig::newInstance
+        */
+       public function testNewInstance() {
+               $conf = HashConfig::newInstance();
+               $this->assertInstanceOf( 'HashConfig', $conf );
+       }
+
+       /**
+        * @covers HashConfig::__construct
+        */
+       public function testConstructor() {
+               $conf = new HashConfig();
+               $this->assertInstanceOf( 'HashConfig', $conf );
+
+               // Test passing arguments to the constructor
+               $conf2 = new HashConfig( array(
+                       'one' => '1',
+               ) );
+               $this->assertEquals( '1', $conf2->get( 'one' ) );
+       }
+
+       /**
+        * @covers HashConfig::get
+        */
+       public function testGet() {
+               $conf = new HashConfig( array(
+                       'one' => '1',
+               ));
+               $this->assertEquals( '1', $conf->get( 'one' ) );
+               $this->setExpectedException( 'ConfigException', 'HashConfig::get: undefined option' );
+               $conf->get( 'two' );
+       }
+
+       /**
+        * @covers HashConfig::has
+        */
+       public function testHas() {
+               $conf = new HashConfig( array(
+                       'one' => '1',
+               ) );
+               $this->assertTrue( $conf->has( 'one' ) );
+               $this->assertFalse( $conf->has( 'two' ) );
+       }
+
+       /**
+        * @covers HashConfig::set
+        */
+       public function testSet() {
+               $conf = new HashConfig( array(
+                       'one' => '1',
+               ) );
+               $conf->set( 'two', '2' );
+               $this->assertEquals( '2', $conf->get( 'two' ) );
+               // Check that set overwrites
+               $conf->set( 'one', '3' );
+               $this->assertEquals( '3', $conf->get( 'one' ) );
+       }
+}
\ No newline at end of file
diff --git a/tests/phpunit/includes/config/MultiConfigTest.php b/tests/phpunit/includes/config/MultiConfigTest.php
new file mode 100644 (file)
index 0000000..158da46
--- /dev/null
@@ -0,0 +1,38 @@
+<?php
+
+class MultiConfigTest extends MediaWikiTestCase {
+
+       /**
+        * Tests that settings are fetched in the right order
+        *
+        * @covers MultiConfig::get
+        */
+       public function testGet() {
+               $multi = new MultiConfig( array(
+                       new HashConfig( array( 'foo' => 'bar' ) ),
+                       new HashConfig( array( 'foo' => 'baz', 'bar' => 'foo' ) ),
+                       new HashConfig( array( 'bar' => 'baz' ) ),
+               ) );
+
+               $this->assertEquals( 'bar', $multi->get( 'foo' ) );
+               $this->assertEquals( 'foo', $multi->get( 'bar' ) );
+               $this->setExpectedException( 'ConfigException', 'MultiConfig::get: undefined option:' );
+               $multi->get( 'notset' );
+       }
+
+       /**
+        * @covers MultiConfig::has
+        */
+       public function testHas() {
+               $conf = new MultiConfig( array(
+                       new HashConfig( array( 'foo' => 'foo' ) ),
+                       new HashConfig( array( 'something' => 'bleh' ) ),
+                       new HashConfig( array( 'meh' => 'eh' ) ),
+               ) );
+
+               $this->assertTrue( $conf->has( 'foo' ) );
+               $this->assertTrue( $conf->has( 'something' ) );
+               $this->assertTrue( $conf->has( 'meh' ) );
+               $this->assertFalse( $conf->has( 'what' ) );
+       }
+}