MultiConfig.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. * Provides a fallback sequence for Config objects
  24. *
  25. * @since 1.24
  26. */
  27. class MultiConfig implements Config {
  28. /**
  29. * Array of Config objects to use
  30. * Order matters, the Config objects
  31. * will be checked in order to see
  32. * whether they have the requested setting
  33. *
  34. * @var Config[]
  35. */
  36. private $configs;
  37. /**
  38. * @param Config[] $configs
  39. */
  40. public function __construct( array $configs ) {
  41. $this->configs = $configs;
  42. }
  43. /**
  44. * @inheritDoc
  45. */
  46. public function get( $name ) {
  47. foreach ( $this->configs as $config ) {
  48. if ( $config->has( $name ) ) {
  49. return $config->get( $name );
  50. }
  51. }
  52. throw new ConfigException( __METHOD__ . ": undefined option: '$name'" );
  53. }
  54. /**
  55. * @inheritDoc
  56. */
  57. public function has( $name ) {
  58. foreach ( $this->configs as $config ) {
  59. if ( $config->has( $name ) ) {
  60. return true;
  61. }
  62. }
  63. return false;
  64. }
  65. }