Skip to content

Commit 1f20a8c

Browse files
authored
Merge pull request #939 from bdaler/improveCrudControllerGeneration.788
[make:crud] Improve controller generation
2 parents 187d02e + a6bcf69 commit 1f20a8c

File tree

5 files changed

+125
-1
lines changed

5 files changed

+125
-1
lines changed

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
use <?= $form_full_class_name ?>;
77
<?php if (isset($repository_full_class_name)): ?>
88
use <?= $repository_full_class_name ?>;
9-
<?php endif ?>
9+
<?php else: ?>
1010
use Doctrine\ORM\EntityManagerInterface;
11+
<?php endif; ?>
1112
use Symfony\Bundle\FrameworkBundle\Controller\<?= $parent_class_name ?>;
1213
use Symfony\Component\HttpFoundation\Request;
1314
use Symfony\Component\HttpFoundation\Response;
@@ -44,18 +45,29 @@ public function index(EntityManagerInterface $entityManager): Response
4445
<?php endif ?>
4546

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

58+
<?php if (isset($repository_full_class_name) && $generator->repositoryHasAddRemoveMethods($repository_full_class_name)) { ?>
59+
if ($form->isSubmitted() && $form->isValid()) {
60+
$<?= $repository_var ?>->add($<?= $entity_var_singular ?>);
61+
return $this->redirectToRoute('<?= $route_name ?>_index', [], Response::HTTP_SEE_OTHER);
62+
}
63+
<?php } else { ?>
5364
if ($form->isSubmitted() && $form->isValid()) {
5465
$entityManager->persist($<?= $entity_var_singular ?>);
5566
$entityManager->flush();
5667

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

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

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

103+
<?php if (isset($repository_full_class_name) && $generator->repositoryHasAddRemoveMethods($repository_full_class_name)) { ?>
104+
if ($form->isSubmitted() && $form->isValid()) {
105+
$<?= $repository_var ?>->add($<?= $entity_var_singular ?>);
106+
return $this->redirectToRoute('<?= $route_name ?>_index', [], Response::HTTP_SEE_OTHER);
107+
}
108+
<?php } else { ?>
87109
if ($form->isSubmitted() && $form->isValid()) {
88110
$entityManager->flush();
89111

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

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

106129
<?= $generator->generateRouteForControllerMethod(sprintf('/{%s}', $entity_identifier), sprintf('%s_delete', $route_name), ['POST']) ?>
130+
<?php if (isset($repository_full_class_name) && $generator->repositoryHasAddRemoveMethods($repository_full_class_name)) { ?>
131+
public function delete(Request $request, <?= $entity_class_name ?> $<?= $entity_var_singular ?>, <?= $repository_class_name ?> $<?= $repository_var ?>): Response
132+
<?php } else { ?>
107133
public function delete(Request $request, <?= $entity_class_name ?> $<?= $entity_var_singular ?>, EntityManagerInterface $entityManager): Response
134+
<?php } ?>
108135
{
136+
<?php if (isset($repository_full_class_name) && $generator->repositoryHasAddRemoveMethods($repository_full_class_name)) { ?>
137+
if ($this->isCsrfTokenValid('delete'.$<?= $entity_var_singular ?>->get<?= ucfirst($entity_identifier) ?>(), $request->request->get('_token'))) {
138+
$<?= $repository_var ?>->remove($<?= $entity_var_singular ?>);
139+
}
140+
<?php } else { ?>
109141
if ($this->isCsrfTokenValid('delete'.$<?= $entity_var_singular ?>->get<?= ucfirst($entity_identifier) ?>(), $request->request->get('_token'))) {
110142
$entityManager->remove($<?= $entity_var_singular ?>);
111143
$entityManager->flush();
112144
}
145+
<?php } ?>
113146

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

src/Resources/skeleton/doctrine/Repository.tpl.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use <?= $entity_full_class_name; ?>;
66
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
7+
use Doctrine\ORM\OptimisticLockException;
8+
use Doctrine\ORM\ORMException;
79
use <?= $doctrine_registry_class; ?>;
810
<?= $with_password_upgrade ? "use Symfony\Component\Security\Core\Exception\UnsupportedUserException;\n" : '' ?>
911
<?= ($with_password_upgrade && str_contains($password_upgrade_user_interface->getFullName(), 'Password')) ? sprintf("use %s;\n", $password_upgrade_user_interface->getFullName()) : null ?>
@@ -22,6 +24,30 @@ public function __construct(ManagerRegistry $registry)
2224
{
2325
parent::__construct($registry, <?= $entity_class_name; ?>::class);
2426
}
27+
28+
/**
29+
* @throws ORMException
30+
* @throws OptimisticLockException
31+
*/
32+
public function add(<?= $entity_class_name ?> $entity, bool $flush = true): void
33+
{
34+
$this->_em->persist($entity);
35+
if ($flush) {
36+
$this->_em->flush();
37+
}
38+
}
39+
40+
/**
41+
* @throws ORMException
42+
* @throws OptimisticLockException
43+
*/
44+
public function remove(<?= $entity_class_name ?> $entity, bool $flush = true): void
45+
{
46+
$this->_em->remove($entity);
47+
if ($flush) {
48+
$this->_em->flush();
49+
}
50+
}
2551
<?php if ($include_example_comments): // When adding a new method without existing default comments, the blank line is automatically added.?>
2652

2753
<?php endif; ?>

src/Util/TemplateComponentGenerator.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
namespace Symfony\Bundle\MakerBundle\Util;
1313

14+
use ReflectionClass;
15+
use ReflectionException;
16+
1417
/**
1518
* @author Jesse Rushlow <jr@rushlow.dev>
1619
*
@@ -96,4 +99,14 @@ public function getPropertyType(ClassNameDetails $classNameDetails): ?string
9699

97100
return sprintf('%s ', $classNameDetails->getShortName());
98101
}
102+
103+
/**
104+
* @throws ReflectionException
105+
*/
106+
public function repositoryHasAddRemoveMethods(string $repositoryFullClassName): bool
107+
{
108+
$reflectedComponents = new ReflectionClass($repositoryFullClassName);
109+
110+
return $reflectedComponents->hasMethod('add') && $reflectedComponents->hasMethod('remove');
111+
}
99112
}

tests/Doctrine/fixtures/expected_xml/src/Repository/UserRepository.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Symfony\Bundle\MakerBundle\Tests\tmp\current_project_xml\src\Entity\UserXml;
66
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
7+
use Doctrine\ORM\OptimisticLockException;
8+
use Doctrine\ORM\ORMException;
79
use Doctrine\Persistence\ManagerRegistry;
810

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

24+
/**
25+
* @throws ORMException
26+
* @throws OptimisticLockException
27+
*/
28+
public function add(UserXml $entity, bool $flush = true): void
29+
{
30+
$this->_em->persist($entity);
31+
if ($flush) {
32+
$this->_em->flush();
33+
}
34+
}
35+
36+
/**
37+
* @throws ORMException
38+
* @throws OptimisticLockException
39+
*/
40+
public function remove(UserXml $entity, bool $flush = true): void
41+
{
42+
$this->_em->remove($entity);
43+
if ($flush) {
44+
$this->_em->flush();
45+
}
46+
}
47+
2248
// /**
2349
// * @return UserXml[] Returns an array of UserXml objects
2450
// */

tests/Doctrine/fixtures/expected_xml/src/Repository/XOtherRepository.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Symfony\Bundle\MakerBundle\Tests\tmp\current_project_xml\src\Entity\XOther;
66
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
7+
use Doctrine\ORM\OptimisticLockException;
8+
use Doctrine\ORM\ORMException;
79
use Doctrine\Persistence\ManagerRegistry;
810

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

24+
/**
25+
* @throws ORMException
26+
* @throws OptimisticLockException
27+
*/
28+
public function add(XOther $entity, bool $flush = true): void
29+
{
30+
$this->_em->persist($entity);
31+
if ($flush) {
32+
$this->_em->flush();
33+
}
34+
}
35+
36+
/**
37+
* @throws ORMException
38+
* @throws OptimisticLockException
39+
*/
40+
public function remove(XOther $entity, bool $flush = true): void
41+
{
42+
$this->_em->remove($entity);
43+
if ($flush) {
44+
$this->_em->flush();
45+
}
46+
}
47+
2248
// /**
2349
// * @return XOther[] Returns an array of XOther objects
2450
// */

0 commit comments

Comments
 (0)