Skip to content

Commit fbeee59

Browse files
authored
Merge pull request #1076 from bocharsky-bw/verify-email-bundle-translate-reasons
[make:registration-form] Translate reasons for VerifyEmailBundle if translator available
2 parents ded06ef + 3bb58de commit fbeee59

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
@@ -183,6 +183,37 @@ public function getTestDetails()
183183
$this->runRegistrationTest($runner, 'it_generates_registration_form_with_verification.php');
184184
}),
185185
];
186+
187+
yield 'it_generates_registration_form_with_verification_and_translator' => [$this->createRegistrationFormTest()
188+
->setRequiredPhpVersion(70200)
189+
->addExtraDependencies('symfonycasts/verify-email-bundle')
190+
// needed for internal functional test
191+
->addExtraDependencies('symfony/web-profiler-bundle', 'mailer', 'symfony/translation')
192+
->run(function (MakerTestRunner $runner) {
193+
$runner->writeFile(
194+
'config/packages/mailer.yaml',
195+
Yaml::dump(['framework' => [
196+
'mailer' => ['dsn' => 'null://null'],
197+
]])
198+
);
199+
200+
$this->makeUser($runner);
201+
202+
$output = $runner->runMaker([
203+
'n', // add UniqueEntity
204+
'y', // verify user
205+
'y', // require authentication to verify user email
206+
'victor@symfonycasts.com', // from email address
207+
'SymfonyCasts', // From Name
208+
'n', // no authenticate after
209+
0, // route number to redirect to
210+
]);
211+
212+
$this->assertStringContainsString('Success', $output);
213+
214+
$this->runRegistrationTest($runner, 'it_generates_registration_form_with_verification.php');
215+
}),
216+
];
186217
}
187218

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

0 commit comments

Comments
 (0)