Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[DI] Add a simple CSV env var processor #25627

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

Closed
wants to merge 2 commits into from
Closed
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
5 changes: 5 additions & 0 deletions 5 src/Symfony/Component/DependencyInjection/EnvVarProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public static function getProvidedTypes()
'base64' => 'string',
'bool' => 'bool',
'const' => 'bool|int|float|string|array',
'csv' => 'array',
'file' => 'string',
'float' => 'float',
'int' => 'int',
Expand Down Expand Up @@ -149,6 +150,10 @@ public function getEnv($prefix, $name, \Closure $getEnv)
}, $env);
}

if ('csv' === $prefix) {
return str_getcsv($env);
}

throw new RuntimeException(sprintf('Unsupported env var prefix "%s".', $prefix));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function testSimpleProcessor()
'base64' => array('string'),
'bool' => array('bool'),
'const' => array('bool', 'int', 'float', 'string', 'array'),
'csv' => array('array'),
'file' => array('string'),
'float' => array('float'),
'int' => array('int'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,23 @@ public function testDumpedBase64EnvParameters()
$this->assertSame('world', $container->getParameter('hello'));
}

public function testDumpedCsvEnvParameters()
{
$container = new ContainerBuilder();
$container->setParameter('env(foo)', 'foo,bar');
$container->setParameter('hello', '%env(csv:foo)%');
$container->compile();

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

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

require self::$fixturesPath.'/php/services_csv_env.php';
$container = new \Symfony_DI_PhpDumper_Test_CsvParameters();
$this->assertSame(array('foo', 'bar'), $container->getParameter('hello'));
}

public function testCustomEnvParameters()
{
$container = new ContainerBuilder();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?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_CsvParameters 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,
);
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('csv: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(
'env(foo)' => 'foo,bar',
);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.