Skip to content

Commit

Permalink
Merge branch '4.1'
Browse files Browse the repository at this point in the history
* 4.1:
  [DI] Remove default env type check on validate
  [FrameworkBundle][TwigBridge] Fix BC break from strong dependency on CSRF token storage
  • Loading branch information
nicolas-grekas committed Jun 3, 2018
2 parents c001098 + ed37583 commit 97c3b3d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 27 deletions.
22 changes: 10 additions & 12 deletions Compiler/ValidateEnvPlaceholdersPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Symfony\Component\Config\Definition\BaseNode;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Extension\ConfigurationExtensionInterface;
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
Expand Down Expand Up @@ -42,21 +41,20 @@ public function process(ContainerBuilder $container)
return;
}

$defaultBag = new ParameterBag($container->getParameterBag()->all());
$defaultBag = new ParameterBag($resolvingBag->all());
$envTypes = $resolvingBag->getProvidedTypes();
try {
foreach ($resolvingBag->getEnvPlaceholders() + $resolvingBag->getUnusedEnvPlaceholders() as $env => $placeholders) {
$prefix = (false === $i = strpos($env, ':')) ? 'string' : substr($env, 0, $i);
$types = $envTypes[$prefix] ?? array('string');
$default = ($hasEnv = (false === $i && $defaultBag->has("env($env)"))) ? $defaultBag->get("env($env)") : null;

if (null !== $default && !in_array($type = self::getType($default), $types, true)) {
throw new LogicException(sprintf('Invalid type for env parameter "env(%s)". Expected "%s", but got "%s".', $env, implode('", "', $types), $type));
}

$values = array();
foreach ($types as $type) {
$values[$type] = $hasEnv ? $default : self::$typeFixtures[$type] ?? null;
if (false === $i = strpos($env, ':')) {
$default = $defaultBag->has("env($env)") ? $defaultBag->get("env($env)") : null;
$defaultType = null !== $default ? self::getType($default) : 'string';
$values[$defaultType] = $default;
} else {
$prefix = substr($env, 0, $i);
foreach ($envTypes[$prefix] ?? array('string') as $type) {
$values[$type] = self::$typeFixtures[$type] ?? null;
}
}
foreach ($placeholders as $placeholder) {
BaseNode::setPlaceholder($placeholder, $values);
Expand Down
28 changes: 14 additions & 14 deletions Tests/Compiler/ValidateEnvPlaceholdersPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,31 @@

class ValidateEnvPlaceholdersPassTest extends TestCase
{
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\LogicException
* @expectedExceptionMessage Invalid type for env parameter "env(FOO)". Expected "string", but got "bool".
*/
public function testDefaultEnvIsValidatedByType()
public function testEnvsAreValidatedInConfig()
{
$container = new ContainerBuilder();
$container->setParameter('env(FOO)', true);
$container->registerExtension(new EnvExtension());
$container->prependExtensionConfig('env_extension', array(
'scalar_node' => '%env(FOO)%',
$container->setParameter('env(NULLED)', null);
$container->setParameter('env(FLOATISH)', 3.2);
$container->registerExtension($ext = new EnvExtension());
$container->prependExtensionConfig('env_extension', $expected = array(
'scalar_node' => '%env(NULLED)%',
'scalar_node_not_empty' => '%env(FLOATISH)%',
'int_node' => '%env(int:FOO)%',
'float_node' => '%env(float:BAR)%',
));

$this->doProcess($container);

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

public function testEnvsAreValidatedInConfig()
public function testDefaultEnvWithoutPrefixIsValidatedInConfig()
{
$container = new ContainerBuilder();
$container->setParameter('env(NULLED)', null);
$container->setParameter('env(FLOATISH)', 3.2);
$container->registerExtension($ext = new EnvExtension());
$container->prependExtensionConfig('env_extension', $expected = array(
'scalar_node' => '%env(NULLED)%',
'int_node' => '%env(int:FOO)%',
'float_node' => '%env(float:BAR)%',
'float_node' => '%env(FLOATISH)%',
));

$this->doProcess($container);
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"symfony/proxy-manager-bridge": "Generate service proxies to lazy load them"
},
"conflict": {
"symfony/config": "<4.1",
"symfony/config": "<4.1.1",
"symfony/finder": "<3.4",
"symfony/proxy-manager-bridge": "<3.4",
"symfony/yaml": "<3.4"
Expand Down

0 comments on commit 97c3b3d

Please sign in to comment.