Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Allow "json:" env var processor to accept null value #26498

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
Mar 27, 2018
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
4 changes: 2 additions & 2 deletions src/Symfony/Component/DependencyInjection/EnvVarProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ public function getEnv($prefix, $name, \Closure $getEnv)
throw new RuntimeException(sprintf('Invalid JSON in env var "%s": '.json_last_error_msg(), $name));
}

if (!is_array($env)) {
throw new RuntimeException(sprintf('Invalid JSON env var "%s": array expected, %s given.', $name, gettype($env)));
if (null !== $env && !is_array($env)) {
throw new RuntimeException(sprintf('Invalid JSON env var "%s": array or null expected, %s given.', $name, gettype($env)));
}

return $env;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,27 @@ public function testDumpedCsvEnvParameters()
$this->assertSame(array('foo', 'bar'), $container->getParameter('hello'));
}

public function testDumpedJsonEnvParameters()
{
$container = new ContainerBuilder();
$container->setParameter('env(foo)', '["foo","bar"]');
$container->setParameter('env(bar)', 'null');
$container->setParameter('hello', '%env(json:foo)%');
$container->setParameter('hello-bar', '%env(json:bar)%');
$container->compile();

$dumper = new PhpDumper($container);
$dumper->dump();

$this->assertStringEqualsFile(self::$fixturesPath.'/php/services_json_env.php', $dumper->dump(array('class' => 'Symfony_DI_PhpDumper_Test_JsonParameters')));

putenv('foobar="hello"');
require self::$fixturesPath.'/php/services_json_env.php';
$container = new \Symfony_DI_PhpDumper_Test_JsonParameters();
$this->assertSame(array('foo', 'bar'), $container->getParameter('hello'));
$this->assertNull($container->getParameter('hello-bar'));
}

public function testCustomEnvParameters()
{
$container = new ContainerBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,17 +233,29 @@ public function testGetEnvBase64()
$this->assertSame('hello', $result);
}

public function testGetEnvJson()
/**
* @dataProvider validJson
*/
public function testGetEnvJson($value, $processed)
{
$processor = new EnvVarProcessor(new Container());

$result = $processor->getEnv('json', 'foo', function ($name) {
$result = $processor->getEnv('json', 'foo', function ($name) use ($value) {
$this->assertSame('foo', $name);

return json_encode(array(1));
return $value;
});

$this->assertSame(array(1), $result);
$this->assertSame($processed, $result);
}

public function validJson()
{
return array(
array('[1]', array(1)),
array('{"key": "value"}', array('key' => 'value')),
array(null, null),
);
}

/**
Expand Down Expand Up @@ -284,6 +296,7 @@ public function otherJsonValues()
array(1.1),
array(true),
array(false),
array('foo'),
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php

use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;

/**
* This class has been auto-generated
* by the Symfony Dependency Injection Component.
*
* @final since Symfony 3.3
*/
class Symfony_DI_PhpDumper_Test_JsonParameters extends Container
{
private $parameters;
private $targetDirs = array();

/**
* @internal but protected for BC on cache:clear
*/
protected $privates = array();

public function __construct()
{
$this->parameters = $this->getDefaultParameters();

$this->services = $this->privates = array();

$this->aliases = array();
}

public function reset()
{
$this->privates = array();
parent::reset();
}

public function compile()
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');
}

public function isCompiled()
{
return true;
}

public function getRemovedIds()
{
return array(
'Psr\\Container\\ContainerInterface' => true,
'Symfony\\Component\\DependencyInjection\\ContainerInterface' => true,
);
}

public function getParameter($name)
{
$name = (string) $name;

if (!(isset($this->parameters[$name]) || isset($this->loadedDynamicParameters[$name]) || array_key_exists($name, $this->parameters))) {
throw new InvalidArgumentException(sprintf('The parameter "%s" must be defined.', $name));
}
if (isset($this->loadedDynamicParameters[$name])) {
return $this->loadedDynamicParameters[$name] ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);
}

return $this->parameters[$name];
}

public function hasParameter($name)
{
$name = (string) $name;

return isset($this->parameters[$name]) || isset($this->loadedDynamicParameters[$name]) || array_key_exists($name, $this->parameters);
}

public function setParameter($name, $value)
{
throw new LogicException('Impossible to call set() on a frozen ParameterBag.');
}

public function getParameterBag()
{
if (null === $this->parameterBag) {
$parameters = $this->parameters;
foreach ($this->loadedDynamicParameters as $name => $loaded) {
$parameters[$name] = $loaded ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);
}
$this->parameterBag = new FrozenParameterBag($parameters);
}

return $this->parameterBag;
}

private $loadedDynamicParameters = array(
'hello' => false,
'hello-bar' => false,
);
private $dynamicParameters = array();

/**
* Computes a dynamic parameter.
*
* @param string The name of the dynamic parameter to load
*
* @return mixed The value of the dynamic parameter
*
* @throws InvalidArgumentException When the dynamic parameter does not exist
*/
private function getDynamicParameter($name)
{
switch ($name) {
case 'hello': $value = $this->getEnv('json:foo'); break;
case 'hello-bar': $value = $this->getEnv('json:bar'); break;
default: throw new InvalidArgumentException(sprintf('The dynamic parameter "%s" must be defined.', $name));
}
$this->loadedDynamicParameters[$name] = true;

return $this->dynamicParameters[$name] = $value;
}

/**
* Gets the default parameters.
*
* @return array An array of the default parameters
*/
protected function getDefaultParameters()
{
return array(
'env(foo)' => '["foo","bar"]',
'env(bar)' => 'null',
);
}
}