From 481be92cfb0b113a6f3c5f5d826cbcdc40acbdd1 Mon Sep 17 00:00:00 2001 From: Ruud Kamphuis Date: Fri, 5 Aug 2022 10:21:48 +0200 Subject: [PATCH] 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: - https://github.com/symfony/symfony/issues/46643 - https://github.com/symfony/symfony/pull/46655 --- .../Component/DependencyInjection/CHANGELOG.md | 1 + .../DependencyInjection/Loader/FileLoader.php | 2 +- .../DependencyInjection/Loader/PhpFileLoader.php | 2 +- .../Tests/Fixtures/Prototype/Foo.php | 11 ++++++++++- 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/CHANGELOG.md b/src/Symfony/Component/DependencyInjection/CHANGELOG.md index 01330f55907d2..fe744789eaa50 100644 --- a/src/Symfony/Component/DependencyInjection/CHANGELOG.md +++ b/src/Symfony/Component/DependencyInjection/CHANGELOG.md @@ -9,6 +9,7 @@ CHANGELOG * Add `enum` env var processor * Add `shuffle` env var processor * Add `resolve-env` option to `debug:config` command to display actual values of environment variables in dumped configuration + * Allow #[When] to be extended 6.1 --- diff --git a/src/Symfony/Component/DependencyInjection/Loader/FileLoader.php b/src/Symfony/Component/DependencyInjection/Loader/FileLoader.php index 3a3e256e151be..9504c4cee664f 100644 --- a/src/Symfony/Component/DependencyInjection/Loader/FileLoader.php +++ b/src/Symfony/Component/DependencyInjection/Loader/FileLoader.php @@ -120,7 +120,7 @@ public function registerClasses(Definition $prototype, string $namespace, string if (null === $errorMessage && $autoconfigureAttributes && $this->env) { $r = $this->container->getReflectionClass($class); $attribute = null; - foreach ($r->getAttributes(When::class) as $attribute) { + foreach ($r->getAttributes(When::class, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) { if ($this->env === $attribute->newInstance()->env) { $attribute = null; break; diff --git a/src/Symfony/Component/DependencyInjection/Loader/PhpFileLoader.php b/src/Symfony/Component/DependencyInjection/Loader/PhpFileLoader.php index 629ca506845c6..7c279d19b241d 100644 --- a/src/Symfony/Component/DependencyInjection/Loader/PhpFileLoader.php +++ b/src/Symfony/Component/DependencyInjection/Loader/PhpFileLoader.php @@ -101,7 +101,7 @@ private function executeCallback(callable $callback, ContainerConfigurator $cont $r = new \ReflectionFunction($callback); $attribute = null; - foreach ($r->getAttributes(When::class) as $attribute) { + foreach ($r->getAttributes(When::class, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) { if ($this->env === $attribute->newInstance()->env) { $attribute = null; break; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/Prototype/Foo.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/Prototype/Foo.php index 49545a8dfa2e6..807c8b3e20086 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/Prototype/Foo.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/Prototype/Foo.php @@ -4,7 +4,7 @@ use Symfony\Component\DependencyInjection\Attribute\When; -#[When(env: 'prod')] +#[ProductionOnly] #[When(env: 'dev')] class Foo implements FooInterface, Sub\BarInterface { @@ -16,3 +16,12 @@ public function setFoo(self $foo) { } } + +#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::TARGET_FUNCTION | \Attribute::IS_REPEATABLE)] +class ProductionOnly extends When +{ + public function __construct() + { + parent::__construct('prod'); + } +} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy