Skip to content

Commit 78e116b

Browse files
committed
feature #1461 Use #[Autowire] attribute instead of Yaml config (GromNaN)
This PR was squashed before being merged into the main branch. Discussion ---------- Use `#[Autowire]` attribute instead of Yaml config I'm not sure this is something you want to do: replacing Yaml config for attribute for class alias, to use the `#[Autowire]` attribute available [since Symfony 6.1](https://symfony.com/blog/new-in-symfony-6-1-service-autowiring-attributes). I did the change, so let you decide if that's good or not for the demo. Commits ------- 9f906e4 Use `#[Autowire]` attribute instead of Yaml config
2 parents 72040e7 + 9f906e4 commit 78e116b

File tree

4 files changed

+6
-8
lines changed

4 files changed

+6
-8
lines changed

config/services.yaml

-8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ services:
1717
bind: # defines the scalar arguments once and apply them to any service defined/created in this file
1818
string $locales: '%app_locales%'
1919
string $defaultLocale: '%locale%'
20-
string $emailSender: '%app.notifications.email_sender%'
2120

2221
# makes classes in src/ available to be used as services
2322
# this creates a service per class whose id is the fully-qualified class name
@@ -27,10 +26,3 @@ services:
2726
- '../src/DependencyInjection/'
2827
- '../src/Entity/'
2928
- '../src/Kernel.php'
30-
31-
# when the service definition only contains arguments, you can omit the
32-
# 'arguments' key and define the arguments just below the service class
33-
App\EventSubscriber\CommentNotificationSubscriber:
34-
$sender: '%app.notifications.email_sender%'
35-
36-
Symfony\Component\Security\Http\Logout\LogoutUrlGenerator: '@security.logout_url_generator'

src/Command/ListUsersCommand.php

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\Console\Output\BufferedOutput;
2121
use Symfony\Component\Console\Output\OutputInterface;
2222
use Symfony\Component\Console\Style\SymfonyStyle;
23+
use Symfony\Component\DependencyInjection\Attribute\Autowire;
2324
use Symfony\Component\Mailer\MailerInterface;
2425
use Symfony\Component\Mime\Email;
2526

@@ -47,6 +48,7 @@ final class ListUsersCommand extends Command
4748
{
4849
public function __construct(
4950
private readonly MailerInterface $mailer,
51+
#[Autowire('%app.notifications.email_sender%')]
5052
private readonly string $emailSender,
5153
private readonly UserRepository $users
5254
) {

src/Controller/UserController.php

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use App\Form\UserType;
1717
use Doctrine\ORM\EntityManagerInterface;
1818
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
19+
use Symfony\Component\DependencyInjection\Attribute\Autowire;
1920
use Symfony\Component\HttpFoundation\Request;
2021
use Symfony\Component\HttpFoundation\Response;
2122
use Symfony\Component\Routing\Annotation\Route;
@@ -62,6 +63,7 @@ public function changePassword(
6263
#[CurrentUser] User $user,
6364
Request $request,
6465
EntityManagerInterface $entityManager,
66+
#[Autowire('@security.logout_url_generator')]
6567
LogoutUrlGenerator $logoutUrlGenerator,
6668
): Response {
6769
$form = $this->createForm(ChangePasswordType::class, $user);

src/EventSubscriber/CommentNotificationSubscriber.php

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use App\Entity\Post;
1515
use App\Entity\User;
1616
use App\Event\CommentCreatedEvent;
17+
use Symfony\Component\DependencyInjection\Attribute\Autowire;
1718
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1819
use Symfony\Component\Mailer\MailerInterface;
1920
use Symfony\Component\Mime\Email;
@@ -31,6 +32,7 @@ public function __construct(
3132
private readonly MailerInterface $mailer,
3233
private readonly UrlGeneratorInterface $urlGenerator,
3334
private readonly TranslatorInterface $translator,
35+
#[Autowire('%app.notifications.email_sender%')]
3436
private readonly string $sender
3537
) {
3638
}

0 commit comments

Comments
 (0)