Skip to content

Commit 00ce9fb

Browse files
committed
Use #[Autowire] attribute instead of Yaml config
1 parent 15962e8 commit 00ce9fb

File tree

5 files changed

+9
-11
lines changed

5 files changed

+9
-11
lines changed

config/services.yaml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ services:
1414
_defaults:
1515
autowire: true # Automatically injects dependencies in your services.
1616
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
17-
bind: # defines the scalar arguments once and apply them to any service defined/created in this file
18-
string $locales: '%app_locales%'
19-
string $defaultLocale: '%locale%'
20-
string $emailSender: '%app.notifications.email_sender%'
2117

2218
# makes classes in src/ available to be used as services
2319
# this creates a service per class whose id is the fully-qualified class name
@@ -27,10 +23,3 @@ services:
2723
- '../src/DependencyInjection/'
2824
- '../src/Entity/'
2925
- '../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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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
}

src/EventSubscriber/RedirectToPreferredLocaleSubscriber.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace App\EventSubscriber;
1313

14+
use Symfony\Component\DependencyInjection\Attribute\Autowire;
1415
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1516
use Symfony\Component\HttpFoundation\RedirectResponse;
1617
use Symfony\Component\HttpKernel\Event\RequestEvent;
@@ -36,7 +37,9 @@ final class RedirectToPreferredLocaleSubscriber implements EventSubscriberInterf
3637

3738
public function __construct(
3839
private readonly UrlGeneratorInterface $urlGenerator,
40+
#[Autowire('%app_locales%')]
3941
string $locales,
42+
#[Autowire('%locale%')]
4043
string $defaultLocale = null
4144
) {
4245
$this->locales = explode('|', trim($locales));

0 commit comments

Comments
 (0)