PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/ConfigurationPhpUnitVars.php

https://github.com/andreaswolf/Menta
PHP | 103 lines | 50 code | 10 blank | 43 comment | 8 complexity | 64466259f065559d6af1007727742651 MD5 | raw file
  1. <?php
  2. /**
  3. * Configuration to get the config from xml files
  4. *
  5. * @author Fabrizio Branca
  6. */
  7. class Menta_ConfigurationPhpUnitVars implements Menta_Interface_Configuration {
  8. /**
  9. * @var Menta_ConfigurationPhpUnitVars
  10. */
  11. protected static $instance;
  12. /**
  13. * @var array
  14. */
  15. protected static $configurationFiles = array();
  16. /**
  17. * @static
  18. * @throws InvalidArgumentException
  19. * @param $configurationFile
  20. * @return void
  21. */
  22. public static function addConfigurationFile($configurationFile) {
  23. if (!is_file($configurationFile)) {
  24. throw new InvalidArgumentException("Could not find file '$configurationFile'");
  25. }
  26. self::$configurationFiles[] = $configurationFile;
  27. }
  28. /**
  29. * Get singleton instance
  30. *
  31. * @return Menta_ConfigurationPhpUnitVars
  32. */
  33. public static function getInstance() {
  34. if (is_null(self::$instance)) {
  35. self::$instance = new Menta_ConfigurationPhpUnitVars();
  36. }
  37. return self::$instance;
  38. }
  39. /**
  40. * Get configuration value
  41. *
  42. * @throws Exception if key is not found
  43. * @param string $key
  44. * @return string
  45. */
  46. public function getValue($key) {
  47. if (empty($GLOBALS[__CLASS__.'_defaultsLoaded'])) {
  48. $this->loadDefaults();
  49. }
  50. if (!$this->issetKey($key)) {
  51. throw new Exception(sprintf('Could not find configuration key "%s"', $key));
  52. }
  53. // json decoding if possible
  54. $jsonDecoded = json_decode($GLOBALS[$key], true);
  55. if (!is_null($jsonDecoded)) {
  56. $GLOBALS[$key] = $jsonDecoded;
  57. }
  58. return $GLOBALS[$key];
  59. }
  60. /**
  61. * Check if key is set
  62. *
  63. * @param string $key
  64. * @return boolean
  65. */
  66. public function issetKey($key) {
  67. if (empty($GLOBALS[__CLASS__.'_defaultsLoaded'])) {
  68. $this->loadDefaults();
  69. }
  70. return isset($GLOBALS[$key]);
  71. }
  72. /**
  73. * Read values from file and stores them into $GLOBALS without overriding existing values
  74. *
  75. * @return void
  76. */
  77. protected function loadDefaults() {
  78. foreach (self::$configurationFiles as $xmlfile) {
  79. if (file_exists($xmlfile)) {
  80. $xml = simplexml_load_file($xmlfile);
  81. foreach ($xml->xpath('//phpunit/php/var') as $node) { /* @var $node SimpleXMLElement */
  82. $key = (string)$node['name'];
  83. if (!isset($GLOBALS[$key])) { // only set the default value if no other value is set
  84. $GLOBALS[$key] = (string)$node['value'];
  85. }
  86. }
  87. }
  88. }
  89. // storing information to globals instead of static properties. So if globals will
  90. // be restored this information also gets lost and will trigger reloading of the defaults
  91. $GLOBALS[__CLASS__.'_defaultsLoaded'] = true;
  92. }
  93. }