| 1 | <?php |
| 2 | /** |
| 3 | * Copyright 2014 |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | * http://www.gnu.org/copyleft/gpl.html |
| 19 | * |
| 20 | * @file |
| 21 | */ |
| 22 | |
| 23 | namespace MediaWiki\Config; |
| 24 | |
| 25 | use ArrayIterator; |
| 26 | use Traversable; |
| 27 | use function array_key_exists; |
| 28 | |
| 29 | /** |
| 30 | * A Config instance which stores all settings as a member variable |
| 31 | * |
| 32 | * @since 1.24 |
| 33 | */ |
| 34 | class HashConfig implements Config, MutableConfig, IterableConfig { |
| 35 | |
| 36 | /** |
| 37 | * Array of config settings |
| 38 | * |
| 39 | * @var array |
| 40 | */ |
| 41 | private $settings; |
| 42 | |
| 43 | /** |
| 44 | * @return HashConfig |
| 45 | */ |
| 46 | public static function newInstance() { |
| 47 | return new HashConfig; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * @param array $settings Any current settings to pre-load |
| 52 | */ |
| 53 | public function __construct( array $settings = [] ) { |
| 54 | $this->settings = $settings; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @inheritDoc |
| 59 | */ |
| 60 | public function get( $name ) { |
| 61 | if ( !$this->has( $name ) ) { |
| 62 | throw new ConfigException( __METHOD__ . ": undefined option: '$name'" ); |
| 63 | } |
| 64 | |
| 65 | return $this->settings[$name]; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @inheritDoc |
| 70 | * @since 1.24 |
| 71 | */ |
| 72 | public function has( $name ) { |
| 73 | return array_key_exists( $name, $this->settings ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @see MutableConfig::set |
| 78 | * @param string $name |
| 79 | * @param mixed $value |
| 80 | */ |
| 81 | public function set( $name, $value ) { |
| 82 | $this->settings[$name] = $value; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * @inheritDoc |
| 87 | * @since 1.38 |
| 88 | * @return Traversable |
| 89 | */ |
| 90 | public function getIterator(): Traversable { |
| 91 | return new ArrayIterator( $this->settings ); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * @inheritDoc |
| 96 | * @since 1.38 |
| 97 | * @return string[] |
| 98 | */ |
| 99 | public function getNames(): array { |
| 100 | return array_keys( $this->settings ); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Clears all config variables. |
| 105 | * @since 1.39 |
| 106 | */ |
| 107 | public function clear() { |
| 108 | $this->settings = []; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /** @deprecated class alias since 1.41 */ |
| 113 | class_alias( HashConfig::class, 'HashConfig' ); |