Skip to content

Commit 567098a

Browse files
committed
Allow extending When attribute
This allows people to extend the When attribute with something like this: ```php use Attribute; use Symfony\Component\DependencyInjection\Attribute\When; #[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::TARGET_FUNCTION | Attribute::IS_REPEATABLE)] final class Exclude extends When { public function __construct() { parent::__construct('never'); } } ``` Then they can use `#[Exclude]` instead of `#[When(env: 'never')]`. References: - symfony/symfony#46643 - symfony/symfony#46655
1 parent 9cf6e25 commit 567098a

File tree

4 files changed

+13
-3
lines changed

4 files changed

+13
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ CHANGELOG
99
* Add `enum` env var processor
1010
* Add `shuffle` env var processor
1111
* Add `resolve-env` option to `debug:config` command to display actual values of environment variables in dumped configuration
12+
* Allow #[When] to be extended
1213

1314
6.1
1415
---

Loader/FileLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public function registerClasses(Definition $prototype, string $namespace, string
120120
if (null === $errorMessage && $autoconfigureAttributes && $this->env) {
121121
$r = $this->container->getReflectionClass($class);
122122
$attribute = null;
123-
foreach ($r->getAttributes(When::class) as $attribute) {
123+
foreach ($r->getAttributes(When::class, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) {
124124
if ($this->env === $attribute->newInstance()->env) {
125125
$attribute = null;
126126
break;

Loader/PhpFileLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ private function executeCallback(callable $callback, ContainerConfigurator $cont
101101
$r = new \ReflectionFunction($callback);
102102

103103
$attribute = null;
104-
foreach ($r->getAttributes(When::class) as $attribute) {
104+
foreach ($r->getAttributes(When::class, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) {
105105
if ($this->env === $attribute->newInstance()->env) {
106106
$attribute = null;
107107
break;

Tests/Fixtures/Prototype/Foo.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Symfony\Component\DependencyInjection\Attribute\When;
66

7-
#[When(env: 'prod')]
7+
#[ProductionOnly]
88
#[When(env: 'dev')]
99
class Foo implements FooInterface, Sub\BarInterface
1010
{
@@ -16,3 +16,12 @@ public function setFoo(self $foo)
1616
{
1717
}
1818
}
19+
20+
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::TARGET_FUNCTION | \Attribute::IS_REPEATABLE)]
21+
class ProductionOnly extends When
22+
{
23+
public function __construct()
24+
{
25+
parent::__construct('prod');
26+
}
27+
}

0 commit comments

Comments
 (0)