8000 [DependencyInjection] Add `#[When(env: 'foo')]` to skip autoregistering a class when the env doesn't match by nicolas-grekas · Pull Request #40782 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DependencyInjection] Add #[When(env: 'foo')] to skip autoregistering a class when the env doesn't match #40782

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
Apr 17, 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
26 changes: 26 additions & 0 deletions src/Symfony/Component/DependencyInjection/Attribute/When.php
8000
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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\Attribute;

/**
* An attribute to tell under which environement this class should be registered as a service.
*
* @author Nicolas Grekas <p@tchwork.com>
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
class When
{
public function __construct(
public string $env,
) {
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ CHANGELOG
* Add support for per-env configuration in XML and Yaml loaders
* Add `ContainerBuilder::willBeAvailable()` to help with conditional configuration
* Add support an integer return value for default_index_method
* Add `#[When(env: 'foo')]` to skip autoregistering a class when the env doesn't match
* Add `env()` and `EnvConfigurator` in the PHP-DSL
* Add support for `ConfigBuilder` in the `PhpFileLoader`
* Add `ContainerConfigurator::env()` to get the current environment
Expand Down
18 changes: 17 additions & 1 deletion src/Symfony/Component/DependencyInjection/Loader/FileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\Config\Loader\FileLoader as BaseFileLoader;
use Symfony\Component\Config\Loader\Loader;
use Symfony\Component\Config\Resource\GlobResource;
use Symfony\Component\DependencyInjection\Attribute\When;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\RegisterAutoconfigureAttributesPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -98,11 +99,26 @@ public function registerClasses(Definition $prototype, string $namespace, string
}

$autoconfigureAttributes = new RegisterAutoconfigureAttributesPass();
$classes = $this->findClasses($namespace, $resource, (array) $exclude, $autoconfigureAttributes->accept($prototype) ? $autoconfigureAttributes : null);
$autoconfigureAttributes = $autoconfigureAttributes->accept($prototype) ? $autoconfigureAttributes : null;
$classes = $this->findClasses($namespace, $resource, (array) $exclude, $autoconfigureAttributes);
// prepare for deep cloning
$serializedPrototype = serialize($prototype);

foreach ($classes as $class => $errorMessage) {
if ($autoconfigureAttributes && $this->env) {
$r = $this->container->getReflectionClass($class);
$attribute = null;
foreach ($r->getAttributes(When::class) as $attribute) {
if ($this->env === $attribute->newInstance()->env) {
$attribute = null;
break;
}
}
if (null !== $attribute) {
continue;
}
}

if (interface_exists($class, false)) {
$this->interfaces[] = $class;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ public function load($resource, string $type = null)

$this->container->fileExists($path);

$this->loadXml($xml, $path);
$env = $this->env;
$this->env = null;
try {
$this->loadXml($xml, $path);
} finally {
$this->env = $env;
}

if ($this->env) {
$xpath = new \DOMXPath($xml);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,13 @@ public function load($resource, string $type = null)
return;
}

$this->loadContent($content, $path);
$env = $this->env;
$this->env = null;
try {
$this->loadContent($content, $path);
} finally {
$this->env = $env;
}

// per-env configuration
if ($this->env && isset($content['when@'.$this->env])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype;

use Symfony\Component\DependencyInjection\Attribute\When;

#[When(env: 'prod')]
#[When(env: 'dev')]
class Foo implements FooInterface, Sub\BarInterface
{
public function __construct($bar = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,27 @@ public function testRegisterClassesWithIncompatibleExclude()
'yaml/*'
);
}

/**
* @requires PHP 8
*
* @testWith ["prod", true]
* ["dev", true]
* ["bar", false]
* [null, true]
*/
public function testRegisterClassesWith 6064 WhenEnv(?string $env, bool $expected)
{
$container = new ContainerBuilder();
$loader = new TestFileLoader($container, new FileLocator(self::$fixturesPath.'/Fixtures'), $env);
$loader->registerClasses(
(new Definition())->setAutoconfigured(true),
'Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\\',
'Prototype/{Foo.php}'
);

$this->assertSame($expected, $container->has(Foo::class));
}
}

class TestFileLoader extends FileLoader
Expand Down
0