Skip to content

[DependencyInjection] Handle env var placeholders in CheckTypeDeclarationsPass #34783

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
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 @@ -14,11 +14,13 @@
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\EnvNotFoundException;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\InvalidParameterTypeException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ExpressionLanguage;
use Symfony\Component\DependencyInjection\Parameter;
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ServiceLocator;
use Symfony\Component\ExpressionLanguage\Expression;
Expand Down Expand Up @@ -104,27 +106,29 @@ private function checkTypeDeclarations(Definition $checkedDefinition, \Reflectio
$reflectionParameters = $reflectionFunction->getParameters();
$checksCount = min($reflectionFunction->getNumberOfParameters(), \count($values));

$envPlaceholderUniquePrefix = $this->container->getParameterBag() instanceof EnvPlaceholderParameterBag ? $this->container->getParameterBag()->getEnvPlaceholderUniquePrefix() : null;

for ($i = 0; $i < $checksCount; ++$i) {
if (!$reflectionParameters[$i]->hasType() || $reflectionParameters[$i]->isVariadic()) {
continue;
}

$this->checkType($checkedDefinition, $values[$i], $reflectionParameters[$i]);
$this->checkType($checkedDefinition, $values[$i], $reflectionParameters[$i], $envPlaceholderUniquePrefix);
}

if ($reflectionFunction->isVariadic() && ($lastParameter = end($reflectionParameters))->hasType()) {
$variadicParameters = \array_slice($values, $lastParameter->getPosition());

foreach ($variadicParameters as $variadicParameter) {
$this->checkType($checkedDefinition, $variadicParameter, $lastParameter);
$this->checkType($checkedDefinition, $variadicParameter, $lastParameter, $envPlaceholderUniquePrefix);
}
}
}

/**
* @throws InvalidParameterTypeException When a parameter is not compatible with the declared type
*/
private function checkType(Definition $checkedDefinition, $value, \ReflectionParameter $parameter): void
private function checkType(Definition $checkedDefinition, $value, \ReflectionParameter $parameter, ?string $envPlaceholderUniquePrefix): void
{
$type = $parameter->getType()->getName();

Expand Down Expand Up @@ -178,8 +182,22 @@ private function checkType(Definition $checkedDefinition, $value, \ReflectionPar
$value = $this->container->getParameter($value);
} elseif ($value instanceof Expression) {
$value = $this->getExpressionLanguage()->evaluate($value, ['container' => $this->container]);
} elseif (\is_string($value) && '%' === ($value[0] ?? '') && preg_match('/^%([^%]+)%$/', $value, $match)) {
$value = $this->container->getParameter($match[1]);
} elseif (\is_string($value)) {
if ('%' === ($value[0] ?? '') && preg_match('/^%([^%]+)%$/', $value, $match)) {
// Only array parameters are not inlined when dumped.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added comments because those things are kind of hard to follow. We actually don't need to resolve the value here since we only care about the type.

$value = [];
} elseif ($envPlaceholderUniquePrefix && false !== strpos($value, 'env_')) {
// If the value is an env placeholder that is either mixed with a string or with another env placeholder, then its resolved value will always be a string, so we don't need to resolve it.
// We don't need to change the value because it is already a string.
if ('' === preg_replace('/'.$envPlaceholderUniquePrefix.'_\w+_[a-f0-9]{32}/U', '', $value, -1, $c) && 1 === $c) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 cases here (with param: "%env(FOO)%":

  • only 1 env placeholder (eg: %param%) -> we try to resolve it to get its type.
  • more than 1 chained env placeholder (eg: %param%%param%) -> it has to be a string
  • mixed env placeholder with a string (eg : foo%param%) -> it has to be a string

try {
$value = $this->container->resolveEnvPlaceholders($value, true);
} catch (EnvNotFoundException | RuntimeException $e) {
// If an env placeholder cannot be resolved, we skip the validation.
return;
}
}
}
}

if (null === $value && $parameter->allowsNull()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\Compiler\CheckTypeDeclarationsPass;
use Symfony\Component\DependencyInjection\Compiler\ResolveParameterPlaceHoldersPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\Bar;
use Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\BarMethodCall;
Expand Down Expand Up @@ -571,6 +573,20 @@ public function testProcessThrowsOnIterableTypeWhenScalarPassed()
$this->assertInstanceOf(\stdClass::class, $container->get('bar')->foo);
}

public function testProcessResolveArrayParameters()
{
$container = new ContainerBuilder();
$container->setParameter('ccc', ['foobar']);

$container
->register('foobar', BarMethodCall::class)
->addMethodCall('setArray', ['%ccc%']);

(new CheckTypeDeclarationsPass(true))->process($container);

$this->addToAssertionCount(1);
}

public function testProcessResolveExpressions()
{
$container = new ContainerBuilder();
Expand All @@ -584,4 +600,73 @@ public function testProcessResolveExpressions()

$this->addToAssertionCount(1);
}

public function testProcessHandleMixedEnvPlaceholder()
{
$this->expectException(\Symfony\Component\DependencyInjection\Exception\InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid definition for service "foobar": argument 1 of "Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\BarMethodCall::setArray" accepts "array", "string" passed.');

$container = new ContainerBuilder(new EnvPlaceholderParameterBag([
'ccc' => '%env(FOO)%',
]));

$container
->register('foobar', BarMethodCall::class)
->addMethodCall('setArray', ['foo%ccc%']);

(new CheckTypeDeclarationsPass(true))->process($container);
}

public function testProcessHandleMultipleEnvPlaceholder()
{
$this->expectException(\Symfony\Component\DependencyInjection\Exception\InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid definition for service "foobar": argument 1 of "Symfony\Component\DependencyInjection\Tests\Fixtures\CheckTypeDeclarationsPass\BarMethodCall::setArray" accepts "array", "string" passed.');

$container = new ContainerBuilder(new EnvPlaceholderParameterBag([
'ccc' => '%env(FOO)%',
'fcy' => '%env(int:BAR)%',
]));

$container
->register('foobar', BarMethodCall::class)
->addMethodCall('setArray', ['%ccc%%fcy%']);

(new CheckTypeDeclarationsPass(true))->process($container);
}

public function testProcessHandleExistingEnvPlaceholder()
{
putenv('ARRAY={"foo":"bar"}');

$container = new ContainerBuilder(new EnvPlaceholderParameterBag([
'ccc' => '%env(json:ARRAY)%',
]));

$container
->register('foobar', BarMethodCall::class)
->addMethodCall('setArray', ['%ccc%']);

(new ResolveParameterPlaceHoldersPass())->process($container);
(new CheckTypeDeclarationsPass(true))->process($container);

$this->addToAssertionCount(1);

putenv('ARRAY=');
}

public function testProcessHandleNotFoundEnvPlaceholder()
{
$container = new ContainerBuilder(new EnvPlaceholderParameterBag([
'ccc' => '%env(json:ARRAY)%',
]));

$container
->register('foobar', BarMethodCall::class)
->addMethodCall('setArray', ['%ccc%']);

(new ResolveParameterPlaceHoldersPass())->process($container);
(new CheckTypeDeclarationsPass(true))->process($container);

$this->addToAssertionCount(1);
}
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy