8000 Add an url EnvProcessor · symfony/symfony@345b19b · GitHub
[go: up one dir, main page]

Skip to content

Commit 345b19b

Browse files
committed
Add an url EnvProcessor
1 parent 8c24c35 commit 345b19b

File tree

5 files changed

+177
-0
lines changed

5 files changed

+177
-0
lines changed

src/Symfony/Component/DependencyInjection/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ CHANGELOG
1010
* added `ServiceLocatorArgument` and `!service_locator` config tag for creating optimized service-locators
1111
* added support for autoconfiguring bindings
1212
* added `%env(key:...)%` processor to fetch a specific key from an array
13+
* added `%env(url:...)%` processor to parse an URL or DNS
1314
* deprecated `ServiceSubscriberInterface`, use the same interface from the `Symfony\Contracts\Service` namespace instead
1415
* deprecated `ResettableContainerInterface`, use `Symfony\Contracts\Service\ResetInterface` instead
1516

src/Symfony/Component/DependencyInjection/EnvVarProcessor.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public static function getProvidedTypes()
4343
'int' => 'int',
4444
'json' => 'array',
4545
'key' => 'bool|int|float|string|array',
46+
'url' => 'array',
4647
'resolve' => 'string',
4748
'string' => 'string',
4849
);
@@ -157,6 +158,30 @@ public function getEnv($prefix, $name, \Closure $getEnv)
157158
return $env;
158159
}
159160

161+
if ('url' === $prefix) {
162+
$env = \parse_url($env);
163+
164+
if (false === $env) {
165+
throw new RuntimeException(sprintf('Invalid URL in env var "%s"', $name));
166+
}
167+
168+
$env += array(
169+
'scheme' => '',
170+
'host' => '',
171+
'port' => 0,
172+
'user' => '',
173+
'pass' => '',
174+
'path' => '',
175+
'query' => '',
176+
'fragment' => '',
177+
);
178+
179+
// remove the '/' separator
180+
$env['path'] = '/' === ($env['path'][0] ?? '') ? substr($env['path'], 1) : $env['path'];
181+
182+
return $env;
183+
}
184+
160185
if ('resolve' === $prefix) {
161186
return preg_replace_callback('/%%|%([^%\s]+)%/', function ($match) use ($name) {
162187
if (!isset($match[1])) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function testSimpleProcessor()
3939
'int' => array('int'),
4040
'json' => array('array'),
4141
'key' => array('bool', 'int', 'float', 'string', 'array'),
42+
'url' => array('array'),
4243
'resolve' => array('string'),
4344
'string' => array('string'),
4445
);

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,32 @@ public function testDumpedCsvEnvParameters()
439439
$this->assertSame(array('foo', 'bar'), $container->getParameter('hello'));
440440
}
441441

442+
public function testDumpedUrlEnvParameters()
443+
{
444+
$container = new ContainerBuilder();
445+
$container->setParameter('env(foo)', 'postgres://user@localhost:5432/database?sslmode=disable');
446+
$container->setParameter('hello', '%env(url:foo)%');
447+
$container->compile();
448+
449+
$dumper = new PhpDumper($container);
450+
$dumper->dump();
451+
452+
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services_url_env.php', $dumper->dump(array('class' => 'Symfony_DI_PhpDumper_Test_UrlParameters')));
453+
454+
require self::$fixturesPath.'/php/services_url_env.php';
455+
$container = new \Symfony_DI_PhpDumper_Test_UrlParameters();
456+
$this->assertSame(array(
457+
'scheme' => 'postgres',
458+
'host' => 'localhost',
459+
'port' => 5432,
460+
'user' => 'user',
461+
'path' => 'database',
462+
'query' => 'sslmode=disable',
463+
'pass' => '',
464+
'fragment' => '',
465+
), $container->getParameter('hello'));
466+
}
467+
442468
public function testDumpedJsonEnvParameters()
443469
{
444470
$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_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)
0