/lib/Milk/Utils/Config.php

https://github.com/geekbuntu/milk · PHP · 112 lines · 107 code · 5 blank · 0 comment · 1 complexity · 8e6cd00ddb2fdbf456ba601d27e558fd MD5 · raw file

  1. <?php
  2. namespace Milk\Utils;
  3. use \F3,
  4. StdClass;
  5. class Config {
  6. static public $config;
  7. static public $mode_config;
  8. static private $file;
  9. static private $mode;
  10. static private $type;
  11. const PRODUCTION = 0;
  12. const STAGE = 1;
  13. const DEBUG = 2;
  14. static public function load($file, $mode=self::PRODUCTION) {
  15. if (!is_readable($file))
  16. return trigger_error("Config file is missing or can't be read");
  17. if ($mode > 0)
  18. F3::clear('MILK.CONFIG');
  19. if (F3::cached('MILK.CONFIG')) {
  20. $confobj = F3::get('MILK.CONFIG');
  21. } else {
  22. self::$type = substr($file, strrpos($file, '.')+1);
  23. switch (self::$type) {
  24. case 'json':
  25. $data = file_get_contents($file);
  26. $confobj = json_decode($data);
  27. break;
  28. case 'yaml':
  29. $confobj = (object)yaml_parse_file($file);
  30. break;
  31. default:
  32. return trigger_error("Unknown config file type");
  33. }
  34. if ($mode > 0)
  35. F3::set('MILK.CONFIG', $confobj, TRUE);
  36. }
  37. switch ($mode) {
  38. case self::PRODUCTION:
  39. $conf = self::array2object($confobj->production);
  40. break;
  41. case self::STAGE:
  42. $conf = self::array2object(array_merge((array)$confobj->production, (array)$confobj->stage));
  43. break;
  44. case self::DEBUG:
  45. $conf = self::array2object(array_merge((array)$confobj->production, (array)$confobj->stage, (array)$confobj->debug));
  46. break;
  47. }
  48. self::$file = $file;
  49. self::$mode = $mode;
  50. self::$config = $confobj;
  51. self::$mode_config = $conf;
  52. }
  53. public static function save($type='') {
  54. if (empty($type))
  55. $type = self::$type;
  56. switch ($type) {
  57. case 'json':
  58. $json = json_encode(self::$config);
  59. file_put_contents(APP_PATH.'/config.json', $json);
  60. break;
  61. case 'yaml':
  62. $yaml = yaml_emit(self::object2array(self::$config));
  63. file_put_contents(APP_PATH.'/config.yaml', $yaml);
  64. break;
  65. default:
  66. return trigger_error("Unknown config file type");
  67. }
  68. return true;
  69. }
  70. static public function object2array($data){
  71. if(!is_object($data) && !is_array($data)) return $data;
  72. if(is_object($data)) $data = get_object_vars($data);
  73. return array_map('Milk\Utils\Config::object2array', $data);
  74. }
  75. static public function array2object($data){
  76. if(!is_array($data)) return $data;
  77. $object = new StdClass();
  78. if (is_array($data) && count($data) > 0) {
  79. foreach ($data as $name=>$value) {
  80. $name = strtolower(trim($name));
  81. if (!empty($name)) {
  82. $object->$name = self::array2object($value);
  83. }
  84. }
  85. }
  86. return $object;
  87. }
  88. static public function get($var) {
  89. return eval("return self::\$mode_config->$var;");
  90. }
  91. }