Skip to content

Commit 64b8399

Browse files
committed
feature #1059 [make:reset-password] Translate exception reasons provided by ResetPasswordBundle (bocharsky-bw)
This PR was squashed before being merged into the 1.0-dev branch. Discussion ---------- [make:reset-password] Translate exception reasons provided by ResetPasswordBundle If there's `Symfony\Contracts\Translation\TranslatorInterface` service available in the project - Maker will generate a slightly different controller's code to inject translator service and translate exception reasons out of the box. Available after: SymfonyCasts/reset-password-bundle#206 Commits ------- b771178 [make:reset-password] Translate exception reasons provided by ResetPasswordBundle
2 parents c1a739e + b771178 commit 64b8399

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

src/Maker/MakeResetPassword.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@
4444
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
4545
use Symfony\Component\Routing\Annotation\Route;
4646
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
47+
use Symfony\Component\Translation\Translator;
4748
use Symfony\Component\Validator\Validation;
4849
use Symfony\Component\Yaml\Yaml;
50+
use Symfony\Contracts\Translation\TranslatorInterface;
4951
use SymfonyCasts\Bundle\ResetPassword\Controller\ResetPasswordControllerTrait;
5052
use SymfonyCasts\Bundle\ResetPassword\Exception\ResetPasswordExceptionInterface;
5153
use SymfonyCasts\Bundle\ResetPassword\Model\ResetPasswordRequestInterface;
@@ -236,6 +238,18 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
236238
EntityManagerInterface::class,
237239
];
238240

241+
// Namespace for ResetPasswordExceptionInterface was imported above
242+
$problemValidateMessageOrConstant = \defined('SymfonyCasts\Bundle\ResetPassword\Exception\ResetPasswordExceptionInterface::MESSAGE_PROBLEM_VALIDATE')
243+
? 'ResetPasswordExceptionInterface::MESSAGE_PROBLEM_VALIDATE'
244+
: "'There was a problem validating your password reset request'";
245+
$problemHandleMessageOrConstant = \defined('SymfonyCasts\Bundle\ResetPassword\Exception\ResetPasswordExceptionInterface::MESSAGE_PROBLEM_HANDLE')
246+
? 'ResetPasswordExceptionInterface::MESSAGE_PROBLEM_HANDLE'
247+
: "'There was a problem handling your password reset request'";
248+
249+
if ($isTranslatorAvailable = class_exists(Translator::class)) {
250+
$useStatements[] = TranslatorInterface::class;
251+
}
252+
239253
$generator->generateController(
240254
$controllerClassNameDetails->getFullName(),
241255
'resetPassword/ResetPasswordController.tpl.php',
@@ -253,6 +267,9 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
253267
'password_hasher_class_details' => ($passwordClassDetails = $generator->createClassNameDetails($passwordHasher, '\\')),
254268
'password_hasher_variable_name' => str_replace('Interface', '', sprintf('$%s', lcfirst($passwordClassDetails->getShortName()))), // @legacy see passwordHasher conditional above
255269
'use_password_hasher' => UserPasswordHasherInterface::class === $passwordHasher, // @legacy see passwordHasher conditional above
270+
'problem_validate_message_or_constant' => $problemValidateMessageOrConstant,
271+
'problem_handle_message_or_constant' => $problemHandleMessageOrConstant,
272+
'translator_available' => $isTranslatorAvailable,
256273
]
257274
);
258275

