Skip to content

Commit

Permalink
feature #1461 Use #[Autowire] attribute instead of Yaml config (Gro…
Browse files Browse the repository at this point in the history
…mNaN)

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
  • Loading branch information
javiereguiluz committed Dec 1, 2023
2 parents 72040e7 + 9f906e4 commit 78e116b
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 8 deletions.
8 changes: 0 additions & 8 deletions config/services.yaml
Expand Up @@ -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
Expand All @@ -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'
2 changes: 2 additions & 0 deletions src/Command/ListUsersCommand.php
Expand Up @@ -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;

Expand Down Expand Up @@ -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
) {
Expand Down
2 changes: 2 additions & 0 deletions src/Controller/UserController.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/EventSubscriber/CommentNotificationSubscriber.php
Expand Up @@ -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;
Expand All @@ -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
) {
}
Expand Down

0 comments on commit 78e116b

Please sign in to comment.