From 9f906e46ff4e1d3666673fb27ba28cab4dcfdd3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Sun, 26 Nov 2023 20:55:01 +0100 Subject: [PATCH] Use `#[Autowire]` attribute instead of Yaml config --- config/services.yaml | 8 -------- src/Command/ListUsersCommand.php | 2 ++ src/Controller/UserController.php | 2 ++ src/EventSubscriber/CommentNotificationSubscriber.php | 2 ++ 4 files changed, 6 insertions(+), 8 deletions(-) diff --git a/config/services.yaml b/config/services.yaml index 32e3234fc..2a603ee10 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -17,7 +17,6 @@ services: bind: # defines the scalar arguments once and apply them to any service defined/created in this file string $locales: '%app_locales%' string $defaultLocale: '%locale%' - string $emailSender: '%app.notifications.email_sender%' # makes classes in src/ available to be used as services # this creates a service per class whose id is the fully-qualified class name @@ -27,10 +26,3 @@ services: - '../src/DependencyInjection/' - '../src/Entity/' - '../src/Kernel.php' - - # when the service definition only contains arguments, you can omit the - # 'arguments' key and define the arguments just below the service class - App\EventSubscriber\CommentNotificationSubscriber: - $sender: '%app.notifications.email_sender%' - - Symfony\Component\Security\Http\Logout\LogoutUrlGenerator: '@security.logout_url_generator' diff --git a/src/Command/ListUsersCommand.php b/src/Command/ListUsersCommand.php index 4c641db1c..9576a1ede 100644 --- a/src/Command/ListUsersCommand.php +++ b/src/Command/ListUsersCommand.php @@ -20,6 +20,7 @@ use Symfony\Component\Console\Output\BufferedOutput; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; +use Symfony\Component\DependencyInjection\Attribute\Autowire; use Symfony\Component\Mailer\MailerInterface; use Symfony\Component\Mime\Email; @@ -47,6 +48,7 @@ final class ListUsersCommand extends Command { public function __construct( private readonly MailerInterface $mailer, + #[Autowire('%app.notifications.email_sender%')] private readonly string $emailSender, private readonly UserRepository $users ) { diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index f36967b42..ab23d13c2 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -16,6 +16,7 @@ use App\Form\UserType; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\DependencyInjection\Attribute\Autowire; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; @@ -62,6 +63,7 @@ public function changePassword( #[CurrentUser] User $user, Request $request, EntityManagerInterface $entityManager, + #[Autowire('@security.logout_url_generator')] LogoutUrlGenerator $logoutUrlGenerator, ): Response { $form = $this->createForm(ChangePasswordType::class, $user); diff --git a/src/EventSubscriber/CommentNotificationSubscriber.php b/src/EventSubscriber/CommentNotificationSubscriber.php index d6abc2c2e..18558c348 100644 --- a/src/EventSubscriber/CommentNotificationSubscriber.php +++ b/src/EventSubscriber/CommentNotificationSubscriber.php @@ -14,6 +14,7 @@ use App\Entity\Post; use App\Entity\User; use App\Event\CommentCreatedEvent; +use Symfony\Component\DependencyInjection\Attribute\Autowire; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\Mailer\MailerInterface; use Symfony\Component\Mime\Email; @@ -31,6 +32,7 @@ public function __construct( private readonly MailerInterface $mailer, private readonly UrlGeneratorInterface $urlGenerator, private readonly TranslatorInterface $translator, + #[Autowire('%app.notifications.email_sender%')] private readonly string $sender ) { }