src/Resources/skeleton/resetPassword/ResetPasswordController.tpl.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,16 @@ public function __construct(ResetPasswordHelperInterface $resetPasswordHelper, E
3434
* @Route("", name="app_forgot_password_request")
3535
*/
3636
<?php } ?>
37-
public function request(Request $request, MailerInterface $mailer): Response
37+
public function request(Request $request, MailerInterface $mailer<?php if ($translator_available): ?>, TranslatorInterface $translator<?php endif ?>): Response
3838
{
3939
$form = $this->createForm(<?= $request_form_type_class_name ?>::class);
4040
$form->handleRequest($request);
4141

4242
if ($form->isSubmitted() && $form->isValid()) {
4343
return $this->processSendingPasswordResetEmail(
4444
$form->get('<?= $email_field ?>')->getData(),
45-
$mailer
45+
$mailer<?php if ($translator_available): ?>,
46+
$translator<?php endif ?><?= "\n" ?>
4647
);
4748
}
4849

@@ -84,7 +85,7 @@ public function checkEmail(): Response
8485
* @Route("/reset/{token}", name="app_reset_password")
8586
*/
8687
<?php } ?>
87-
public function reset(Request $request, <?= $password_hasher_class_details->getShortName() ?> <?= $password_hasher_variable_name ?>, string $token = null): Response
88+
public function reset(Request $request, <?= $password_hasher_class_details->getShortName() ?> <?= $password_hasher_variable_name ?><?php if ($translator_available): ?>, TranslatorInterface $translator<?php endif ?>, string $token = null): Response
8889
{
8990
if ($token) {
9091
// We store the token in session and remove it from the URL, to avoid the URL being
@@ -103,8 +104,9 @@ public function reset(Request $request, <?= $password_hasher_class_details->getS
103104
$user = $this->resetPasswordHelper->validateTokenAndFetchUser($token);
104105
} catch (ResetPasswordExceptionInterface $e) {
105106
$this->addFlash('reset_password_error', sprintf(
106-
'There was a problem validating your reset request - %s',
107-
$e->getReason()
107+
'%s - %s',
108+
<?php if ($translator_available): ?>$translator->trans(<?= $problem_validate_message_or_constant ?>, [], 'ResetPasswordBundle')<?php else: ?><?= $problem_validate_message_or_constant ?><?php endif ?>,
109+
<?php if ($translator_available): ?>$translator->trans($e->getReason(), [], 'ResetPasswordBundle')<?php else: ?>$e->getReason()<?php endif ?><?= "\n" ?>
108110
));
109111

110112
return $this->redirectToRoute('app_forgot_password_request');
@@ -138,7 +140,7 @@ public function reset(Request $request, <?= $password_hasher_class_details->getS
138140
]);
139141
}
140142

141-
private function processSendingPasswordResetEmail(string $emailFormData, MailerInterface $mailer): RedirectResponse
143+
private function processSendingPasswordResetEmail(string $emailFormData, MailerInterface $mailer<?php if ($translator_available): ?>, TranslatorInterface $translator<?php endif ?>): RedirectResponse
142144
{
143145
$user = $this->entityManager->getRepository(<?= $user_class_name ?>::class)->findOneBy([
144146
'<?= $email_field ?>' => $emailFormData,
@@ -157,8 +159,9 @@ private function processSendingPasswordResetEmail(string $emailFormData, MailerI
157159
// Caution: This may reveal if a user is registered or not.
158160
//
159161
// $this->addFlash('reset_password_error', sprintf(
160-
// 'There was a problem handling your password reset request - %s',
161-
// $e->getReason()
162+
// '%s - %s',
163+
// <?php if ($translator_available): ?>$translator->trans(<?= $problem_handle_message_or_constant ?>, [], 'ResetPasswordBundle')<?php else: ?><?= $problem_handle_message_or_constant ?><?php endif ?>,
164+
// <?php if ($translator_available): ?>$translator->trans($e->getReason(), [], 'ResetPasswordBundle')<?php else: ?>$e->getReason()<?php endif ?><?= "\n" ?>
162165
// ));
163166

164167
return $this->redirectToRoute('app_check_email');

tests/Maker/MakeResetPasswordTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,22 @@ public function getTestDetails()
7676
}),
7777
];
7878

79+
yield 'it_generates_with_translator_installed' => [$this->createResetPasswordTest()
80+
->addExtraDependencies('symfony/translation')
81+
->run(function (MakerTestRunner $runner) {
82+
$this->makeUser($runner);
83+
84+
$output = $runner->runMaker([
85+
'App\Entity\User',
86+
'app_home',
87+
'victor@symfonycasts.com',
88+
'SymfonyCasts',
89+
]);
90+
91+
$this->assertStringContainsString('Success', $output);
92+
}),
93+
];
94+
7995
yield 'it_generates_with_custom_config' => [$this->createResetPasswordTest()
8096
->run(function (MakerTestRunner $runner) {
8197
$runner->deleteFile('config/packages/reset_password.yaml');

0 commit comments

Comments
 (0)