HashConfig.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. * A Config instance which stores all settings as a member variable
  24. *
  25. * @since 1.24
  26. */
  27. class HashConfig implements Config, MutableConfig {
  28. /**
  29. * Array of config settings
  30. *
  31. * @var array
  32. */
  33. private $settings;
  34. /**
  35. * @return HashConfig
  36. */
  37. public static function newInstance() {
  38. return new HashConfig;
  39. }
  40. /**
  41. * @param array $settings Any current settings to pre-load
  42. */
  43. public function __construct( array $settings = [] ) {
  44. $this->settings = $settings;
  45. }
  46. /**
  47. * @inheritDoc
  48. */
  49. public function get( $name ) {
  50. if ( !$this->has( $name ) ) {
  51. throw new ConfigException( __METHOD__ . ": undefined option: '$name'" );
  52. }
  53. return $this->settings[$name];
  54. }
  55. /**
  56. * @inheritDoc
  57. * @since 1.24
  58. */
  59. public function has( $name ) {
  60. return array_key_exists( $name, $this->settings );
  61. }
  62. /**
  63. * @see MutableConfig::set
  64. * @param string $name
  65. * @param mixed $value
  66. */
  67. public function set( $name, $value ) {
  68. $this->settings[$name] = $value;
  69. }
  70. }