Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use #[Autowire] attribute instead of Yaml config #1461

Merged
merged 1 commit into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions config/services.yaml
Original file line number Diff line number Diff line change
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%'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would do it only for this one as ultra specific param
other are more generic

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I reverted the 2 first.


# 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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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')]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need this? We're using the default logout URL generator service, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The question is whether Symfony makes it auto-wirable

Copy link
Member Author

@GromNaN GromNaN Nov 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's needed.

Cannot autowire argument $logoutUrlGenerator of "App\Controller\UserController::changePassword()": it references class "Symfony\Component\Security\Http\Logout\LogoutUrlGenerator" but no such service exists. You should maybe alias this class to the existing "security.logout_url_generator" service.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for checking this 🙏

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened a PR symfony/symfony#52833

LogoutUrlGenerator $logoutUrlGenerator,
): Response {
$form = $this->createForm(ChangePasswordType::class, $user);
Expand Down
2 changes: 2 additions & 0 deletions src/EventSubscriber/CommentNotificationSubscriber.php
Original file line number Diff line number Diff line change
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