PageRenderTime 67ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Milk/Core/Configuration.php

https://github.com/geekbuntu/milk
PHP | 199 lines | 142 code | 39 blank | 18 comment | 17 complexity | dcc6762547620181a1838832844c0965 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. namespace Milk\Core;
  3. use \StdClass,
  4. \ArrayAccess,
  5. \Iterator,
  6. \IteratorAggregate,
  7. \ArrayIterator;
  8. // Dependencies
  9. use Milk\Core\Module,
  10. Milk\Core\Exception,
  11. Milk\Core\Cache,
  12. Milk\Core\Translation;
  13. class Configuration extends Module\Singleton {
  14. public static $current;
  15. public static $settings = array(
  16. 'default' => array()
  17. );
  18. private static $group = "default";
  19. private static $file;
  20. private static $type;
  21. public $loaded = false;
  22. /**
  23. Set default configuration settings
  24. @param $defaults array
  25. **/
  26. public function setDefaults($defaults) {
  27. self::$settings['default'] = array_merge(
  28. self::$settings['default'], $defaults);
  29. return $this;
  30. }
  31. /**
  32. Set current configuration group
  33. @param $group string
  34. **/
  35. public function setGroup($group) {
  36. if ($group !== 'default')
  37. self::$current = new Settings(array_merge(self::$settings['default'], self::$settings[$group]));
  38. else
  39. self::$current = new Settings(self::$settings['default']);
  40. $this->loaded = true;
  41. return $this;
  42. }
  43. /**
  44. Load configuration settings from file
  45. @param $file string
  46. @param $mode
  47. **/
  48. public function loadFile($file) {
  49. if (!is_readable($file))
  50. throw Exception( _("Config file is missing or could not be read") );
  51. self::$type = substr($file, strrpos($file, '.')+1);
  52. switch (self::$type) {
  53. case 'yaml':
  54. $confobj = yaml_parse_file($file);
  55. break;
  56. case 'json':
  57. $data = file_get_contents($file);
  58. $confobj = json_decode($data, true);
  59. break;
  60. default:
  61. return trigger_error("Unknown config file type");
  62. }
  63. self::$file = $file;
  64. self::$settings = array_merge(self::$settings, $confobj);
  65. }
  66. public function saveFile($type='') {
  67. if (empty($type))
  68. $type = self::$type;
  69. switch ($type) {
  70. case 'json':
  71. $json = json_encode(self::$config);
  72. file_put_contents(APP_PATH.'/config.json', $json);
  73. break;
  74. case 'yaml':
  75. $yaml = yaml_emit(self::object2array(self::$config));
  76. file_put_contents(APP_PATH.'/config.yaml', $yaml);
  77. break;
  78. default:
  79. return trigger_error("Unknown config file type");
  80. }
  81. return true;
  82. }
  83. public function __get($var) {
  84. if (isset(self::$current->$var))
  85. return self::$current->$var;
  86. else if (isset($this->$var))
  87. return $this->$var;
  88. }
  89. public function __isset($var) {
  90. if (isset(self::$current->$var))
  91. return true;
  92. else if (isset($this->$var))
  93. return true;
  94. return false;
  95. }
  96. public static function __callStatic($func, $arguments) {
  97. $obj = self::getInstance();
  98. return call_user_func_array($obj->$func, $arguments);
  99. }
  100. /**
  101. Static access to the configuration
  102. @todo We all hate eval() so let's find a better solution
  103. **/
  104. public static function get($var) {
  105. return eval("return self::\$current->$var;");
  106. }
  107. }
  108. class Settings implements ArrayAccess, Iterator {
  109. private $position = 0;
  110. private $settings = array();
  111. public function __construct(array $settings) {
  112. $this->position = 0;
  113. $this->settings = $settings;
  114. }
  115. public function offsetSet($offset, $value) {
  116. if (is_null($offset)) {
  117. $this->settings[] = $value;
  118. } else {
  119. $this->settings[$offset] = $value;
  120. }
  121. }
  122. public function offsetExists($offset) {
  123. return isset($this->settings[$offset]);
  124. }
  125. public function offsetUnset($offset) {
  126. unset($this->settings[$offset]);
  127. }
  128. public function offsetGet($offset) {
  129. if (!isset($this->settings[$offset]))
  130. return null;
  131. if (is_array($this->settings[$offset]))
  132. $this->settings[$offset] = new self($this->settings[$offset]);
  133. return $this->settings[$offset];
  134. }
  135. public function rewind() {
  136. return reset($this->settings);
  137. }
  138. public function current() {
  139. return current($this->settings);
  140. }
  141. public function key() {
  142. return key($this->settings);
  143. }
  144. public function next() {
  145. return next($this->settings);
  146. }
  147. public function valid() {
  148. return key($this->settings) !== null;
  149. }
  150. public function __get($var) {
  151. if (isset($this->settings[$var])) {
  152. if (is_array($this->settings[$var]))
  153. $this->settings[$var] = new self($this->settings[$var]);
  154. return $this->settings[$var];
  155. }
  156. }
  157. public function __isset($var) {
  158. return isset($this->settings[$var]);
  159. }
  160. }