/framework/boot/config/Config.php

http://zoop.googlecode.com/ · PHP · 151 lines · 104 code · 22 blank · 25 comment · 11 complexity · 6e21327dc591f5328784a1eef55d4ebe MD5 · raw file

  1. <?php
  2. /**
  3. * Configuration class
  4. *
  5. * Provides methods for retrieving configuration options from a YAML config file.
  6. *
  7. */
  8. class Config
  9. {
  10. private static $info = array();
  11. private static $file;
  12. static public function suggest($file, $prefix = NULL)
  13. {
  14. if($prefix)
  15. $root = &self::getReference($prefix);
  16. else
  17. $root = &self::$info;
  18. $root = self::mergeArray(Yaml::read($file), $root, true);
  19. }
  20. static public function insist($file, $prefix = NULL)
  21. {
  22. if($prefix)
  23. $root = &self::getReference($prefix);
  24. else
  25. $root = &self::$info;
  26. $root = self::mergeArray($root, Yaml::read($file), false);
  27. }
  28. //
  29. // these should maybe be put into a generic utilities file
  30. //
  31. static public function mergeArray($suggested, $insisted, $insistedFirst)
  32. {
  33. return self::_mergeArray($suggested, $insisted, $insistedFirst);
  34. }
  35. static public function _mergeArray(&$suggested, &$insisted, $insistedFirst)
  36. {
  37. if($insistedFirst)
  38. {
  39. foreach($suggested as $key => $val)
  40. {
  41. if(is_array($val))
  42. self::_mergeArray($suggested[$key], $insisted[$key], $insistedFirst);
  43. else
  44. $insisted[$key] = $val;
  45. }
  46. return $insisted;
  47. }
  48. else
  49. {
  50. foreach($insisted as $key => $val)
  51. {
  52. if(is_array($val))
  53. self::_mergeArray($suggested[$key], $insisted[$key], $insistedFirst);
  54. else
  55. $suggested[$key] = $val;
  56. }
  57. return $suggested;
  58. }
  59. }
  60. /**
  61. * Specify configuration file to use
  62. *
  63. * @param string $file Path and filename of the config file to use
  64. */
  65. static function setConfigFile($file)
  66. {
  67. self::$file = $file;
  68. }
  69. /**
  70. * Loads the config file specified by the $file member variable (or app_dir/config.yaml)
  71. *
  72. */
  73. static function load()
  74. {
  75. self::suggest(zoop_dir . '/config.yaml', 'zoop');
  76. if(!self::$file)
  77. self::setConfigFile(app_dir . '/config.yaml');
  78. self::insist(self::$file);
  79. if(defined('instance_dir') && instance_dir)
  80. self::insist(instance_dir . '/config.yaml');
  81. }
  82. /**
  83. * Returns configuration options based on a path (i.e. zoop.db or zoop.application.info)
  84. *
  85. * @param string $path Path for which to fetch options
  86. * @return array of configuration values
  87. */
  88. static function get($path)
  89. {
  90. if($path === '')
  91. return self::$info;
  92. $parts = explode('.', $path);
  93. $cur = self::$info;
  94. foreach($parts as $thisPart)
  95. if(isset($cur[$thisPart]))
  96. $cur = $cur[$thisPart];
  97. else
  98. return false;
  99. return $cur;
  100. }
  101. static public function set($path, $value)
  102. {
  103. $ref = &self::getReference($path, false);
  104. $ref = $value;
  105. }
  106. static function &getReference($path)
  107. {
  108. $parts = explode('.', $path);
  109. $cur = &self::$info;
  110. foreach($parts as $thisPart)
  111. {
  112. if(isset($cur[$thisPart]))
  113. $cur = &$cur[$thisPart];
  114. else
  115. {
  116. $cur[$thisPart] = array();
  117. $cur = &$cur[$thisPart];
  118. }
  119. }
  120. return $cur;
  121. }
  122. // functions for getting scalar values and then formatting them
  123. static public function getFilePath($configPath)
  124. {
  125. $config = self::get($configPath);
  126. assert(is_string($config));
  127. return Zoop::expandPath($config);
  128. }
  129. }