Skip to content

Commit 3bb58de

Browse files
committed
Translate reasons for VerifyEmailBundle if translator available
1 parent bff3fbb commit 3bb58de

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

src/Maker/MakeRegistrationForm.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@
5050
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
5151
use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
5252
use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
53+
use Symfony\Component\Translation\Translator;
5354
use Symfony\Component\Validator\Validation;
55+
use Symfony\Contracts\Translation\TranslatorInterface;
5456
use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
5557
use SymfonyCasts\Bundle\VerifyEmail\Model\VerifyEmailSignatureComponents;
5658
use SymfonyCasts\Bundle\VerifyEmail\SymfonyCastsVerifyEmailBundle;
@@ -324,6 +326,10 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
324326
}
325327
}
326328

329+
if ($isTranslatorAvailable = class_exists(Translator::class)) {
330+
$useStatements[] = TranslatorInterface::class;
331+
}
332+
327333
$generator->generateController(
328334
$controllerClassNameDetails->getFullName(),
329335
'registration/RegistrationController.tpl.php',
@@ -348,6 +354,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
348354
'password_hasher_class_details' => ($passwordClassDetails = $generator->createClassNameDetails($passwordHasher, '\\')),
349355
'password_hasher_variable_name' => str_replace('Interface', '', sprintf('$%s', lcfirst($passwordClassDetails->getShortName()))), // @legacy see passwordHasher conditional above
350356
'use_password_hasher' => UserPasswordHasherInterface::class === $passwordHasher, // @legacy see passwordHasher conditional above
357+
'translator_available' => $isTranslatorAvailable,
351358
],
352359
$userRepoVars
353360
)

src/Resources/skeleton/registration/RegistrationController.tpl.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function register(Request $request, <?= $password_hasher_class_details->g
7373
<?php if ($will_verify_email): ?>
7474

7575
<?= $generator->generateRouteForControllerMethod('/verify/email', 'app_verify_email') ?>
76-
public function verifyUserEmail(Request $request<?= $verify_email_anonymously ? sprintf(', %s %s', $repository_class_name, $repository_var) : null ?>): Response
76+
public function verifyUserEmail(Request $request<?php if ($translator_available): ?>, TranslatorInterface $translator<?php endif ?><?= $verify_email_anonymously ? sprintf(', %s %s', $repository_class_name, $repository_var) : null ?>): Response
7777
{
7878
<?php if (!$verify_email_anonymously): ?>
7979
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
@@ -101,7 +101,7 @@ public function verifyUserEmail(Request $request<?= $verify_email_anonymously ?
101101
try {
102102
$this->emailVerifier->handleEmailConfirmation($request, <?= $verify_email_anonymously ? '$user' : '$this->getUser()' ?>);
103103
} catch (VerifyEmailExceptionInterface $exception) {
104-
$this->addFlash('verify_email_error', $exception->getReason());
104+
$this->addFlash('verify_email_error', <?php if ($translator_available): ?>$translator->trans($exception->getReason(), [], 'VerifyEmailBundle')<?php else: ?>$exception->getReason()<?php endif ?>);
105105

106106
return $this->redirectToRoute('<?= $route_name ?>');
107107
}

tests/Maker/MakeRegistrationFormTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,37 @@ public function getTestDetails()
165165
$this->runRegistrationTest($runner, 'it_generates_registration_form_with_verification.php');
166166
}),
167167
];
168+
169+
yield 'it_generates_registration_form_with_verification_and_translator' => [$this->createRegistrationFormTest()
170+
->setRequiredPhpVersion(70200)
171+
->addExtraDependencies('symfonycasts/verify-email-bundle')
172+
// needed for internal functional test
173+
->addExtraDependencies('symfony/web-profiler-bundle', 'mailer', 'symfony/translation')
174+
->run(function (MakerTestRunner $runner) {
175+
$runner->writeFile(
176+
'config/packages/mailer.yaml',
177+
Yaml::dump(['framework' => [
178+
'mailer' => ['dsn' => 'null://null'],
179+
]])
180+
);
181+
182+
$this->makeUser($runner);
183+
184+
$output = $runner->runMaker([
185+
'n', // add UniqueEntity
186+
'y', // verify user
187+
'y', // require authentication to verify user email
188+
'victor@symfonycasts.com', // from email address
189+
'SymfonyCasts', // From Name
190+
'n', // no authenticate after
191+
0, // route number to redirect to
192+
]);
193+
194+
$this->assertStringContainsString('Success', $output);
195+
196+
$this->runRegistrationTest($runner, 'it_generates_registration_form_with_verification.php');
197+
}),
198+
];
168199
}
169200

170201
private function makeUser(MakerTestRunner $runner, string $identifier = 'email')

0 commit comments

Comments
 (0)