Skip to content

Commit 0f214bd

Browse files
MCLOUD-14020: Added normalized data function to handle custom tags
1 parent d7f070b commit 0f214bd

File tree

2 files changed

+105
-1
lines changed

2 files changed

+105
-1
lines changed

src/Config/Source/CloudSource.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Magento\CloudDocker\Service\ServiceFactory;
1616
use Magento\CloudDocker\Service\ServiceInterface;
1717
use Symfony\Component\Yaml\Yaml;
18+
use Symfony\Component\Yaml\Tag\TaggedValue;
19+
1820
use Exception;
1921

2022
/**
@@ -94,6 +96,8 @@ public function read(): Repository
9496
$this->filesystem->get($this->fileList->getAppConfig()),
9597
$flags
9698
);
99+
100+
$appConfig = $this->normalizeYamlData($appConfig);
97101
} catch (\Exception $exception) {
98102
throw new SourceException($exception->getMessage(), $exception->getCode(), $exception);
99103
}
@@ -157,6 +161,55 @@ public function read(): Repository
157161
return $repository;
158162
}
159163

164+
/**
165+
* Recursively unwrap Symfony YAML TaggedValue objects and handle common tags.
166+
*
167+
* This method handles !env, !include, !php/const, and unknown tags,
168+
* ensuring all YAML values are normalized to arrays or scalars for safe merging.
169+
*
170+
* @param mixed $data
171+
* @return mixed
172+
*
173+
* @SuppressWarnings("PHPMD.CyclomaticComplexity") Method is intentionally complex due to tag handling.
174+
*/
175+
private function normalizeYamlData(mixed $data): mixed
176+
{
177+
if ($data instanceof TaggedValue) {
178+
$tag = $data->getTag();
179+
$value = $data->getValue();
180+
181+
switch ($tag) {
182+
case '!env':
183+
$envValue = getenv((string)$value);
184+
return $envValue !== false ? $envValue : null;
185+
186+
case '!include':
187+
if (file_exists((string)$value)) {
188+
$included = Yaml::parseFile((string)$value);
189+
return $this->normalizeYamlData($included);
190+
}
191+
return null;
192+
193+
case '!php/const':
194+
// Evaluate the PHP constant
195+
return defined($value) ? constant($value) : null;
196+
197+
default:
198+
$val = $this->normalizeYamlData($value);
199+
return is_array($val) ? $val : [$val];
200+
}
201+
}
202+
203+
if (is_array($data)) {
204+
foreach ($data as $key => $value) {
205+
$data[$key] = $this->normalizeYamlData($value);
206+
}
207+
return $data;
208+
}
209+
210+
return $data;
211+
}
212+
160213
/**
161214
* Adds service relationships to the repository.
162215
*

tests/functional/Codeception/TestInfrastructure.php

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Composer\Factory;
1111
use Composer\IO\NullIO;
1212
use Symfony\Component\Yaml\Yaml;
13+
use Symfony\Component\Yaml\Tag\TaggedValue;
1314

1415
/**
1516
* The module to work with test infrastructure
@@ -706,7 +707,57 @@ private function readYamlConfiguration(string $path): array
706707
if (defined(Yaml::class . '::PARSE_CUSTOM_TAGS')) {
707708
$flags |= Yaml::PARSE_CUSTOM_TAGS;
708709
}
709-
return (array) Yaml::parseFile($path, $flags);
710+
$data = (array) Yaml::parseFile($path, $flags);
711+
return $this->normalizeYamlData($data) ?: [];
712+
}
713+
714+
/**
715+
* Recursively unwrap Symfony YAML TaggedValue objects and handle common tags.
716+
*
717+
* This method handles !env, !include, !php/const, and unknown tags,
718+
* ensuring all YAML values are normalized to arrays or scalars for safe merging.
719+
*
720+
* @param mixed $data
721+
* @return mixed
722+
*
723+
* @SuppressWarnings("PHPMD.CyclomaticComplexity") Method is intentionally complex due to tag handling.
724+
*/
725+
private function normalizeYamlData(mixed $data): mixed
726+
{
727+
if ($data instanceof TaggedValue) {
728+
$tag = $data->getTag();
729+
$value = $data->getValue();
730+
731+
switch ($tag) {
732+
case '!env':
733+
$envValue = getenv((string)$value);
734+
return $envValue !== false ? $envValue : null;
735+
736+
case '!include':
737+
if (file_exists((string)$value)) {
738+
$included = Yaml::parseFile((string)$value);
739+
return $this->normalizeYamlData($included);
740+
}
741+
return null;
742+
743+
case '!php/const':
744+
// Evaluate the PHP constant
745+
return defined($value) ? constant($value) : null;
746+
747+
default:
748+
$val = $this->normalizeYamlData($value);
749+
return is_array($val) ? $val : [$val];
750+
}
751+
}
752+
753+
if (is_array($data)) {
754+
foreach ($data as $key => $value) {
755+
$data[$key] = $this->normalizeYamlData($value);
756+
}
757+
return $data;
758+
}
759+
760+
return $data;
710761
}
711762

712763
/**

0 commit comments

Comments
 (0)