Skip to content

Instantly share code, notes, and snippets.

@Marko-M
Last active June 20, 2022 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Marko-M/cddfcc65a4edf1b0c9ba7b6021977bad to your computer and use it in GitHub Desktop.
Save Marko-M/cddfcc65a4edf1b0c9ba7b6021977bad to your computer and use it in GitHub Desktop.
--- App/DeploymentConfig.php 2022-03-08 00:52:02.000000000 +0000
+++ App/DeploymentConfig.php 2022-06-20 19:45:21.192956000 +0000
@@ -35,14 +35,14 @@
*
* @var array
*/
- private $data;
+ private $data = [];
/**
* Flattened data
*
* @var array
*/
- private $flatData;
+ private $flatData = [];
/**
* Injected configuration data
@@ -76,16 +76,18 @@
*/
public function get($key = null, $defaultValue = null)
{
- $this->load();
if ($key === null) {
+ if (empty($this->flatData)) {
+ $this->reloadData();
+ }
return $this->flatData;
}
-
- if (array_key_exists($key, $this->flatData) && $this->flatData[$key] === null) {
- return '';
+ $result = $this->getByKey($key);
+ if ($result === null) {
+ $this->reloadData();
+ $result = $this->getByKey($key);
}
-
- return $this->flatData[$key] ?? $defaultValue;
+ return $result ?? $defaultValue;
}
/**
@@ -97,27 +99,31 @@
*/
public function isAvailable()
{
- $this->load();
- return isset($this->flatData[ConfigOptionsListConstants::CONFIG_PATH_INSTALL_DATE]);
+ return $this->get(ConfigOptionsListConstants::CONFIG_PATH_INSTALL_DATE) !== null;
}
/**
* Gets a value specified key from config data
*
- * @param string $key
+ * @param string|null $key
* @return null|mixed
* @throws FileSystemException
* @throws RuntimeException
*/
public function getConfigData($key = null)
{
- $this->load();
-
- if ($key !== null && !isset($this->data[$key])) {
- return null;
+ if ($key === null) {
+ if (empty($this->data)) {
+ $this->reloadData();
+ }
+ return $this->data;
}
-
- return $this->data[$key] ?? $this->data;
+ $result = $this->getConfigDataByKey($key);
+ if ($result === null) {
+ $this->reloadData();
+ $result = $this->getConfigDataByKey($key);
+ }
+ return $result;
}
/**
@@ -127,10 +133,29 @@
*/
public function resetData()
{
- $this->data = null;
+ $this->data = [];
+ $this->flatData = [];
}
/**
+ * Loads the configuration data
+ *
+ * @return void
+ * @throws FileSystemException
+ * @throws RuntimeException
+ */
+ private function reloadData(): void
+ {
+ $this->data = array_replace(
+ $this->reader->load(),
+ $this->overrideData ?? [],
+ $this->getEnvOverride()
+ );
+
+ // flatten data for config retrieval using get()
+ $this->flatData = $this->flattenParams($this->data);
+ }
+ /**
* Check if data from deploy files is available
*
* @return bool
@@ -140,8 +165,7 @@
*/
public function isDbAvailable()
{
- $this->load();
- return isset($this->data['db']);
+ return $this->getConfigData('db') !== null;
}
/**
@@ -197,12 +221,12 @@
* each level of array is accessible by path key
*
* @param array $params
- * @param string $path
- * @param array $flattenResult
+ * @param string|null $path
+ * @param array|null $flattenResult
* @return array
* @throws RuntimeException
*/
- private function flattenParams(array $params, $path = null, array &$flattenResult = null) : array
+ private function flattenParams(array $params, ?string $path = null, array &$flattenResult = null): array
{
if (null === $flattenResult) {
$flattenResult = [];
@@ -236,4 +260,26 @@
return $flattenResult;
}
+
+ /**
+ * @param string|null $key
+ * @return mixed|null
+ */
+ private function getByKey(?string $key)
+ {
+ if (array_key_exists($key, $this->flatData) && $this->flatData[$key] === null) {
+ return '';
+ }
+
+ return $this->flatData[$key] ?? null;
+ }
+
+ /**
+ * @param string|null $key
+ * @return mixed|null
+ */
+ private function getConfigDataByKey(?string $key)
+ {
+ return $this->data[$key] ?? null;
+ }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment