-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractCrudController.php
More file actions
192 lines (151 loc) · 6.39 KB
/
AbstractCrudController.php
File metadata and controls
192 lines (151 loc) · 6.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
namespace Jeandanyel\CrudBundle\Controller;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Jeandanyel\CrudBundle\Enum\CrudTemplatePath;
use Jeandanyel\CrudBundle\Event\EntityAfterSaveEvent;
use Jeandanyel\CrudBundle\Event\EntityBeforeSaveEvent;
use Jeandanyel\CrudBundle\Helper\ControllerHelper;
use Jeandanyel\ListBundle\Factory\ListFactoryInterface;
use Jeandanyel\ListBundle\List\ListInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\Service\Attribute\Required;
use Symfony\Contracts\Translation\TranslatorInterface;
abstract class AbstractCrudController extends AbstractController implements CrudControllerInterface
{
protected EntityManagerInterface $entityManager;
protected ListFactoryInterface $listFactory;
protected EventDispatcherInterface $eventDispather;
protected TranslatorInterface $translator;
public function list(Request $request): Response
{
$list = $this->createList($this->getListTypeClass());
$controllerName = ControllerHelper::getName($this);
$templatePath = sprintf('%s/%s', $controllerName, CrudTemplatePath::LIST->getFileName());
if (!$this->container->get('twig')->getLoader()->exists($templatePath)) {
$templatePath = CrudTemplatePath::LIST->value;
}
return $this->render($templatePath, [
'controller_name' => $controllerName,
'list' => $list->createView(),
]);
}
public function create(Request $request): Response
{
$entityClass = $this->getEntityClass();
$entity = new $entityClass();
$controllerName = ControllerHelper::getName($this);
$form = $this->createForm($this->getFormTypeClass(), $entity);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->eventDispather->dispatch(new EntityBeforeSaveEvent($this, $entity, $request, $form));
$this->entityManager->persist($entity);
$this->entityManager->flush();
$this->eventDispather->dispatch(new EntityAfterSaveEvent($this, $entity, $request, $form));
$this->addFlash('success', $this->translator->trans(
"{$controllerName}.messages.has_been_created",
['%name%' => $entity]
));
return $this->redirectToRoute("crud_{$controllerName}_created", ['id' => $entity->getId()]);
}
$templatePath = sprintf('%s/%s', $controllerName, CrudTemplatePath::CREATE->getFileName());
if (!$this->container->get('twig')->getLoader()->exists($templatePath)) {
$templatePath = CrudTemplatePath::CREATE->value;
}
return $this->render($templatePath, [
$controllerName => $entity,
'controller_name' => $controllerName,
'form' => $form->createView(),
]);
}
public function update(Request $request): Response
{
$entity = $this->findEntity($request);
$controllerName = ControllerHelper::getName($this);
$form = $this->createForm($this->getFormTypeClass(), $entity);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->eventDispather->dispatch(new EntityBeforeSaveEvent($this, $entity, $request, $form));
$this->entityManager->persist($entity);
$this->entityManager->flush();
$this->eventDispather->dispatch(new EntityAfterSaveEvent($this, $entity, $request, $form));
$this->addFlash('success', $this->translator->trans(
"{$controllerName}.messages.has_been_updated",
['%name%' => $entity]
));
return $this->redirectToRoute("crud_{$controllerName}_update", ['id' => $entity->getId()]);
}
$templatePath = sprintf('%s/%s', $controllerName, CrudTemplatePath::UPDATE->getFileName());
if (!$this->container->get('twig')->getLoader()->exists($templatePath)) {
$templatePath = CrudTemplatePath::UPDATE->value;
}
return $this->render($templatePath, [
$controllerName => $entity,
'controller_name' => $controllerName,
'form' => $form->createView(),
]);
}
public function delete(Request $request): Response
{
$entity = $this->findEntity($request);
$this->entityManager->remove($entity);
$this->entityManager->flush();
return new Response();
}
protected function createList(string $listTypeClass, array $options = []): ListInterface
{
return $this->listFactory->create($listTypeClass, $options);
}
protected function findEntity(Request $request): object
{
$id = $request->attributes->get('id');
$entity = $this->getRepository()->find($id);
if (!$entity) {
throw $this->createNotFoundException(sprintf(
'The %s entity with ID %d was not found.',
$this->getEntityClass(),
$id
));
}
return $entity;
}
protected function getRepository(): EntityRepository
{
return $this->entityManager->getRepository($this->getEntityClass());
}
protected function getEntityClass(): string
{
return ControllerHelper::getCrudControllerAttribute($this)->getEntityClass();
}
protected function getFormTypeClass(): string
{
return ControllerHelper::getCrudControllerAttribute($this)->getFormTypeClass();
}
protected function getListTypeClass(): string
{
return ControllerHelper::getCrudControllerAttribute($this)->getListTypeClass();
}
#[Required]
public function setEntityManager(EntityManagerInterface $entityManager): void
{
$this->entityManager = $entityManager;
}
#[Required]
public function setListFactory(ListFactoryInterface $listFactory): void
{
$this->listFactory = $listFactory;
}
#[Required]
public function setEventDispatcher(EventDispatcherInterface $eventDispather): void
{
$this->eventDispather = $eventDispather;
}
#[Required]
public function setTranslator(TranslatorInterface $translator): void
{
$this->translator = $translator;
}
}