Config.class.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * A container to reference config settings without having to re-read the config file.
  4. *
  5. * @author Ironpilot
  6. * @copyright Copywrite (c) 2011, STAPLE CODE
  7. *
  8. * This file is part of the STAPLE Framework.
  9. *
  10. * The STAPLE Framework is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License as published by the
  12. * Free Software Foundation, either version 3 of the License, or (at your option)
  13. * any later version.
  14. *
  15. * The STAPLE Framework is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  18. * more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with the STAPLE Framework. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. class Staple_Config extends Staple_Registry
  24. {
  25. protected static $read = false;
  26. /**
  27. * Disable construction of this object.
  28. */
  29. private function __construct(){}
  30. /**
  31. * Get a config set by header.
  32. * @param string $name
  33. */
  34. public function __get($name)
  35. {
  36. if(!self::$read)
  37. {
  38. self::read();
  39. }
  40. if(array_key_exists($name, self::$store))
  41. {
  42. return self::$store[$name];
  43. }
  44. else
  45. {
  46. return array();
  47. }
  48. }
  49. /**
  50. * Setting config values during runtime are not allowed.
  51. * @throws Exception
  52. */
  53. public function __set($name,$value)
  54. {
  55. throw new Exception('Config changes are not allowed at execution', Staple_Error::APPLICATION_ERROR);
  56. }
  57. /**
  58. * Get a config set by header.
  59. * @param string $name
  60. * @return array
  61. */
  62. public static function get($name)
  63. {
  64. if(!self::$read)
  65. {
  66. self::read();
  67. }
  68. if(array_key_exists($name, self::$store))
  69. {
  70. return self::$store[$name];
  71. }
  72. else
  73. {
  74. return array();
  75. }
  76. }
  77. /**
  78. * Returns the entire config array.
  79. * @return array
  80. */
  81. public static function getAll()
  82. {
  83. return self::$store;
  84. }
  85. /**
  86. * Returns a single value from the configuration file.
  87. *
  88. * @param string $set
  89. * @param string $key
  90. * @return mixed
  91. */
  92. public static function getValue($set,$key)
  93. {
  94. if(!self::$read)
  95. {
  96. self::read();
  97. }
  98. if(array_key_exists($set, self::$store))
  99. {
  100. if(array_key_exists($key, self::$store[$set]))
  101. {
  102. return self::$store[$set][$key];
  103. }
  104. else
  105. {
  106. return NULL;
  107. }
  108. }
  109. else
  110. {
  111. return NULL;
  112. }
  113. }
  114. /**
  115. * Setting config values during runtime are not allowed.
  116. * @throws Exception
  117. */
  118. public static function set($name,$value,$storeInSession = false)
  119. {
  120. throw new Exception('Config changes are not allowed at execution', Staple_Error::APPLICATION_ERROR);
  121. }
  122. /**
  123. * Sets a configuration value at runtime. Returns a true or false on success or failure.
  124. *
  125. * @param string $set
  126. * @param string $key
  127. * @param mixed $value
  128. * @return bool
  129. */
  130. public static function setValue($set,$key,$value)
  131. {
  132. if(!self::$read)
  133. {
  134. self::read();
  135. }
  136. if(array_key_exists($set, self::$store))
  137. {
  138. if(array_key_exists($key, self::$store[$set]))
  139. {
  140. self::$store[$set][$key] = $value;
  141. return true;
  142. }
  143. }
  144. return false;
  145. }
  146. /**
  147. * Read and store the application.ini config file.
  148. */
  149. private static function read()
  150. {
  151. if(defined('CONFIG_ROOT'))
  152. {
  153. $settings = array();
  154. if(file_exists(CONFIG_ROOT.'application.ini'))
  155. {
  156. self::$store = parse_ini_file(CONFIG_ROOT.'application.ini',true);
  157. }
  158. }
  159. self::$read = true;
  160. }
  161. }