|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (C) 2018 Gerrit Addiks. |
|
4
|
|
|
* This package (including this file) was released under the terms of the GPL-3.0. |
|
5
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
|
6
|
|
|
* If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy. |
|
7
|
|
|
* |
|
8
|
|
|
* @license GPL-3.0 |
|
9
|
|
|
* |
|
10
|
|
|
* @author Gerrit Addiks <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Addiks\SymfonyGenerics\Controllers; |
|
14
|
|
|
|
|
15
|
|
|
use Throwable; |
|
16
|
|
|
use Twig_Environment; |
|
17
|
|
|
use Psr\Log\LoggerInterface; |
|
18
|
|
|
use Addiks\SymfonyGenerics\Controllers\ControllerHelperInterface; |
|
19
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
20
|
|
|
use Doctrine\Common\Persistence\ObjectRepository; |
|
21
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
|
22
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
|
23
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
24
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
25
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
|
26
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
27
|
|
|
use Symfony\Component\EventDispatcher\Event; |
|
28
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
29
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
30
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The default implementation of the controller-helper. |
|
34
|
|
|
*/ |
|
35
|
|
|
final class DefaultControllerHelper implements ControllerHelperInterface |
|
36
|
|
|
{ |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var EntityManagerInterface |
|
40
|
|
|
*/ |
|
41
|
|
|
private $entityManager; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var Twig_Environment |
|
45
|
|
|
*/ |
|
46
|
|
|
private $twig; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var AuthorizationCheckerInterface |
|
50
|
|
|
*/ |
|
51
|
|
|
private $authorization; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var UrlGeneratorInterface |
|
55
|
|
|
*/ |
|
56
|
|
|
private $urlGenerator; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var Session |
|
60
|
|
|
*/ |
|
61
|
|
|
private $session; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var LoggerInterface |
|
65
|
|
|
*/ |
|
66
|
|
|
private $logger; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @var EventDispatcherInterface |
|
70
|
|
|
*/ |
|
71
|
|
|
private $eventDispatcher; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @var RequestStack |
|
75
|
|
|
*/ |
|
76
|
|
|
private $requestStack; |
|
77
|
|
|
|
|
78
|
8 |
|
public function __construct( |
|
79
|
|
|
EntityManagerInterface $entityManager, |
|
80
|
|
|
Twig_Environment $twig, |
|
81
|
|
|
AuthorizationCheckerInterface $authorization, |
|
82
|
|
|
UrlGeneratorInterface $urlGenerator, |
|
83
|
|
|
Session $session, |
|
84
|
|
|
LoggerInterface $logger, |
|
85
|
|
|
EventDispatcherInterface $eventDispatcher, |
|
86
|
|
|
RequestStack $requestStack |
|
87
|
|
|
) { |
|
88
|
8 |
|
$this->entityManager = $entityManager; |
|
89
|
8 |
|
$this->twig = $twig; |
|
90
|
8 |
|
$this->authorization = $authorization; |
|
91
|
8 |
|
$this->urlGenerator = $urlGenerator; |
|
92
|
8 |
|
$this->session = $session; |
|
93
|
8 |
|
$this->logger = $logger; |
|
94
|
8 |
|
$this->eventDispatcher = $eventDispatcher; |
|
95
|
8 |
|
$this->requestStack = $requestStack; |
|
96
|
8 |
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function renderTemplate(string $templatePath, array $arguments = array()): Response |
|
99
|
|
|
{ |
|
100
|
|
|
return new Response($this->twig->render($templatePath, $arguments)); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function findEntity(string $entityClass, string $id) |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->entityManager->find($entityClass, $id); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function findEntities(string $entityClass, array $criteria): array |
|
109
|
|
|
{ |
|
110
|
|
|
/** @var ObjectRepository $repository */ |
|
111
|
|
|
$repository = $this->entityManager->getRepository($entityClass); |
|
112
|
|
|
|
|
113
|
|
|
return $repository->findBy($criteria); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
1 |
|
public function persistEntity($entity): void |
|
117
|
|
|
{ |
|
118
|
1 |
|
$this->entityManager->persist($entity); |
|
119
|
1 |
|
} |
|
120
|
|
|
|
|
121
|
1 |
|
public function removeEntity($entity): void |
|
122
|
|
|
{ |
|
123
|
1 |
|
$this->entityManager->remove($entity); |
|
124
|
1 |
|
} |
|
125
|
|
|
|
|
126
|
1 |
|
public function flushORM(): void |
|
127
|
|
|
{ |
|
128
|
1 |
|
$this->entityManager->flush(); |
|
129
|
1 |
|
} |
|
130
|
|
|
|
|
131
|
1 |
|
public function handleException(Throwable $exception): void |
|
132
|
|
|
{ |
|
133
|
1 |
|
$this->logger->log("error", (string)$exception); |
|
134
|
1 |
|
} |
|
135
|
|
|
|
|
136
|
1 |
|
public function addFlashMessage(string $message, string $type = "default"): void |
|
137
|
|
|
{ |
|
138
|
1 |
|
$this->session->getFlashBag()->add($type, $message); |
|
139
|
1 |
|
} |
|
140
|
|
|
|
|
141
|
2 |
|
public function redirectToRoute(string $route, array $parameters = array(), int $status = 301): Response |
|
142
|
|
|
{ |
|
143
|
|
|
/** @var string $url */ |
|
144
|
2 |
|
$url = $this->urlGenerator->generate($route, $parameters, UrlGeneratorInterface::ABSOLUTE_URL); |
|
145
|
|
|
|
|
146
|
2 |
|
return new RedirectResponse($url, $status); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
public function getRequestStack(): RequestStack |
|
150
|
|
|
{ |
|
151
|
|
|
return $this->requestStack; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
public function getCurrentRequest(): ?Request |
|
155
|
|
|
{ |
|
156
|
|
|
return $this->requestStack->getCurrentRequest(); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
1 |
|
public function denyAccessUnlessGranted(string $attribute, $subject): void |
|
160
|
|
|
{ |
|
161
|
1 |
|
if (!$this->authorization->isGranted($attribute, $subject)) { |
|
162
|
1 |
|
$exception = new AccessDeniedException('Access Denied.'); |
|
163
|
1 |
|
$exception->setSubject($subject); |
|
164
|
1 |
|
$exception->setAttributes($attribute); |
|
165
|
|
|
|
|
166
|
1 |
|
throw $exception; |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public function dispatchEvent(string $eventName, Event $event = null): Event |
|
171
|
|
|
{ |
|
172
|
|
|
return $this->eventDispatcher->dispatch($eventName, $event); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
} |
|
176
|
|
|
|