PageRenderTime 43ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/core/classes/framework/config/config.php

https://github.com/GinoPane/fantasia
PHP | 135 lines | 58 code | 12 blank | 65 comment | 7 complexity | a8574f79e3e940b64a1e5ce016f4e761 MD5 | raw file
  1. <?php
  2. class Config
  3. {
  4. /*
  5. * Array to store settings for configuration
  6. */
  7. protected static $_settings = array();
  8. /*
  9. *
  10. */
  11. protected static $_packSettings = array();
  12. /**
  13. *
  14. * Loads configuration data from the file and sets options
  15. *
  16. * @param string $filePath
  17. */
  18. public static function loadFromFile($filePath)
  19. {
  20. static::setOptions(Loader::loadConfigurationFromFile($filePath));
  21. }
  22. /**
  23. *
  24. * @param array $data
  25. */
  26. public static function setOptions($data)
  27. {
  28. if (is_array($data)) {
  29. static::$_settings = array_merge(static::$_settings, $data);
  30. } else {
  31. trigger_error("Wrong settings passed!", E_USER_WARNING);
  32. }
  33. }
  34. /**
  35. *
  36. * @param mixed $key
  37. * @param mixed $value
  38. */
  39. public static function setOption($key, $value)
  40. {
  41. static::setOptions(array($key => $value));
  42. }
  43. /**
  44. *
  45. * Returns option for the key
  46. *
  47. * @param mixed $key
  48. * @return mixed
  49. */
  50. public static function getOption($key)
  51. {
  52. return isset(static::$_settings[$key]) ? static::$_settings[$key] : null;
  53. }
  54. /**
  55. *
  56. * Returns all options from the config
  57. *
  58. * @return array Array with option_key => option_value pairs
  59. */
  60. public static function getOptions()
  61. {
  62. return static::$_settings;
  63. }
  64. /**
  65. *
  66. * Gets array of options from the pack config
  67. *
  68. * @param string $packName
  69. * @param bool $onlyChanges if true, only options set from the config will be returned;
  70. * or they will be merged with application config otherwise
  71. */
  72. public static function getPackOptions($packName, $onlyChanges = false)
  73. {
  74. if (isset(static::$_packSettings[$packName])){
  75. if (!$onlyChanges){
  76. return array_merge(static::getOptions(), static::$_packSettings[$packName]);
  77. } else {
  78. return static::$_packSettings[$packName];
  79. }
  80. } else {
  81. return static::getOptions();
  82. }
  83. }
  84. /**
  85. *
  86. * Gets the option value from the pack config
  87. *
  88. * @param string $key
  89. * @param string $packName
  90. */
  91. public static function getPackOption($key, $packName = "")
  92. {
  93. $options = static::getPackOptions($packName);
  94. return isset($options[$key]) ? $options[$key] : null;
  95. }
  96. /**
  97. *
  98. * Sets pack option value
  99. *
  100. * @param string $key
  101. * @param mixed $value
  102. * @param string $packName
  103. * @return void
  104. */
  105. public static function setPackOption($key, $value, $packName = "")
  106. {
  107. if (!isset(static::$_packSettings[$packName])){
  108. static::$_packSettings[$packName] = array();
  109. }
  110. static::$_packSettings[$packName][$key] = $value;
  111. }
  112. /**
  113. *
  114. * Alias for getOption method
  115. *
  116. * @param mixed $key
  117. * @return mixed
  118. */
  119. public static function _($key)
  120. {
  121. return static::getOption($key);
  122. }
  123. }