[DependencyInjection] inject $env in the scope of PHP-DSL files
This commit is contained in:
parent
31bd00e6d7
commit
29173a91d1
@ -16,7 +16,7 @@ namespace Symfony\Component\DependencyInjection\Attribute;
|
|||||||
*
|
*
|
||||||
* @author Nicolas Grekas <p@tchwork.com>
|
* @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
|
class When
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
|
@ -15,6 +15,7 @@ use Symfony\Component\Config\Builder\ConfigBuilderGenerator;
|
|||||||
use Symfony\Component\Config\Builder\ConfigBuilderGeneratorInterface;
|
use Symfony\Component\Config\Builder\ConfigBuilderGeneratorInterface;
|
||||||
use Symfony\Component\Config\Builder\ConfigBuilderInterface;
|
use Symfony\Component\Config\Builder\ConfigBuilderInterface;
|
||||||
use Symfony\Component\Config\FileLocatorInterface;
|
use Symfony\Component\Config\FileLocatorInterface;
|
||||||
|
use Symfony\Component\DependencyInjection\Attribute\When;
|
||||||
use Symfony\Component\DependencyInjection\Container;
|
use Symfony\Component\DependencyInjection\Container;
|
||||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
||||||
@ -55,12 +56,12 @@ class PhpFileLoader extends FileLoader
|
|||||||
$this->container->fileExists($path);
|
$this->container->fileExists($path);
|
||||||
|
|
||||||
// the closure forbids access to the private scope in the included file
|
// 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;
|
return include $path;
|
||||||
}, $this, ProtectedPhpFileLoader::class);
|
}, $this, ProtectedPhpFileLoader::class);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$callback = $load($path);
|
$callback = $load($path, $this->env);
|
||||||
|
|
||||||
if (\is_object($callback) && \is_callable($callback)) {
|
if (\is_object($callback) && \is_callable($callback)) {
|
||||||
$this->executeCallback($callback, new ContainerConfigurator($this->container, $this, $this->instanceof, $path, $resource, $this->env), $path);
|
$this->executeCallback($callback, new ContainerConfigurator($this->container, $this, $this->instanceof, $path, $resource, $this->env), $path);
|
||||||
@ -98,8 +99,22 @@ class PhpFileLoader extends FileLoader
|
|||||||
|
|
||||||
$arguments = [];
|
$arguments = [];
|
||||||
$configBuilders = [];
|
$configBuilders = [];
|
||||||
$parameters = (new \ReflectionFunction($callback))->getParameters();
|
$r = new \ReflectionFunction($callback);
|
||||||
foreach ($parameters as $parameter) {
|
|
||||||
|
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();
|
$reflectionType = $parameter->getType();
|
||||||
if (!$reflectionType instanceof \ReflectionNamedType) {
|
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));
|
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));
|
||||||
|
@ -2,6 +2,10 @@
|
|||||||
|
|
||||||
use Symfony\Component\DependencyInjection\Tests\Fixtures\AcmeConfig;
|
use Symfony\Component\DependencyInjection\Tests\Fixtures\AcmeConfig;
|
||||||
|
|
||||||
return static function (AcmeConfig $config) {
|
if ('prod' !== $env) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
return function (AcmeConfig $config) {
|
||||||
$config->color('blue');
|
$config->color('blue');
|
||||||
};
|
};
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
|
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
|
||||||
|
|
||||||
return static function (ContainerConfigurator $configurator): void {
|
return function (ContainerConfigurator $configurator): void {
|
||||||
$services = $configurator->services();
|
$services = $configurator->services();
|
||||||
|
|
||||||
$services
|
$services
|
||||||
|
@ -2,6 +2,6 @@
|
|||||||
|
|
||||||
use Symfony\Config\AcmeConfig\NestedConfig;
|
use Symfony\Config\AcmeConfig\NestedConfig;
|
||||||
|
|
||||||
return static function (NestedConfig $config) {
|
return function (NestedConfig $config) {
|
||||||
throw new RuntimeException('This code should not be run.');
|
throw new RuntimeException('This code should not be run.');
|
||||||
};
|
};
|
||||||
|
@ -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.');
|
||||||
|
};
|
@ -176,4 +176,16 @@ class PhpFileLoaderTest extends TestCase
|
|||||||
|
|
||||||
$loader->load($fixtures.'/config/nested_bundle_config.php');
|
$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');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user