GlobalVarConfig.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. * Accesses configuration settings from $GLOBALS
  24. *
  25. * @since 1.23
  26. */
  27. class GlobalVarConfig implements Config {
  28. /**
  29. * Prefix to use for configuration variables
  30. * @var string
  31. */
  32. private $prefix;
  33. /**
  34. * Default builder function
  35. * @return GlobalVarConfig
  36. */
  37. public static function newInstance() {
  38. return new GlobalVarConfig();
  39. }
  40. public function __construct( $prefix = 'wg' ) {
  41. $this->prefix = $prefix;
  42. }
  43. /**
  44. * @inheritDoc
  45. */
  46. public function get( $name ) {
  47. if ( !$this->has( $name ) ) {
  48. throw new ConfigException( __METHOD__ . ": undefined option: '$name'" );
  49. }
  50. return $this->getWithPrefix( $this->prefix, $name );
  51. }
  52. /**
  53. * @inheritDoc
  54. */
  55. public function has( $name ) {
  56. return $this->hasWithPrefix( $this->prefix, $name );
  57. }
  58. /**
  59. * Get a variable with a given prefix, if not the defaults.
  60. *
  61. * @param string $prefix Prefix to use on the variable, if one.
  62. * @param string $name Variable name without prefix
  63. * @return mixed
  64. */
  65. protected function getWithPrefix( $prefix, $name ) {
  66. return $GLOBALS[$prefix . $name];
  67. }
  68. /**
  69. * Check if a variable with a given prefix is set
  70. *
  71. * @param string $prefix Prefix to use on the variable
  72. * @param string $name Variable name without prefix
  73. * @return bool
  74. */
  75. protected function hasWithPrefix( $prefix, $name ) {
  76. $var = $prefix . $name;
  77. return array_key_exists( $var, $GLOBALS );
  78. }
  79. }