Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[DependencyInjection] Fix env default processor with scalar node #59262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,8 @@ public function process(ContainerBuilder $container)
$defaultBag = new ParameterBag($resolvingBag->all());
$envTypes = $resolvingBag->getProvidedTypes();
tBibaut marked this conversation as resolved.
Show resolved Hide resolved
foreach ($resolvingBag->getEnvPlaceholders() + $resolvingBag->getUnusedEnvPlaceholders() as $env => $placeholders) {
$values = [];
if (false === $i = strpos($env, ':')) {
$default = $defaultBag->has("env($env)") ? $defaultBag->get("env($env)") : self::TYPE_FIXTURES['string'];
$defaultType = null !== $default ? get_debug_type($default) : 'string';
$values[$defaultType] = $default;
} else {
$prefix = substr($env, 0, $i);
foreach ($envTypes[$prefix] ?? ['string'] as $type) {
$values[$type] = self::TYPE_FIXTURES[$type] ?? null;
}
}
$values = $this->getPlaceholderValues($env, $defaultBag, $envTypes);

foreach ($placeholders as $placeholder) {
BaseNode::setPlaceholder($placeholder, $values);
}
Expand Down Expand Up @@ -100,4 +91,50 @@ public function getExtensionConfig(): array
$this->extensionConfig = [];
}
}

/**
* @param array<string, list<string>> $envTypes
*
* @return array<string, mixed>
*/
private function getPlaceholderValues(string $env, ParameterBag $defaultBag, array $envTypes): array
{
if (false === $i = strpos($env, ':')) {
[$default, $defaultType] = $this->getParameterDefaultAndDefaultType("env($env)", $defaultBag);

return [$defaultType => $default];
}

$prefix = substr($env, 0, $i);
if ('default' === $prefix) {
$parts = explode(':', $env);
array_shift($parts); // Remove 'default' prefix
$parameter = array_shift($parts); // Retrieve and remove parameter

[$defaultParameter, $defaultParameterType] = $this->getParameterDefaultAndDefaultType($parameter, $defaultBag);

return [
$defaultParameterType => $defaultParameter,
...$this->getPlaceholderValues(implode(':', $parts), $defaultBag, $envTypes),
];
}

$values = [];
foreach ($envTypes[$prefix] ?? ['string'] as $type) {
$values[$type] = self::TYPE_FIXTURES[$type] ?? null;
}

return $values;
}

/**
* @return array{0: string, 1: string}
*/
private function getParameterDefaultAndDefaultType(string $name, ParameterBag $defaultBag): array
{
$default = $defaultBag->has($name) ? $defaultBag->get($name) : self::TYPE_FIXTURES['string'];
$defaultType = null !== $default ? get_debug_type($default) : 'string';

return [$default, $defaultType];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,36 @@ public function testDefaultEnvWithoutPrefixIsValidatedInConfig()
$this->doProcess($container);
}

public function testDefaultProcessorWithScalarNode()
{
$container = new ContainerBuilder();
$container->setParameter('parameter_int', 12134);
$container->setParameter('env(FLOATISH)', 4.2);
$container->registerExtension($ext = new EnvExtension());
$container->prependExtensionConfig('env_extension', $expected = [
'scalar_node' => '%env(default:parameter_int:FLOATISH)%',
]);

$this->doProcess($container);
$this->assertSame($expected, $container->resolveEnvPlaceholders($ext->getConfig()));
}

public function testDefaultProcessorAndAnotherProcessorWithScalarNode()
{
$this->expectException(InvalidTypeException::class);
$this->expectExceptionMessageMatches('/^Invalid type for path "env_extension\.scalar_node"\. Expected one of "bool", "int", "float", "string", but got one of "int", "array"\.$/');

$container = new ContainerBuilder();
$container->setParameter('parameter_int', 12134);
$container->setParameter('env(JSON)', '{ "foo": "bar" }');
$container->registerExtension($ext = new EnvExtension());
$container->prependExtensionConfig('env_extension', [
'scalar_node' => '%env(default:parameter_int:json:JSON)%',
]);

$this->doProcess($container);
}

public function testEnvsAreValidatedInConfigWithInvalidPlaceholder()
{
$this->expectException(InvalidTypeException::class);
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.