diff options
author | daniel <dkinzler@wikimedia.org> | 2022-02-11 11:06:46 +0100 |
---|---|---|
committer | daniel <dkinzler@wikimedia.org> | 2022-02-14 21:27:23 +0100 |
commit | 8299c9de3f31fc1fbee27a25de2923bebda078a3 (patch) | |
tree | a455b3942795318461720ce419528f438177a964 | |
parent | 9ca83949432f312aff18e29534a3d1cd81dd55b7 (diff) | |
download | mediawikicore-8299c9de3f31fc1fbee27a25de2923bebda078a3.tar.gz mediawikicore-8299c9de3f31fc1fbee27a25de2923bebda078a3.zip |
Introduce IterableConfig
Bug: T301544
Change-Id: I0a93ebb5305f95cf1b12df276f25a44195fedafa
-rw-r--r-- | includes/Settings/Config/ArrayConfigBuilder.php | 4 | ||||
-rw-r--r-- | includes/config/HashConfig.php | 22 | ||||
-rw-r--r-- | includes/config/IterableConfig.php | 52 | ||||
-rw-r--r-- | tests/phpunit/unit/includes/Settings/Config/ArrayConfigBuilderTest.php | 10 | ||||
-rw-r--r-- | tests/phpunit/unit/includes/config/HashConfigTest.php | 29 |
5 files changed, 113 insertions, 4 deletions
diff --git a/includes/Settings/Config/ArrayConfigBuilder.php b/includes/Settings/Config/ArrayConfigBuilder.php index 300590278680..61790eba7b83 100644 --- a/includes/Settings/Config/ArrayConfigBuilder.php +++ b/includes/Settings/Config/ArrayConfigBuilder.php @@ -4,6 +4,7 @@ namespace MediaWiki\Settings\Config; use Config; use HashConfig; +use MediaWiki\Config\IterableConfig; class ArrayConfigBuilder implements ConfigBuilder { @@ -30,7 +31,8 @@ class ArrayConfigBuilder implements ConfigBuilder { /** * Build the configuration. * - * @return Config + * @todo Once we can use PHP 7.4, change the return type declaration to IterableConfig. + * @return IterableConfig */ public function build(): Config { return new HashConfig( $this->config ); diff --git a/includes/config/HashConfig.php b/includes/config/HashConfig.php index d020d20fe01c..36311b4490fe 100644 --- a/includes/config/HashConfig.php +++ b/includes/config/HashConfig.php @@ -20,12 +20,14 @@ * @file */ +use MediaWiki\Config\IterableConfig; + /** * A Config instance which stores all settings as a member variable * * @since 1.24 */ -class HashConfig implements Config, MutableConfig { +class HashConfig implements Config, MutableConfig, IterableConfig { /** * Array of config settings @@ -75,4 +77,22 @@ class HashConfig implements Config, MutableConfig { public function set( $name, $value ) { $this->settings[$name] = $value; } + + /** + * @inheritDoc + * @since 1.38 + * @return Traversable + */ + public function getIterator(): Traversable { + return new ArrayIterator( $this->settings ); + } + + /** + * @inheritDoc + * @since 1.38 + * @return string[] + */ + public function getNames(): array { + return array_keys( $this->settings ); + } } diff --git a/includes/config/IterableConfig.php b/includes/config/IterableConfig.php new file mode 100644 index 000000000000..7584cc518078 --- /dev/null +++ b/includes/config/IterableConfig.php @@ -0,0 +1,52 @@ +<?php +/** + * Interface for iterable configuration instances. + * + * 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 + */ + +namespace MediaWiki\Config; + +use Config; +use IteratorAggregate; +use Traversable; + +/** + * Interface for iterable configuration instances. + * + * @stable to implement + * + * @since 1.38 + */ +interface IterableConfig extends Config, IteratorAggregate { + + /** + * Returns a traversable view of the configuration variables in this Config object. + * + * @return Traversable<string,mixed> + */ + public function getIterator(): Traversable; + + /** + * Returns the names of configuration variables in this Config object. + * + * @return string[] + */ + public function getNames(): array; + +} diff --git a/tests/phpunit/unit/includes/Settings/Config/ArrayConfigBuilderTest.php b/tests/phpunit/unit/includes/Settings/Config/ArrayConfigBuilderTest.php index e67904bbc7df..fa1bed069917 100644 --- a/tests/phpunit/unit/includes/Settings/Config/ArrayConfigBuilderTest.php +++ b/tests/phpunit/unit/includes/Settings/Config/ArrayConfigBuilderTest.php @@ -2,6 +2,7 @@ namespace MediaWiki\Tests\Unit\Settings\Config; +use MediaWiki\Config\IterableConfig; use MediaWiki\Settings\Config\ArrayConfigBuilder; use MediaWiki\Settings\Config\ConfigBuilder; use PHPUnit\Framework\TestCase; @@ -32,7 +33,12 @@ class ArrayConfigBuilderTest extends TestCase { $this->builder ->set( 'foo', 'bar' ) ->set( 'baz', 'quu' ); - $this->assertSame( 'bar', $this->builder->build()->get( 'foo' ) ); - $this->assertSame( 'quu', $this->builder->build()->get( 'baz' ) ); + + $config = $this->builder->build(); + + $this->assertInstanceOf( IterableConfig::class, $config ); + $this->assertSame( 'bar', $config->get( 'foo' ) ); + $this->assertSame( 'quu', $config->get( 'baz' ) ); + $this->assertSame( [ 'foo', 'baz' ], $config->getNames() ); } } diff --git a/tests/phpunit/unit/includes/config/HashConfigTest.php b/tests/phpunit/unit/includes/config/HashConfigTest.php index 97815ef27e1a..17d176ae8757 100644 --- a/tests/phpunit/unit/includes/config/HashConfigTest.php +++ b/tests/phpunit/unit/includes/config/HashConfigTest.php @@ -61,4 +61,33 @@ class HashConfigTest extends \MediaWikiUnitTestCase { $conf->set( 'one', '3' ); $this->assertSame( '3', $conf->get( 'one' ) ); } + + /** + * @covers HashConfig::getNames + */ + public function testGetNames() { + $conf = new HashConfig( [ + 'one' => '1', + ] ); + $conf->set( 'two', '2' ); + + $this->assertSame( [ 'one', 'two' ], $conf->getNames() ); + } + + /** + * @covers HashConfig::getIterator + */ + public function testTraversable() { + $conf = new HashConfig( [ + 'one' => '1', + ] ); + $conf->set( 'two', '2' ); + + $actual = []; + foreach ( $conf as $name => $value ) { + $actual[$name] = $value; + } + + $this->assertSame( [ 'one' => '1', 'two' => '2' ], $actual ); + } } |