This repository has been archived by the owner on Feb 10, 2023. It is now read-only.
/
Controller.php
234 lines (203 loc) · 5.85 KB
/
Controller.php
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
namespace Alex\WebBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller as BaseController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
/**
* A reusable controller.
*
* @author Alexandre Salomé <alexandre.salome@gmail.com>
*/
abstract class Controller extends BaseController
{
/**
* @return Doctrine\ORM\EntityRepository
*/
protected function getRepository($name)
{
return $this->getDoctrine()->getRepository($name);
}
/**
* Persists an entity with Doctrine and flushes connection.
*
* @param object $entity
*
* @return Controller
*/
protected function persistAndFlush($entity)
{
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this;
}
/**
* Creates a form with a given name. To get form fields name without prefix,
* pass an empty string as first argument:
*
* $this->createNamedForm('foo', 'repeat') => "foo[first]" and "foo[second]"
* $this->createNamedForm('', 'repeat') => "first" and "second"
*
* @see Symfony\Component\Form\FormFactoryInterface
*
* @return Symfony\Component\Form\FormInterface
*/
protected function createNamedForm($name, $type = 'form', $data = null, array $options = array())
{
return $this->get('form.factory')->createNamed($name, $type, $data, $options);
}
/**
* Adds a success message to the session flashbag.
*
* @param string $message
*
* @return Controller
*/
protected function addSuccess($message)
{
$this->get('session')->getFlashBag()->add('message_success', $message);
return $this;
}
/**
* Adds a warning message to the session flashbag.
*
* @param string $message
*
* @return Controller
*/
protected function addWarning($message)
{
$this->get('session')->getFlashBag()->add('message_warning', $message);
return $this;
}
/**
* Adds a error message to the session flashbag.
*
* @param string $message
*
* @return Controller
*/
protected function addError($message)
{
$this->get('session')->getFlashBag()->add('message_error', $message);
return $this;
}
/**
* Adds a notice message to the session flashbag.
*
* @param string $message
*
* @return Controller
*/
protected function addNotice($message)
{
$this->get('session')->getFlashBag()->add('message_notice', $message);
return $this;
}
protected function renderJson($data)
{
$response = new Response();
$response->headers->set('Content-Type', 'application/json');
$response->setContent(json_encode($data));
return $response;
}
/**
* Throws an exception (404 Not found) if condition is true.
*
* @param mixed $condition condition to test
* @param string $message error message
*
* @return Controller
*/
protected function throwNotFoundIf($condition, $message = 'Not found')
{
if ($condition) {
throw $this->createNotFoundException($message);
}
return $this;
}
/**
* Throws an exception (404 Not found) unless condition is true.
*
* @param mixed $condition condition to test
* @param string $message error message
*
* @return Controller
*/
protected function throwNotFoundUnless($condition, $message = 'Not found')
{
$this->throwNotFoundIf(!$condition, $message);
return $this;
}
/**
* Throws an exception (403 Access denied) if condition is true.
*
* @param mixed $condition condition to test
* @param string $message error message
*
* @return Controller
*/
protected function throwAccessDeniedIf($condition, $message = 'Access denied')
{
if ($condition) {
throw $this->createAccessDeniedException($message);
}
return $this;
}
/**
* Throws an exception (403 Access denied) unless condition is true.
*
* @param mixed $condition condition to test
* @param string $message error message
*
* @return Controller
*/
protected function throwAccessDeniedUnless($condition, $message = 'Access denied')
{
$this->throwAccessDeniedIf(!$condition, $message);
return $this;
}
/**
* @return AccessDeniedHttpException
*/
protected function createAccessDeniedException($message = 'Access denied', \Exception $previous = null)
{
return new AccessDeniedException($message, $previous);
}
/**
* Throws an exception (400 Bad request) if condition is true.
*
* @param mixed $condition condition to test
* @param string $message error message
*
* @return Controller
*/
protected function throwBadRequestIf($condition, $message = 'Bad request')
{
if ($condition) {
throw $this->createBadRequestException($message);
}
return $this;
}
/**
* Throws an exception (400 Bad request) unless condition is true.
*
* @param mixed $condition condition to test
* @param string $message error message
*
* @return Controller
*/
protected function throwBadRequestUnless($condition, $message = 'Bad request')
{
$this->throwBadRequestIf(!$condition, $message);
return $this;
}
/**
* @return BadRequestHttpException
*/
protected function createBadRequestException($message = 'Bad request', \Exception $previous = null)
{
return new AccessDeniedHttpException($message, $previous);
}
}