-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DI] Allow fetching env vars with lookup-dedicated services #20276
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
Changes from all commits
685ff0e
6760127
3d54c3b
c396e8c
f534315
1b9619c
daa0251
72791d6
d1c754d
eeaea83
2a84059
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; | ||
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; | ||
use Symfony\Component\DependencyInjection\GetEnvInterface; | ||
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag; | ||
|
||
/** | ||
* Checks that all env-referenced services exist and implement GetEnvInterface. | ||
* | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
class CheckEnvReferencedServicesPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
$envReferencedServices = $container->getParameterBag() instanceof EnvPlaceholderParameterBag ? $container->getParameterBag()->getEnvReferencedServices() : array(); | ||
|
||
foreach ($envReferencedServices as $id) { | ||
if (!$container->has($id)) { | ||
throw new ServiceNotFoundException($id); | ||
} | ||
$class = $container->getDefinition($id)->getClass(); | ||
if (!is_subclass_of($class, GetEnvInterface::class)) { | ||
if (!class_exists($class, false)) { | ||
throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id)); | ||
} | ||
|
||
throw new InvalidArgumentException(sprintf('The service "%s" referenced in env parameters must implement "%s".', $id, GetEnvInterface::class)); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -443,7 +443,7 @@ protected function load($file) | |
* | ||
* @param string The name of the environment variable | ||
* | ||
* @return scalar The value to use for the provided environment variable name | ||
* @return mixed The value to use for the provided environment variable | ||
* | ||
* @throws EnvNotFoundException When the environment variable is not found and has no default value | ||
*/ | ||
|
@@ -458,6 +458,14 @@ protected function getEnv($name) | |
if (false !== $env = getenv($name)) { | ||
return $this->envCache[$name] = $env; | ||
} | ||
if (false !== $i = strpos($name, '@')) { | ||
$id = substr($name, 1 + $i); | ||
$service = isset($this->services[$id]) ? $this->services[$id] : (isset($this->methodMap[$id]) ? $this->{$this->methodMap[$id]}() : $this->get($id)); | ||
|
||
if (null !== $env = $service->getEnv(substr($name, 0, $i))) { | ||
return $this->envCache[$name] = $env; | ||
} | ||
} | ||
if (!$this->hasParameter("env($name)")) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldnt we use the normalized name here? And provide a default value regarding the lookup service? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By normalized name, you mean with lowercased service id? If yes, it's not required: parameters are already case insensitive in the base ParameterBag. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean the normalized key: env(FOO): default
env(FOO@service): default The first could work in all cases right? Making the second obsolete. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But these are really two separate variable identifiers. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right, lets stay true to the parameter architecture. (opposed to what im thinking towards;) parameters:
key: value
env:
FOO: bar |
||
throw new EnvNotFoundException($name); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection; | ||
|
||
/** | ||
* The GetEnvInterface is implemented by objects that manage environment-like variables. | ||
* | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
interface GetEnvInterface | ||
{ | ||
/** | ||
* Returns the value of the given variable as managed by the current instance. | ||
* | ||
* @param string $name The name of the variable | ||
* | ||
* @return mixed|null The value of the given variable or null when it is not found | ||
*/ | ||
public function getEnv($name); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection\Tests\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CheckEnvReferencedServicesPass; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\GetEnvInterface; | ||
|
||
class CheckEnvReferencedServicePassTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testProcess() | ||
{ | ||
$container = new ContainerBuilder(); | ||
$container->getParameterBag()->get('env(foo@a)'); | ||
$container->register('a', GetEnvService::class); | ||
|
||
$pass = new CheckEnvReferencedServicesPass(); | ||
$pass->process($container); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException | ||
*/ | ||
public function testProcessThrowsExceptionOnInvalidReference() | ||
{ | ||
$container = new ContainerBuilder(); | ||
$container->getParameterBag()->get('env(foo@a)'); | ||
|
||
$pass = new CheckEnvReferencedServicesPass(); | ||
$pass->process($container); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException | ||
*/ | ||
public function testProcessThrowsExceptionOnInvalidReferenceFromInlinedDefinition() | ||
{ | ||
$container = new ContainerBuilder(); | ||
$container->getParameterBag()->get('env(foo@a)'); | ||
$container->register('a', \stdClass::class); | ||
|
||
$pass = new CheckEnvReferencedServicesPass(); | ||
$pass->process($container); | ||
} | ||
} | ||
|
||
class GetEnvService implements GetEnvInterface | ||
{ | ||
public function getEnv($name) | ||
F438 { | ||
return $name.$name; | ||
} | ||
} |
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.
What about a default implementation of
GetEnvInterface
? and be explicit here (in terms of priority);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.
We already have a default here: it's the code surrounding the new block.
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.
I mean a
GetEnv
class for thegetenv/$_ENV
lookup.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.
Yep, that's what I thought, and that would require a class, which means a service, thus a service definition. But the container comes without any service preconfigured...
Uh oh!
There was an error while loading. Please reload this page.
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.
This is about if we should allow to override the lookup service by having
FOO@service
in our$_ENV
var for example. I've no strong opinion on this, but it could make sense to do:Ie. consistently rely on 1 lookup implementation.
Uh oh!
There was an error while loading. Please reload this page.
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 BEA4 a>.
Oh and shouldnt this be
strrpos
? Not sure ;-)edit: never mind, it's no expected value anyway.