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
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The default implementation of the controller-helper. |
32
|
|
|
*/ |
33
|
|
|
final class DefaultControllerHelper implements ControllerHelperInterface |
34
|
|
|
{ |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var EntityManagerInterface |
38
|
|
|
*/ |
39
|
|
|
private $entityManager; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var Twig_Environment |
43
|
|
|
*/ |
44
|
|
|
private $twig; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var AuthorizationCheckerInterface |
48
|
|
|
*/ |
49
|
|
|
private $authorization; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var UrlGeneratorInterface |
53
|
|
|
*/ |
54
|
|
|
private $urlGenerator; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var Session |
58
|
|
|
*/ |
59
|
|
|
private $session; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var LoggerInterface |
63
|
|
|
*/ |
64
|
|
|
private $logger; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var EventDispatcherInterface |
68
|
|
|
*/ |
69
|
|
|
private $eventDispatcher; |
70
|
|
|
|
71
|
8 |
|
public function __construct( |
72
|
|
|
EntityManagerInterface $entityManager, |
73
|
|
|
Twig_Environment $twig, |
74
|
|
|
AuthorizationCheckerInterface $authorization, |
75
|
|
|
UrlGeneratorInterface $urlGenerator, |
76
|
|
|
Session $session, |
77
|
|
|
LoggerInterface $logger, |
78
|
|
|
EventDispatcherInterface $eventDispatcher |
79
|
|
|
) { |
80
|
8 |
|
$this->entityManager = $entityManager; |
81
|
8 |
|
$this->twig = $twig; |
82
|
8 |
|
$this->authorization = $authorization; |
83
|
8 |
|
$this->urlGenerator = $urlGenerator; |
84
|
8 |
|
$this->session = $session; |
85
|
8 |
|
$this->logger = $logger; |
86
|
8 |
|
$this->eventDispatcher = $eventDispatcher; |
87
|
8 |
|
} |
88
|
|
|
|
89
|
|
|
public function renderTemplate(string $templatePath, array $arguments = array()): Response |
90
|
|
|
{ |
91
|
|
|
return new Response($this->twig->render($templatePath, $arguments)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function findEntity(string $entityClass, string $id) |
95
|
|
|
{ |
96
|
|
|
return $this->entityManager->find($entityClass, $id); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function findEntities(string $entityClass, array $criteria): array |
100
|
|
|
{ |
101
|
|
|
/** @var ObjectRepository $repository */ |
102
|
|
|
$repository = $this->entityManager->getRepository($entityClass); |
103
|
|
|
|
104
|
|
|
return $repository->findBy($criteria); |
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
public function persistEntity($entity): void |
108
|
|
|
{ |
109
|
1 |
|
$this->entityManager->persist($entity); |
110
|
1 |
|
} |
111
|
|
|
|
112
|
1 |
|
public function removeEntity($entity): void |
113
|
|
|
{ |
114
|
1 |
|
$this->entityManager->remove($entity); |
115
|
1 |
|
} |
116
|
|
|
|
117
|
1 |
|
public function flushORM(): void |
118
|
|
|
{ |
119
|
1 |
|
$this->entityManager->flush(); |
120
|
1 |
|
} |
121
|
|
|
|
122
|
1 |
|
public function handleException(Throwable $exception): void |
123
|
|
|
{ |
124
|
1 |
|
$this->logger->log("error", (string)$exception); |
125
|
1 |
|
} |
126
|
|
|
|
127
|
1 |
|
public function addFlashMessage(string $message, string $type = "default"): void |
128
|
|
|
{ |
129
|
1 |
|
$this->session->getFlashBag()->add($type, $message); |
130
|
1 |
|
} |
131
|
|
|
|
132
|
2 |
|
public function redirectToRoute(string $route, array $parameters = array(), int $status = 301): Response |
133
|
|
|
{ |
134
|
|
|
/** @var string $url */ |
135
|
2 |
|
$url = $this->urlGenerator->generate($route, $parameters, UrlGeneratorInterface::ABSOLUTE_URL); |
136
|
|
|
|
137
|
2 |
|
return new RedirectResponse($url, $status); |
138
|
|
|
} |
139
|
|
|
|
140
|
1 |
|
public function denyAccessUnlessGranted(string $attribute, $subject): void |
141
|
|
|
{ |
142
|
1 |
|
if (!$this->authorization->isGranted($attribute, $subject)) { |
143
|
1 |
|
$exception = new AccessDeniedException('Access Denied.'); |
144
|
1 |
|
$exception->setSubject($subject); |
145
|
1 |
|
$exception->setAttributes($attribute); |
146
|
|
|
|
147
|
1 |
|
throw $exception; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function dispatchEvent(string $eventName, Event $event = null): Event |
152
|
|
|
{ |
153
|
|
|
return $this->eventDispatcher->dispatch($eventName, $event); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
} |
157
|
|
|
|