Skip to content
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

[make:crud] Improve controller generation #939

Merged
merged 2 commits into from
Feb 23, 2022
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
35 changes: 34 additions & 1 deletion src/Resources/skeleton/crud/controller/Controller.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
use <?= $form_full_class_name ?>;
<?php if (isset($repository_full_class_name)): ?>
use <?= $repository_full_class_name ?>;
<?php endif ?>
<?php else: ?>
use Doctrine\ORM\EntityManagerInterface;
<?php endif; ?>
use Symfony\Bundle\FrameworkBundle\Controller\<?= $parent_class_name ?>;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -44,18 +45,29 @@ public function index(EntityManagerInterface $entityManager): Response
<?php endif ?>

<?= $generator->generateRouteForControllerMethod('/new', sprintf('%s_new', $route_name), ['GET', 'POST']) ?>
<?php if (isset($repository_full_class_name) && $generator->repositoryHasAddRemoveMethods($repository_full_class_name)) { ?>
public function new(Request $request, <?= $repository_class_name ?> $<?= $repository_var ?>): Response
<?php } else { ?>
public function new(Request $request, EntityManagerInterface $entityManager): Response
<?php } ?>
{
$<?= $entity_var_singular ?> = new <?= $entity_class_name ?>();
$form = $this->createForm(<?= $form_class_name ?>::class, $<?= $entity_var_singular ?>);
$form->handleRequest($request);

<?php if (isset($repository_full_class_name) && $generator->repositoryHasAddRemoveMethods($repository_full_class_name)) { ?>
if ($form->isSubmitted() && $form->isValid()) {
$<?= $repository_var ?>->add($<?= $entity_var_singular ?>);
return $this->redirectToRoute('<?= $route_name ?>_index', [], Response::HTTP_SEE_OTHER);
}
<?php } else { ?>
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->persist($<?= $entity_var_singular ?>);
$entityManager->flush();

return $this->redirectToRoute('<?= $route_name ?>_index', [], Response::HTTP_SEE_OTHER);
}
<?php } ?>

