Skip to content

Commit 136ac74

Browse files
committed
feature #725 php8 attributes support for generated controllers (jrushlow)
This PR was squashed before being merged into the 1.0-dev branch. Discussion ---------- php8 attributes support for generated controllers When using PHP8, Maker Bundle uses route attributes in the generated controllers instead of annotations. - make:controller - make:crud - make:registration-form - make:reset-password As Doctrine/ORM does not currently have PHP8 support, we are unable to test the route functionality for `crud`, `registration-form`, & `reset-password`. No additional tests should be needed when Doctrine is PHP8 compatible. Commits ------- 75532b5 php8 attributes support for generated controllers
2 parents b5ca7c6 + 75532b5 commit 136ac74

File tree

9 files changed

+64
-0
lines changed

9 files changed

+64
-0
lines changed

src/Maker/AbstractMaker.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,9 @@ protected function addDependencies(array $dependencies, string $message = null):
4848
$message
4949
);
5050
}
51+
52+
final protected function useAttributes(): bool
53+
{
54+
return \PHP_VERSION_ID >= 80000;
55+
}
5156
}

src/Maker/MakeController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
5858
$controllerClassNameDetails->getFullName(),
5959
'controller/Controller.tpl.php',
6060
[
61+
'use_attributes' => $this->useAttributes(),
6162
'route_path' => Str::asRoutePath($controllerClassNameDetails->getRelativeNameWithoutSuffix()),
6263
'route_name' => Str::asRouteName($controllerClassNameDetails->getRelativeNameWithoutSuffix()),
6364
'with_template' => $this->isTwigInstalled() && !$noTemplate,

src/Maker/MakeCrud.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
143143
$controllerClassDetails->getFullName(),
144144
'crud/controller/Controller.tpl.php',
145145
array_merge([
146+
'use_attributes' => $this->useAttributes(),
146147
'entity_full_class_name' => $entityClassDetails->getFullName(),
147148
'entity_class_name' => $entityClassDetails->getShortName(),
148149
'form_full_class_name' => $formClassDetails->getFullName(),

src/Maker/MakeRegistrationForm.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
258258
$controllerClassNameDetails->getFullName(),
259259
'registration/RegistrationController.tpl.php',
260260
[
261+
'use_attributes' => $this->useAttributes(),
261262
'route_path' => '/register',
262263
'route_name' => 'app_register',
263264
'form_class_name' => $formClassDetails->getShortName(),

src/Maker/MakeResetPassword.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
197197
$controllerClassNameDetails->getFullName(),
198198
'resetPassword/ResetPasswordController.tpl.php',
199199
[
200+
'use_attributes' => $this->useAttributes(),
200201
'user_full_class_name' => $userClassNameDetails->getFullName(),
201202
'user_class_name' => $userClassNameDetails->getShortName(),
202203
'request_form_type_full_class_name' => $requestFormTypeClassNameDetails->getFullName(),

src/Resources/skeleton/controller/Controller.tpl.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@
88

99
class <?= $class_name; ?> extends <?= $parent_class_name; ?><?= "\n" ?>
1010
{
11+
<?php if ($use_attributes) { ?>
12+
#[Route('<?= $route_path ?>', name: '<?= $route_name ?>')]
13+
<?php } else { ?>
1114
/**
1215
* @Route("<?= $route_path ?>", name="<?= $route_name ?>")
1316
*/
17+
<?php } ?>
1418
public function index(): Response
1519
{
1620
<?php if ($with_template) { ?>

src/Resources/skeleton/crud/controller/Controller.tpl.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,22 @@
1212
use Symfony\Component\HttpFoundation\Response;
1313
use Symfony\Component\Routing\Annotation\Route;
1414

15+
<?php if ($use_attributes) { ?>
16+
#[Route('<?= $route_path ?>')]
17+
<?php } else { ?>
1518
/**
1619
* @Route("<?= $route_path ?>")
1720
*/
21+
<?php } ?>
1822
class <?= $class_name ?> extends <?= $parent_class_name; ?><?= "\n" ?>
1923
{
24+
<?php if ($use_attributes) { ?>
25+
#[Route('/', name: '<?= $route_name ?>_index', methods: ['GET'])]
26+
<?php } else { ?>
2027
/**
2128
* @Route("/", name="<?= $route_name ?>_index", methods={"GET"})
2229
*/
30+
<?php } ?>
2331
<?php if (isset($repository_full_class_name)): ?>
2432
public function index(<?= $repository_class_name ?> $<?= $repository_var ?>): Response
2533
{
@@ -40,9 +48,13 @@ public function index(): Response
4048
}
4149
<?php endif ?>
4250

51+
<?php if ($use_attributes) { ?>
52+
#[Route('/new', name: '<?= $route_name ?>_new', methods: ['GET', 'POST'])]
53+
<?php } else { ?>
4354
/**
4455
* @Route("/new", name="<?= $route_name ?>_new", methods={"GET","POST"})
4556
*/
57+
<?php } ?>
4658
public function new(Request $request): Response
4759
{
4860
$<?= $entity_var_singular ?> = new <?= $entity_class_name ?>();
@@ -63,19 +75,27 @@ public function new(Request $request): Response
6375
]);
6476
}
6577

78+
<?php if ($use_attributes) { ?>
79+
#[Route('/{<?= $entity_identifier ?>}', name: '<?= $route_name ?>_show', methods: ['GET'])]
80+
<?php } else { ?>
6681
/**
6782
* @Route("/{<?= $entity_identifier ?>}", name="<?= $route_name ?>_show", methods={"GET"})
6883
*/
84+
<?php } ?>
6985
public function show(<?= $entity_class_name ?> $<?= $entity_var_singular ?>): Response
7086
{
7187
return $this->render('<?= $templates_path ?>/show.html.twig', [
7288
'<?= $entity_twig_var_singular ?>' => $<?= $entity_var_singular ?>,
7389
]);
7490
}
7591

92+
<?php if ($use_attributes) { ?>
93+
#[Route('/{<?= $entity_identifier ?>}/edit', name: '<?= $route_name ?>_edit', methods: ['GET', 'POST'])]
94+
<?php } else { ?>
7695
/**
7796
* @Route("/{<?= $entity_identifier ?>}/edit", name="<?= $route_name ?>_edit", methods={"GET","POST"})
7897
*/
98+
<?php } ?>
7999
public function edit(Request $request, <?= $entity_class_name ?> $<?= $entity_var_singular ?>): Response
80100
{
81101
$form = $this->createForm(<?= $form_class_name ?>::class, $<?= $entity_var_singular ?>);
@@ -93,9 +113,13 @@ public function edit(Request $request, <?= $entity_class_name ?> $<?= $entity_va
93113
]);
94114
}
95115

116+
<?php if ($use_attributes) { ?>
117+
#[Route('/{<?= $entity_identifier ?>}', name: '<?= $route_name ?>_delete', methods: ['DELETE'])]
118+
<?php } else { ?>
96119
/**
97120
* @Route("/{<?= $entity_identifier ?>}", name="<?= $route_name ?>_delete", methods={"DELETE"})
98121
*/
122+
<?php } ?>
99123
public function delete(Request $request, <?= $entity_class_name ?> $<?= $entity_var_singular ?>): Response
100124
{
101125
if ($this->isCsrfTokenValid('delete'.$<?= $entity_var_singular ?>->get<?= ucfirst($entity_identifier) ?>(), $request->request->get('_token'))) {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,13 @@ public function __construct(EmailVerifier $emailVerifier)
3939
}
4040

4141
<?php endif; ?>
42+
<?php if ($use_attributes) { ?>
43+
#[Route('<?= $route_path ?>', name: '<?= $route_name ?>')]
44+
<?php } else { ?>
4245
/**
4346
* @Route("<?= $route_path ?>", name="<?= $route_name ?>")
4447
*/
48+
<?php } ?>
4549
public function register(Request $request, UserPasswordEncoderInterface $passwordEncoder<?= $authenticator_full_class_name ? sprintf(', GuardAuthenticatorHandler $guardHandler, %s $authenticator', $authenticator_class_name) : '' ?>): Response
4650
{
4751
$user = new <?= $user_class_name ?>();
@@ -91,9 +95,13 @@ public function register(Request $request, UserPasswordEncoderInterface $passwor
9195
}
9296
<?php if ($will_verify_email): ?>
9397

98+
<?php if ($use_attributes) { ?>
99+
#[Route('/verify/email', name: 'app_verify_email')]
100+
<?php } else { ?>
94101
/**
95102
* @Route("/verify/email", name="app_verify_email")
96103
*/
104+
<?php } ?>
97105
public function verifyUserEmail(Request $request): Response
98106
{
99107
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@
1818
use SymfonyCasts\Bundle\ResetPassword\Exception\ResetPasswordExceptionInterface;
1919
use SymfonyCasts\Bundle\ResetPassword\ResetPasswordHelperInterface;
2020

21+
<?php if ($use_attributes) { ?>
22+
#[Route('/reset-password')]
23+
<?php } else { ?>
2124
/**
2225
* @Route("/reset-password")
2326
*/
27+
<?php } ?>
2428
class <?= $class_name ?> extends AbstractController
2529
{
2630
use ResetPasswordControllerTrait;
@@ -35,8 +39,13 @@ public function __construct(ResetPasswordHelperInterface $resetPasswordHelper)
3539
/**
3640
* Display & process form to request a password reset.
3741
*
42+
<?php if ($use_attributes) { ?>
43+
*/
44+
#[Route('', name: 'app_forgot_password_request')]
45+
<?php } else { ?>
3846
* @Route("", name="app_forgot_password_request")
3947
*/
48+
<?php } ?>
4049
public function request(Request $request, MailerInterface $mailer): Response
4150
{
4251
$form = $this->createForm(<?= $request_form_type_class_name ?>::class);
@@ -57,8 +66,13 @@ public function request(Request $request, MailerInterface $mailer): Response
5766
/**
5867
* Confirmation page after a user has requested a password reset.
5968
*
69+
<?php if ($use_attributes) { ?>
70+
*/
71+
#[Route('/check-email', name: 'app_check_email')]
72+
<?php } else { ?>
6073
* @Route("/check-email", name="app_check_email")
6174
*/
75+
<?php } ?>
6276
public function checkEmail(): Response
6377
{
6478
// We prevent users from directly accessing this page
@@ -74,8 +88,13 @@ public function checkEmail(): Response
7488
/**
7589
* Validates and process the reset URL that the user clicked in their email.
7690
*
91+
<?php if ($use_attributes) { ?>
92+
*/
93+
#[Route('/reset/{token}', name: 'app_reset_password')]
94+
<?php } else { ?>
7795
* @Route("/reset/{token}", name="app_reset_password")
7896
*/
97+
<?php } ?>
7998
public function reset(Request $request, UserPasswordEncoderInterface $passwordEncoder, string $token = null): Response
8099
{
81100
if ($token) {

0 commit comments

Comments
 (0)