Skip to content

php8 attributes support for generated controllers #725

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

Merged
merged 1 commit into from
Nov 4, 2020
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
5 changes: 5 additions & 0 deletions src/Maker/AbstractMaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,9 @@ protected function addDependencies(array $dependencies, string $message = null):
$message
);
}

final protected function useAttributes(): bool
{
return \PHP_VERSION_ID >= 80000;
}
}
1 change: 1 addition & 0 deletions src/Maker/MakeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
$controllerClassNameDetails->getFullName(),
'controller/Controller.tpl.php',
[
'use_attributes' => $this->useAttributes(),
'route_path' => Str::asRoutePath($controllerClassNameDetails->getRelativeNameWithoutSuffix()),
'route_name' => Str::asRouteName($controllerClassNameDetails->getRelativeNameWithoutSuffix()),
'with_template' => $this->isTwigInstalled() && !$noTemplate,
Expand Down
1 change: 1 addition & 0 deletions src/Maker/MakeCrud.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
$controllerClassDetails->getFullName(),
'crud/controller/Controller.tpl.php',
array_merge([
'use_attributes' => $this->useAttributes(),
'entity_full_class_name' => $entityClassDetails->getFullName(),
'entity_class_name' => $entityClassDetails->getShortName(),
'form_full_class_name' => $formClassDetails->getFullName(),
Expand Down
1 change: 1 addition & 0 deletions src/Maker/MakeRegistrationForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
$controllerClassNameDetails->getFullName(),
'registration/RegistrationController.tpl.php',
[
'use_attributes' => $this->useAttributes(),
'route_path' => '/register',
'route_name' => 'app_register',
'form_class_name' => $formClassDetails->getShortName(),
Expand Down
1 change: 1 addition & 0 deletions src/Maker/MakeResetPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
$controllerClassNameDetails->getFullName(),
'resetPassword/ResetPasswordController.tpl.php',
[
'use_attributes' => $this->useAttributes(),
'user_full_class_name' => $userClassNameDetails->getFullName(),
'user_class_name' => $userClassNameDetails->getShortName(),
'request_form_type_full_class_name' => $requestFormTypeClassNameDetails->getFullName(),
Expand Down
4 changes: 4 additions & 0 deletions src/Resources/skeleton/controller/Controller.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@

class <?= $class_name; ?> extends <?= $parent_class_name; ?><?= "\n" ?>
{
<?php if ($use_attributes) { ?>
#[Route('<?= $route_path ?>', name: '<?= $route_name ?>')]
<?php } else { ?>
/**
* @Route("<?= $route_path ?>", name="<?= $route_name ?>")
*/
<?php } ?>
public function index(): Response
{
<?php if ($with_template) { ?>
Expand Down
24 changes: 24 additions & 0 deletions src/Resources/skeleton/crud/controller/Controller.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,22 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

<?php if ($use_attributes) { ?>
#[Route('<?= $route_path ?>')]
<?php } else { ?>
/**
* @Route("<?= $route_path ?>")
*/
<?php } ?>
class <?= $class_name ?> extends <?= $parent_class_name; ?><?= "\n" ?>
{
<?php if ($use_attributes) { ?>
#[Route('/', name: '<?= $route_name ?>_index', methods: ['GET'])]
<?php } else { ?>
/**
* @Route("/", name="<?= $route_name ?>_index", methods={"GET"})
*/
<?php } ?>
<?php if (isset($repository_full_class_name)): ?>
public function index(<?= $repository_class_name ?> $<?= $repository_var ?>): Response
{
Expand All @@ -40,9 +48,13 @@ public function index(): Response
}
<?php endif ?>

<?php if ($use_attributes) { ?>
#[Route('/new', name: '<?= $route_name ?>_new', methods: ['GET', 'POST'])]
<?php } else { ?>
/**
* @Route("/new", name="<?= $route_name ?>_new", methods={"GET","POST"})
*/
<?php } ?>
public function new(Request $request): Response
{
$<?= $entity_var_singular ?> = new <?= $entity_class_name ?>();
Expand All @@ -63,19 +75,27 @@ public function new(Request $request): Response
]);
}

<?php if ($use_attributes) { ?>
#[Route('/{<?= $entity_identifier ?>}', name: '<?= $route_name ?>_show', methods: ['GET'])]
<?php } else { ?>
/**
* @Route("/{<?= $entity_identifier ?>}", name="<?= $route_name ?>_show", methods={"GET"})
*/
<?php } ?>
public function show(<?= $entity_class_name ?> $<?= $entity_var_singular ?>): Response
{
return $this->render('<?= $templates_path ?>/show.html.twig', [
'<?= $entity_twig_var_singular ?>' => $<?= $entity_var_singular ?>,
]);
}

<?php if ($use_attributes) { ?>
#[Route('/{<?= $entity_identifier ?>}/edit', name: '<?= $route_name ?>_edit', methods: ['GET', 'POST'])]
<?php } else { ?>
/**
* @Route("/{<?= $entity_identifier ?>}/edit", name="<?= $route_name ?>_edit", methods={"GET","POST"})
*/
<?php } ?>
public function edit(Request $request, <?= $entity_class_name ?> $<?= $entity_var_singular ?>): Response
{
$form = $this->createForm(<?= $form_class_name ?>::class, $<?= $entity_var_singular ?>);
Expand All @@ -93,9 +113,13 @@ public function edit(Request $request, <?= $entity_class_name ?> $<?= $entity_va
]);
}

<?php if ($use_attributes) { ?>
#[Route('/{<?= $entity_identifier ?>}', name: '<?= $route_name ?>_delete', methods: ['DELETE'])]
<?php } else { ?>
/**
* @Route("/{<?= $entity_identifier ?>}", name="<?= $route_name ?>_delete", methods={"DELETE"})
*/
<?php } ?>
public function delete(Request $request, <?= $entity_class_name ?> $<?= $entity_var_singular ?>): Response
{
if ($this->isCsrfTokenValid('delete'.$<?= $entity_var_singular ?>->get<?= ucfirst($entity_identifier) ?>(), $request->request->get('_token'))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ public function __construct(EmailVerifier $emailVerifier)
}

<?php endif; ?>
<?php if ($use_attributes) { ?>
#[Route('<?= $route_path ?>', name: '<?= $route_name ?>')]
<?php } else { ?>
/**
* @Route("<?= $route_path ?>", name="<?= $route_name ?>")
*/
<?php } ?>
public function register(Request $request, UserPasswordEncoderInterface $passwordEncoder<?= $authenticator_full_class_name ? sprintf(', GuardAuthenticatorHandler $guardHandler, %s $authenticator', $authenticator_class_name) : '' ?>): Response
{
$user = new <?= $user_class_name ?>();
Expand Down Expand Up @@ -91,9 +95,13 @@ public function register(Request $request, UserPasswordEncoderInterface $passwor
}
<?php if ($will_verify_email): ?>

<?php if ($use_attributes) { ?>
#[Route('/verify/email', name: 'app_verify_email')]
<?php } else { ?>
/**
* @Route("/verify/email", name="app_verify_email")
*/
<?php } ?>
public function verifyUserEmail(Request $request): Response
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@
use SymfonyCasts\Bundle\ResetPassword\Exception\ResetPasswordExceptionInterface;
use SymfonyCasts\Bundle\ResetPassword\ResetPasswordHelperInterface;

<?php if ($use_attributes) { ?>
#[Route('/reset-password')]
<?php } else { ?>
/**
* @Route("/reset-password")
*/
<?php } ?>
class <?= $class_name ?> extends AbstractController
{
use ResetPasswordControllerTrait;
Expand All @@ -35,8 +39,13 @@ public function __construct(ResetPasswordHelperInterface $resetPasswordHelper)
/**
* Display & process form to request a password reset.
*
<?php if ($use_attributes) { ?>
*/
#[Route('', name: 'app_forgot_password_request')]
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

the empty '' path was added on purpose when we first created this maker - but I don't remember exactly why we decided to do this. Will investigate and adjust accordingly under a separate issue.

Copy link
Member

Choose a reason for hiding this comment

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

I also can't remember... it may have been that / here would have made the final URL have a trailing slash - /reset-password/

<?php } else { ?>
* @Route("", name="app_forgot_password_request")
*/
<?php } ?>
public function request(Request $request, MailerInterface $mailer): Response
{
$form = $this->createForm(<?= $request_form_type_class_name ?>::class);
Expand All @@ -57,8 +66,13 @@ public function request(Request $request, MailerInterface $mailer): Response
/**
* Confirmation page after a user has requested a password reset.
*
<?php if ($use_attributes) { ?>
*/
#[Route('/check-email', name: 'app_check_email')]
<?php } else { ?>
* @Route("/check-email", name="app_check_email")
*/
<?php } ?>
public function checkEmail(): Response
{
// We prevent users from directly accessing this page
Expand All @@ -74,8 +88,13 @@ public function checkEmail(): Response
/**
* Validates and process the reset URL that the user clicked in their email.
*
<?php if ($use_attributes) { ?>
*/
#[Route('/reset/{token}', name: 'app_reset_password')]
<?php } else { ?>
* @Route("/reset/{token}", name="app_reset_password")
*/
<?php } ?>
public function reset(Request $request, UserPasswordEncoderInterface $passwordEncoder, string $token = null): Response
{
if ($token) {
Expand Down