DeploymentConfig.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App;
  7. use Magento\Framework\Config\ConfigOptionsListConstants;
  8. /**
  9. * Application deployment configuration
  10. *
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class DeploymentConfig
  15. {
  16. /**
  17. * Configuration reader
  18. *
  19. * @var DeploymentConfig\Reader
  20. */
  21. private $reader;
  22. /**
  23. * Configuration data
  24. *
  25. * @var array
  26. */
  27. private $data;
  28. /**
  29. * Flattened data
  30. *
  31. * @var array
  32. */
  33. private $flatData;
  34. /**
  35. * Injected configuration data
  36. *
  37. * @var array
  38. */
  39. private $overrideData;
  40. /**
  41. * Constructor
  42. *
  43. * Data can be optionally injected in the constructor. This object's public interface is intentionally immutable
  44. *
  45. * @param DeploymentConfig\Reader $reader
  46. * @param array $overrideData
  47. */
  48. public function __construct(DeploymentConfig\Reader $reader, $overrideData = [])
  49. {
  50. $this->reader = $reader;
  51. $this->overrideData = $overrideData;
  52. }
  53. /**
  54. * Gets data from flattened data
  55. *
  56. * @param string $key
  57. * @param mixed $defaultValue
  58. * @return mixed|null
  59. */
  60. public function get($key = null, $defaultValue = null)
  61. {
  62. $this->load();
  63. if ($key === null) {
  64. return $this->flatData;
  65. }
  66. if (array_key_exists($key, $this->flatData) && $this->flatData[$key] === null) {
  67. return '';
  68. }
  69. return $this->flatData[$key] ?? $defaultValue;
  70. }
  71. /**
  72. * Checks if data available
  73. *
  74. * @return bool
  75. */
  76. public function isAvailable()
  77. {
  78. $this->data = null;
  79. $this->load();
  80. return isset($this->flatData[ConfigOptionsListConstants::CONFIG_PATH_INSTALL_DATE]);
  81. }
  82. /**
  83. * Gets a value specified key from config data
  84. *
  85. * @param string $key
  86. * @return null|mixed
  87. */
  88. public function getConfigData($key = null)
  89. {
  90. $this->load();
  91. if ($key !== null && !isset($this->data[$key])) {
  92. return null;
  93. }
  94. if (isset($this->data[$key])) {
  95. return $this->data[$key];
  96. }
  97. return $this->data;
  98. }
  99. /**
  100. * Resets config data
  101. *
  102. * @return void
  103. */
  104. public function resetData()
  105. {
  106. $this->data = null;
  107. }
  108. /**
  109. * Check if data from deploy files is available
  110. *
  111. * @return bool
  112. * @since 100.1.3
  113. */
  114. public function isDbAvailable()
  115. {
  116. $this->load();
  117. return isset($this->data['db']);
  118. }
  119. /**
  120. * Loads the configuration data
  121. *
  122. * @return void
  123. */
  124. private function load()
  125. {
  126. if (null === $this->data) {
  127. $this->data = $this->reader->load();
  128. if ($this->overrideData) {
  129. $this->data = array_replace($this->data, $this->overrideData);
  130. }
  131. // flatten data for config retrieval using get()
  132. $this->flatData = $this->flattenParams($this->data);
  133. }
  134. }
  135. /**
  136. * Array keys conversion
  137. *
  138. * Convert associative array of arbitrary depth to a flat associative array with concatenated key path as keys
  139. * each level of array is accessible by path key
  140. *
  141. * @param array $params
  142. * @param string $path
  143. * @return array
  144. * @throws \Exception
  145. */
  146. private function flattenParams(array $params, $path = null)
  147. {
  148. $cache = [];
  149. foreach ($params as $key => $param) {
  150. if ($path) {
  151. $newPath = $path . '/' . $key;
  152. } else {
  153. $newPath = $key;
  154. }
  155. if (isset($cache[$newPath])) {
  156. throw new \Exception("Key collision {$newPath} is already defined.");
  157. }
  158. $cache[$newPath] = $param;
  159. if (is_array($param)) {
  160. $cache = array_merge($cache, $this->flattenParams($param, $newPath));
  161. }
  162. }
  163. return $cache;
  164. }
  165. }