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

Skip to content

Commit fc01bbc

Browse files
committed
Add an url EnvProcessor
1 parent f4682c3 commit fc01bbc

File tree

6 files changed

+330
-0
lines changed

6 files changed

+330
-0
lines changed

src/Symfony/Component/DependencyInjection/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ CHANGELOG
66

77
* added `%env(trim:...)%` processor to trim a string value
88
* added `%env(default:...)%` processor to fallback to a default value
9+
* added `%env(url:...)%` processor to convert an URL or DNS into an array of components
10+
* added `%env(query:...)%` processor to convert a query string into an array of key values
911

1012
4.2.0
1113
-----

src/Symfony/Component/DependencyInjection/EnvVarProcessor.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public static function getProvidedTypes()
4141
'int' => 'int',
4242
'json' => 'array',
4343
'key' => 'bool|int|float|string|array',
44+
'url' => 'array',
45+
'query' => 'array',
4446
'resolve' => 'string',
4547
'default' => 'bool|int|float|string|array',
4648
'string' => 'string',
@@ -177,6 +179,36 @@ public function getEnv($prefix, $name, \Closure $getEnv)
177179
return $env;
178180
}
179181

182+
if ('url' === $prefix) {
183+
$env = \parse_url($env);
184+
185+
if (false === $env) {
186+
throw new RuntimeException(sprintf('Invalid URL in env var "%s"', $name));
187+
}
188+
189+
$env += array(
190+
'scheme' => '',
191+
'host' => '',
192+
'port' => 0,
193+
'user' => '',
194+
'pass' => '',
195+
'path' => '',
196+
'query' => '',
197+
'fragment' => '',
198+
);
199+
200+
// remove the '/' separator
201+
$env['path'] = '/' === ($env['path'][0] ?? '') ? substr($env['path'], 1) : $env['path'];
202+
203+
return $env;
204+
}
205+
206+
if ('query' === $prefix) {
207+
\parse_str($env, $result);
208+
209+
return $result;
210+
}
211+
180212
if ('resolve' === $prefix) {
181213
return preg_replace_callback('/%%|%([^%\s]+)%/', function ($match) use ($name) {
182214
if (!isset($match[1])) {

src/Symfony/Component/DependencyInjection/Tests/Compiler/RegisterEnvVarProcessorsPassTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public function testSimpleProcessor()
3939
'int' => ['int'],
4040
'json' => ['array'],
4141
'key' => ['bool', 'int', 'float', 'string', 'array'],
42+
'url' => ['array'],
43+
'query' => ['array'],
4244
'resolve' => ['string'],
4345
'default' => ['bool', 'int', 'float', 'string', 'array'],
4446
'string' => ['string'],

src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,52 @@ public function testDumpedDefaultEnvParameters()
479479
$this->assertSame('foobaz', $container->getParameter('hello-bar'));
480480
}
481481

482+
public function testDumpedUrlEnvParameters()
483+
{
484+
$container = new ContainerBuilder();
485+
$container->setParameter('env(foo)', 'postgres://user@localhost:5432/database?sslmode=disable');
486+
$container->setParameter('hello', '%env(url:foo)%');
487+
$container->compile();
488+
489+
$dumper = new PhpDumper($container);
490+
$dumper->dump();
491+
492+
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services_url_env.php', $dumper->dump(array('class' => 'Symfony_DI_PhpDumper_Test_UrlParameters')));
493+
494+
require self::$fixturesPath.'/php/services_url_env.php';
495+
$container = new \Symfony_DI_PhpDumper_Test_UrlParameters();
496+
$this->assertSame(array(
497+
'scheme' => 'postgres',
498+
'host' => 'localhost',
499+
'port' => 5432,
500+
'user' => 'user',
501+
'path' => 'database',
502+
'query' => 'sslmode=disable',
503+
'pass' => '',
504+
'fragment' => '',
505+
), $container->getParameter('hello'));
506+
}
507+
508+
public function testDumpedQueryEnvParameters()
509+
{
510+
$container = new ContainerBuilder();
511+
$container->setParameter('env(foo)', 'foo=bar&baz[]=qux');
512+
$container->setParameter('hello', '%env(query:foo)%');
513+
$container->compile();
514+
515+
$dumper = new PhpDumper($container);
516+
$dumper->dump();
517+
518+
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services_query_env.php', $dumper->dump(array('class' => 'Symfony_DI_PhpDumper_Test_QueryParameters')));
519+
520+
require self::$fixturesPath.'/php/services_query_env.php';
521+
$container = new \Symfony_DI_PhpDumper_Test_QueryParameters();
522+
$this->assertSame(array(
523+
'foo' => 'bar',
524+
'baz' => array('qux'),
525+
), $container->getParameter('hello'));
526+
}
527+
482528
public function testDumpedJsonEnvParameters()
483529
{
484530
$container = new ContainerBuilder();
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
4+
use Symfony\Component\DependencyInjection\ContainerInterface;
5+
use Symfony\Component\DependencyInjection\Container;
6+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
7+
use Symfony\Component\DependencyInjection\Exception\LogicException;
8+
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
9+
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
10+
11+
/**
12+
* This class has been auto-generated
13+
* by the Symfony Dependency Injection Component.
14+
*
15+
* @final since Symfony 3.3
16+
*/
17+
class Symfony_DI_PhpDumper_Test_QueryParameters extends Container
18+
{
19+
private $parameters;
20+
private $targetDirs = array();
21+
22+
public function __construct()
23+
{
24+
$this->parameters = $this->getDefaultParameters();
25+
26+
$this->services = $this->privates = array();
27+
28+
$this->aliases = array();
29+
}
30+
31+
public function compile()
32+
{
33+
throw new LogicException('You cannot compile a dumped container that was already compiled.');
34+
}
35+
36+
public function isCompiled()
37+
{
38+
return true;
39+
}
40+
41+
public function getRemovedIds()
42+
{
43+
return array(
44+
'Psr\\Container\\ContainerInterface' => true,
45+
'Symfony\\Component\\DependencyInjection\\ContainerInterface' => true,
46+
);
47+
}
48+
49+
public function getParameter($name)
50+
{
51+
$name = (string) $name;
52+
53+
if (!(isset($this->parameters[$name]) || isset($this->loadedDynamicParameters[$name]) || array_key_exists($name, $this->parameters))) {
54+
throw new InvalidArgumentException(sprintf('The parameter "%s" must be defined.', $name));
55+
}
56+
if (isset($this->loadedDynamicParameters[$name])) {
57+
return $this->loadedDynamicParameters[$name] ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);
58+
}
59+
60+
return $this->parameters[$name];
61+
}
62+
63+
public function hasParameter($name)
64+
{
65+
$name = (string) $name;
66+
67+
return isset($this->parameters[$name]) || isset($this->loadedDynamicParameters[$name]) || array_key_exists($name, $this->parameters);
68+
}
69+
70+
public function setParameter($name, $value)
71+
{
72+
throw new LogicException('Impossible to call set() on a frozen ParameterBag.');
73+
}
74+
75+
public function getParameterBag()
76+
{
77+
if (null === $this->parameterBag) {
78+
$parameters = $this->parameters;
79+
foreach ($this->loadedDynamicParameters as $name => $loaded) {
80+
$parameters[$name] = $loaded ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);
81+
}
82+
$this->parameterBag = new FrozenParameterBag($parameters);
83+
}
84+
85+
return $this->parameterBag;
86+
}
87+
88+
private $loadedDynamicParameters = array(
89+
'hello' => false,
90+
);
91+
private $dynamicParameters = array();
92+
93+
/**
94+
* Computes a dynamic parameter.
95+
*
96+
* @param string The name of the dynamic parameter to load
97+
*
98+
* @return mixed The value of the dynamic parameter
99+
*
100+
* @throws InvalidArgumentException When the dynamic parameter does not exist
101+
*/
102+
private function getDynamicParameter($name)
103+
{
104+
switch ($name) {
105+
case 'hello': $value = $this->getEnv('query:foo'); break;
106+
default: throw new InvalidArgumentException(sprintf('The dynamic parameter "%s" must be defined.', $name));
107+
}
108+
$this->loadedDynamicParameters[$name] = true;
109+
110+
return $this->dynamicParameters[$name] = $value;
111+
}
112+
113+
/**
114+
* Gets the default parameters.
115+
*
116+
* @return array An array of the default parameters
117+
*/
118+
protected function getDefaultParameters()
119+
{
120+
return array(
121+
'env(foo)' => 'foo=bar&baz[]=qux',
122+
);
123+
}
124+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
4+
use Symfony\Component\DependencyInjection\ContainerInterface;
5+
use Symfony\Component\DependencyInjection\Container;
6+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
7+
use Symfony\Component\DependencyInjection\Exception\LogicException;
8+
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
9+
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
10+
11+
/**
12+
* This class has been auto-generated
13+
* by the Symfony Dependency Injection Component.
14+
*
15+
* @final since Symfony 3.3
16+
*/
17+
class Symfony_DI_PhpDumper_Test_UrlParameters extends Container
18+
{
19+
private $parameters;
20+
private $targetDirs = array();
21+
22+
public function __construct()
23+
{
24+
$this->parameters = $this->getDefaultParameters();
25+
26+
$this->services = $this->privates = array();
27+
28+
$this->aliases = array();
29+
}
30+
31+
public function compile()
32+
{
33+
throw new LogicException('You cannot compile a dumped container that was already compiled.');
34+
}
35+
36+
public function isCompiled()
37+
{
38+
return true;
39+
}
40+
41+
public function getRemovedIds()
42+
{
43+
return array(
44+
'Psr\\Container\\ContainerInterface' => true,
45+
'Symfony\\Component\\DependencyInjection\\ContainerInterface' => true,
46+
);
47+
}
48+
49+
public function getParameter($name)
50+
{
51+
$name = (string) $name;
52+
53+
if (!(isset($this->parameters[$name]) || isset($this->loadedDynamicParameters[$name]) || array_key_exists($name, $this->parameters))) {
54+
throw new InvalidArgumentException(sprintf('The parameter "%s" must be defined.', $name));
55+
}
56+
if (isset($this->loadedDynamicParameters[$name])) {
57+
return $this->loadedDynamicParameters[$name] ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);
58+
}
59+
60+
return $this->parameters[$name];
61+
}
62+
63+
public function hasParameter($name)
64+
{
65+
$name = (string) $name;
66+
67+
return isset($this->parameters[$name]) || isset($this->loadedDynamicParameters[$name]) || array_key_exists($name, $this->parameters);
68+
}
69+
70+
public function setParameter($name, $value)
71+
{
72+
throw new LogicException('Impossible to call set() on a frozen ParameterBag.');
73+
}
74+
75+
public function getParameterBag()
76+
{
77+
if (null === $this->parameterBag) {
78+
$parameters = $this->parameters;
79+
foreach ($this->loadedDynamicParameters as $name => $loaded) {
80+
$parameters[$name] = $loaded ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name);
81+
}
82+
$this->parameterBag = new FrozenParameterBag($parameters);
83+
}
84+
85+
return $this->parameterBag;
86+
}
87+
88+
private $loadedDynamicParameters = array(
89+
'hello' => false,
90+
);
91+
private $dynamicParameters = array();
92+
93+
/**
94+
* Computes a dynamic parameter.
95+
*
96+
* @param string The name of the dynamic parameter to load
97+
*
98+
* @return mixed The value of the dynamic parameter
99+
*
100+
* @throws InvalidArgumentException When the dynamic parameter does not exist
101+
*/
102+
private function getDynamicParameter($name)
103+
{
104+
switch ($name) {
105+
case 'hello': $value = $this->getEnv('url:foo'); break;
106+
default: throw new InvalidArgumentException(sprintf('The dynamic parameter "%s" must be defined.', $name));
107+
}
108+
$this->loadedDynamicParameters[$name] = true;
109+
110+
return $this->dynamicParameters[$name] = $value;
111+
}
112+
113+
/**
114+
* Gets the default parameters.
115+
*
116+
* @return array An array of the default parameters
117+
*/
118+
protected function getDefaultParameters()
119+
{
120+
return array(
121+
'env(foo)' => 'postgres://user@localhost:5432/database?sslmode=disable',
122+
);
123+
}
124+
}

0 commit comments

Comments
 (0)