Skip to content

Conversation

Jean-Beru
Copy link
Contributor

@Jean-Beru Jean-Beru commented Sep 1, 2025

Q A
Branch? 7.4
Bug fix? no
New feature? yes
Deprecations? no
Issues
License MIT

Make #[AsDecorator] attribute repeatable to allow decorating multiple services at once.

Example for a simple HttpClient logger:

#[AsDecorator('api1.client')]
#[AsDecorator('api2.client')]
#[AsDecorator('api3.client')]
class LoggableService implements HttpClientInterface
{
    use DecoratorTrait;

    public function __construct(
        private HttpClientInterface $client,
        private readonly LoggerInterface $logger,
    ) {
    }

    public function request(string $method, string $url, array $options = []): ResponseInterface
    {
        try {
            $response = $this->client->request($method, $url, $options);

            $this->logger->info('API call: {method} {url}.', ['method'= > $method, 'url' => $url]);

            return $response;
        } catch (\Throwable $e) {
            $this->logger->error('API call failed: {method} {url}.', ['method'= > $method, 'url' => $url, 'exception' => $e]);

            throw $exception;
        }
    }
}

@carsonbot

This comment has been minimized.

@Jean-Beru Jean-Beru force-pushed the repeatable-asdecorator-attribute branch from f85a598 to 90f5b52 Compare September 1, 2025 11:34
@OskarStark OskarStark changed the title [DependencyInjection] Allow multiple #[AsDecorator] attributes [DependencyInjection] Allow multiple #[AsDecorator] attributes Sep 1, 2025
@fabpot fabpot force-pushed the repeatable-asdecorator-attribute branch from a766b4d to 3986c94 Compare September 3, 2025 05:36
@fabpot
Copy link
Member

fabpot commented Sep 3, 2025

Thank you @Jean-Beru.

@fabpot fabpot merged commit 18d7668 into symfony:7.4 Sep 3, 2025
9 of 12 checks passed
@Jean-Beru Jean-Beru deleted the repeatable-asdecorator-attribute branch September 3, 2025 07:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants