Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #26498 Allow "json:" env var processor to accept null value (…
…mcfedr) This PR was merged into the 4.1-dev branch. Discussion ---------- Allow "json:" env var processor to accept null value | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | Currently no docs for this feature Edits the EnvVarProcessor so that it allows any type of variable in JSON encoded vars. Previously it was only possible to use `%env(json:ENV)%` for array types, but this seems to be an arbitrary restriction, when I could have any other data type in that JSON. Valid JSON that was previously rejected: - `1` - `null` - `"string"` Commits ------- abc7480 Allow "json:" env var processor to parse null values
- Loading branch information
Showing
4 changed files
with
178 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_json_env.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
); | ||
} | ||
} |