-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DI] Add a "default" EnvProcessor #28976
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
130 changes: 130 additions & 0 deletions
130
src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_default_env.php
This file contains hidden or 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,130 @@ | ||
<?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_DefaultParameters extends Container | ||
{ | ||
private $parameters; | ||
private $targetDirs = array(); | ||
|
||
public function __construct() | ||
{ | ||
$this->parameters = $this->getDefaultParameters(); | ||
|
||
$this->services = $this->privates = array(); | ||
|
||
$this->aliases = array(); | ||
} | ||
|
||
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( | ||
'fallback_env' => false, | ||
'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 'fallback_env': $value = $this->getEnv('foobar'); break; | ||
case 'hello': $value = $this->getEnv('default:fallback_param:bar'); break; | ||
case 'hello-bar': $value = $this->getEnv('default:fallback_env:key:baz:json:foo'); 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( | ||
'fallback_param' => 'baz', | ||
'env(foobar)' => 'foobaz', | ||
'env(foo)' => '{"foo": "bar"}', | ||
); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
while at it..
extends RuntimeException
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the variable is not found or a a key contain a typo, it make sens to be a logic exception.