<?php if ($use_render_form) { ?>
return $this->renderForm('<?= $templates_path ?>/new.html.twig', [
Expand All @@ -79,16 +91,27 @@ public function show(<?= $entity_class_name ?> $<?= $entity_var_singular ?>): Re
}

<?= $generator->generateRouteForControllerMethod(sprintf('/{%s}/edit', $entity_identifier), sprintf('%s_edit', $route_name), ['GET', 'POST']) ?>
<?php if (isset($repository_full_class_name) && $generator->repositoryHasAddRemoveMethods($repository_full_class_name)) { ?>
public function edit(Request $request, <?= $entity_class_name ?> $<?= $entity_var_singular ?>, <?= $repository_class_name ?> $<?= $repository_var ?>): Response
<?php } else { ?>
public function edit(Request $request, <?= $entity_class_name ?> $<?= $entity_var_singular ?>, EntityManagerInterface $entityManager): Response
<?php } ?>
{
$form = $this->createForm(<?= $form_class_name ?>::class, $<?= $entity_var_singular ?>);
$form->handleRequest($request);

<?php if (isset($repository_full_class_name) && $generator->repositoryHasAddRemoveMethods($repository_full_class_name)) { ?>
if ($form->isSubmitted() && $form->isValid()) {
$<?= $repository_var ?>->add($<?= $entity_var_singular ?>);
return $this->redirectToRoute('<?= $route_name ?>_index', [], Response::HTTP_SEE_OTHER);
}
<?php } else { ?>
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->flush();

return $this->redirectToRoute('<?= $route_name ?>_index', [], Response::HTTP_SEE_OTHER);
}
<?php } ?>

<?php if ($use_render_form) { ?>
return $this->renderForm('<?= $templates_path ?>/edit.html.twig', [
Expand All @@ -104,12 +127,22 @@ public function edit(Request $request, <?= $entity_class_name ?> $<?= $entity_va
}

<?= $generator->generateRouteForControllerMethod(sprintf('/{%s}', $entity_identifier), sprintf('%s_delete', $route_name), ['POST']) ?>
<?php if (isset($repository_full_class_name) && $generator->repositoryHasAddRemoveMethods($repository_full_class_name)) { ?>
public function delete(Request $request, <?= $entity_class_name ?> $<?= $entity_var_singular ?>, <?= $repository_class_name ?> $<?= $repository_var ?>): Response
<?php } else { ?>
public function delete(Request $request, <?= $entity_class_name ?> $<?= $entity_var_singular ?>, EntityManagerInterface $entityManager): Response
<?php } ?>
{
<?php if (isset($repository_full_class_name) && $generator->repositoryHasAddRemoveMethods($repository_full_class_name)) { ?>
if ($this->isCsrfTokenValid('delete'.$<?= $entity_var_singular ?>->get<?= ucfirst($entity_identifier) ?>(), $request->request->get('_token'))) {
$<?= $repository_var ?>->remove($<?= $entity_var_singular ?>);
}
<?php } else { ?>
if ($this->isCsrfTokenValid('delete'.$<?= $entity_var_singular ?>->get<?= ucfirst($entity_identifier) ?>(), $request->request->get('_token'))) {
$entityManager->remove($<?= $entity_var_singular ?>);
$entityManager->flush();
}
<?php } ?>

return $this->redirectToRoute('<?= $route_name ?>_index', [], Response::HTTP_SEE_OTHER);
}
Expand Down
26 changes: 26 additions & 0 deletions src/Resources/skeleton/doctrine/Repository.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use <?= $entity_full_class_name; ?>;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\OptimisticLockException;
use Doctrine\ORM\ORMException;
use <?= $doctrine_registry_class; ?>;
<?= $with_password_upgrade ? "use Symfony\Component\Security\Core\Exception\UnsupportedUserException;\n" : '' ?>
<?= ($with_password_upgrade && str_contains($password_upgrade_user_interface->getFullName(), 'Password')) ? sprintf("use %s;\n", $password_upgrade_user_interface->getFullName()) : null ?>
Expand All @@ -22,6 +24,30 @@ public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, <?= $entity_class_name; ?>::class);
}

/**
* @throws ORMException
* @throws OptimisticLockException
*/
public function add(<?= $entity_class_name ?> $entity, bool $flush = true): void
{
$this->_em->persist($entity);
if ($flush) {
$this->_em->flush();
}
}

/**
* @throws ORMException
* @throws OptimisticLockException
*/
public function remove(<?= $entity_class_name ?> $entity, bool $flush = true): void
{
$this->_em->remove($entity);
if ($flush) {
$this->_em->flush();
}
}
<?php if ($include_example_comments): // When adding a new method without existing default comments, the blank line is automatically added.?>

<?php endif; ?>
Expand Down
13 changes: 13 additions & 0 deletions src/Util/TemplateComponentGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

namespace Symfony\Bundle\MakerBundle\Util;

use ReflectionClass;
use ReflectionException;

/**
* @author Jesse Rushlow <[email protected]>
*
Expand Down Expand Up @@ -96,4 +99,14 @@ public function getPropertyType(ClassNameDetails $classNameDetails): ?string

return sprintf('%s ', $classNameDetails->getShortName());
}

/**
* @throws ReflectionException
*/
public function repositoryHasAddRemoveMethods(string $repositoryFullClassName): bool
{
$reflectedComponents = new ReflectionClass($repositoryFullClassName);

return $reflectedComponents->hasMethod('add') && $reflectedComponents->hasMethod('remove');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Symfony\Bundle\MakerBundle\Tests\tmp\current_project_xml\src\Entity\UserXml;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\OptimisticLockException;
use Doctrine\ORM\ORMException;
use Doctrine\Persistence\ManagerRegistry;

/**
Expand All @@ -19,6 +21,30 @@ public function __construct(ManagerRegistry $registry)
parent::__construct($registry, UserXml::class);
}

/**
* @throws ORMException
* @throws OptimisticLockException
*/
public function add(UserXml $entity, bool $flush = true): void
{
$this->_em->persist($entity);
if ($flush) {
$this->_em->flush();
}
}

/**
* @throws ORMException
* @throws OptimisticLockException
*/
public function remove(UserXml $entity, bool $flush = true): void
{
$this->_em->remove($entity);
if ($flush) {
$this->_em->flush();
}
}

// /**
// * @return UserXml[] Returns an array of UserXml objects
// */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Symfony\Bundle\MakerBundle\Tests\tmp\current_project_xml\src\Entity\XOther;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\OptimisticLockException;
use Doctrine\ORM\ORMException;
use Doctrine\Persistence\ManagerRegistry;

/**
Expand All @@ -19,6 +21,30 @@ public function __construct(ManagerRegistry $registry)
parent::__construct($registry, XOther::class);
}

/**
* @throws ORMException
* @throws OptimisticLockException
*/
public function add(XOther $entity, bool $flush = true): void
{
$this->_em->persist($entity);
if ($flush) {
$this->_em->flush();
}
}

/**
* @throws ORMException
* @throws OptimisticLockException
*/
public function remove(XOther $entity, bool $flush = true): void
{
$this->_em->remove($entity);
if ($flush) {
$this->_em->flush();
}
}

// /**
// * @return XOther[] Returns an array of XOther objects
// */
Expand Down