AbstractConfigManager.php 3.5 KB

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