ConfigManager.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace clagiordano\weblibs\configmanager;
  3. use Exception;
  4. use RuntimeException;
  5. /**
  6. * Class ConfigManager, class for easily read and access to php config array file.
  7. * @package clagiordano\weblibs\configmanager
  8. */
  9. class ConfigManager
  10. {
  11. /** @var array $configData */
  12. private $configData = null;
  13. /** @var string $configFilePath */
  14. private $configFilePath = null;
  15. /**
  16. * Create config object, optionally automatic load config
  17. * from argument $configFilePath
  18. *
  19. * @param string $configFilePath
  20. * @return ConfigManager
  21. */
  22. public function __construct($configFilePath = null)
  23. {
  24. return $this->loadConfig($configFilePath);
  25. }
  26. /**
  27. * Load config data from file and store it into internal property
  28. *
  29. * @param null|string $configFilePath
  30. *
  31. * @return ConfigManager
  32. */
  33. public function loadConfig($configFilePath = null)
  34. {
  35. if (!is_null($configFilePath)) {
  36. $this->configFilePath = $configFilePath;
  37. if (file_exists($configFilePath)) {
  38. $this->configData = require $configFilePath;
  39. }
  40. }
  41. return $this;
  42. }
  43. /**
  44. * Prepare and write config file on disk
  45. *
  46. * @param null|string $configFilePath
  47. * @param bool $autoReloadConfig
  48. *
  49. * @return ConfigManager
  50. * @throws RuntimeException
  51. */
  52. public function saveConfigFile($configFilePath = null, $autoReloadConfig = false)
  53. {
  54. if (is_null($configFilePath)) {
  55. $configFilePath = $this->configFilePath;
  56. }
  57. $configFileContent = "<?php\n\n";
  58. $configFileContent .= "return ";
  59. $configFileContent .= var_export($this->configData, true);
  60. $configFileContent .= ";\n\n";
  61. try {
  62. file_put_contents($configFilePath, $configFileContent);
  63. if (is_callable('opcache_invalidate')) {
  64. /**
  65. * Invalidate opcache for writed file if opcache is available
  66. */
  67. opcache_invalidate($configFilePath, true);
  68. }
  69. } catch (Exception $exc) {
  70. throw new RuntimeException(
  71. __METHOD__ . ": Failed to write config file to path '{$configFilePath}'"
  72. );
  73. }
  74. if ($autoReloadConfig) {
  75. $this->loadConfig($configFilePath);
  76. }
  77. return $this;
  78. }
  79. /**
  80. * Get value pointer from config for get/set value
  81. *
  82. * @param string $configPath
  83. *
  84. * @return mixed
  85. */
  86. private function & getValuePointer($configPath)
  87. {
  88. $configData =& $this->configData;
  89. $parts = explode('.', $configPath);
  90. $length = count($parts);
  91. for ($i = 0; $i < $length; $i++) {
  92. if (!isset($configData[ $parts[ $i ] ])) {
  93. $configData[ $parts[ $i ] ] = ($i === $length) ? [] : null;
  94. }
  95. $configData = &$configData[ $parts[ $i ] ];
  96. }
  97. return $configData;
  98. }
  99. /**
  100. * Get value from config data throught keyValue path
  101. *
  102. * @param string $configPath
  103. * @param mixed $defaultValue
  104. *
  105. * @return mixed
  106. */
  107. public function getValue($configPath, $defaultValue = null)
  108. {
  109. $stored = $this->getValuePointer($configPath);
  110. return (is_null($stored)
  111. ? $defaultValue
  112. : $stored);
  113. }
  114. /**
  115. * Check if exist required config for keyValue
  116. *
  117. * @param string $keyValue
  118. *
  119. * @return mixed
  120. */
  121. public function existValue($keyValue)
  122. {
  123. return !is_null($this->getValue($keyValue));
  124. }
  125. /**
  126. * Set value in config path
  127. *
  128. * @param string $configPath
  129. * @param mixed $newValue
  130. *
  131. * @return ConfigManager
  132. */
  133. public function setValue($configPath, $newValue)
  134. {
  135. $configData = &$this->getValuePointer($configPath);
  136. $configData = $newValue;
  137. return $this;
  138. }
  139. }