Skip to content

[DependencyInjection] allow PHP-DSL files to be env-conditional #41182

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 1 commit into from
May 12, 2021
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* @author Nicolas Grekas <p@tchwork.com>
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
class When
{
public function __construct(
Expand Down
23 changes: 19 additions & 4 deletions src/Symfony/Component/DependencyInjection/Loader/PhpFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Config\Builder\ConfigBuilderGeneratorInterface;
use Symfony\Component\Config\Builder\ConfigBuilderInterface;
use Symfony\Component\Config\FileLocatorInterface;
use Symfony\Component\DependencyInjection\Attribute\When;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
Expand Down Expand Up @@ -55,12 +56,12 @@ public function load($resource, string $type = null)
$this->container->fileExists($path);

// the closure forbids access to the private scope in the included file
$load = \Closure::bind(function ($path) use ($container, $loader, $resource, $type) {
$load = \Closure::bind(function ($path, $env) use ($container, $loader, $resource, $type) {
return include $path;
}, $this, ProtectedPhpFileLoader::class);

try {
$callback = $load($path);
$callback = $load($path, $this->env);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to inject then $env? Or should we just do the PHP8 attributes?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need it for ppl on PHP < 7 at least yes;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Symfony 4.0 we have the solution to use config/packages/dev/acme.php.

Copy link
Member Author

@nicolas-grekas nicolas-grekas May 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, but that's unrelated feature. Also, we already inject a few variables in the scope. Adding $env next to them just makes sense.


if (\is_object($callback) && \is_callable($callback)) {
$this->executeCallback($callback, new ContainerConfigurator($this->container, $this, $this->instanceof, $path, $resource, $this->env), $path);
Expand Down Expand Up @@ -98,8 +99,22 @@ private function executeCallback(callable $callback, ContainerConfigurator $cont

$arguments = [];
$configBuilders = [];
$parameters = (new \ReflectionFunction($callback))->getParameters();
foreach ($parameters as $parameter) {
$r = new \ReflectionFunction($callback);

if (\PHP_VERSION_ID >= 80000) {
$attribute = null;
foreach ($r->getAttributes(When::class) as $attribute) {
if ($this->env === $attribute->newInstance()->env) {
$attribute = null;
break;
}
}
if (null !== $attribute) {
return;
}
}

foreach ($r->getParameters() as $parameter) {
$reflectionType = $parameter->getType();
if (!$reflectionType instanceof \ReflectionNamedType) {
throw new \InvalidArgumentException(sprintf('Could not resolve argument "$%s" for "%s". You must typehint it (for example with "%s" or "%s").', $parameter->getName(), $path, ContainerConfigurator::class, ContainerBuilder::class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

use Symfony\Component\DependencyInjection\Tests\Fixtures\AcmeConfig;

return static function (AcmeConfig $config) {
if ('prod' !== $env) {
return;
}

return function (AcmeConfig $config) {
$config->color('blue');
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

return static function (ContainerConfigurator $configurator): void {
return function (ContainerConfigurator $configurator): void {
$services = $configurator->services();

$services
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

use Symfony\Config\AcmeConfig\NestedConfig;

return static function (NestedConfig $config) {
return function (NestedConfig $config) {
throw new RuntimeException('This code should not be run.');
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

use Symfony\Component\DependencyInjection\Attribute\When;

return #[When(env: 'prod')] function () {
throw new RuntimeException('This code should not be run.');
};
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,16 @@ public function testNestedBundleConfigNotAllowed()

$loader->load($fixtures.'/config/nested_bundle_config.php');
}

/**
* @requires PHP 8
*/
public function testWhenEnv()
{
$fixtures = realpath(__DIR__.'/../Fixtures');
$container = new ContainerBuilder();
$loader = new PhpFileLoader($container, new FileLocator(), 'dev', new ConfigBuilderGenerator(sys_get_temp_dir()));

$loader->load($fixtures.'/config/when_env.php');
}
}