11
11
12
12
namespace Symfony \Bundle \FrameworkBundle \Controller ;
13
13
14
+ use Doctrine \Common \Persistence \ManagerRegistry ;
14
15
use Psr \Container \ContainerInterface ;
15
16
use Symfony \Component \HttpFoundation \BinaryFileResponse ;
16
17
use Symfony \Component \HttpFoundation \JsonResponse ;
23
24
use Symfony \Component \Security \Core \Exception \AccessDeniedException ;
24
25
use Symfony \Component \Security \Csrf \CsrfToken ;
25
26
use Symfony \Component \Form \Extension \Core \Type \FormType ;
26
- use Symfony \Component \Form \Form ;
27
- use Symfony \Component \Form \FormBuilder ;
27
+ use Symfony \Component \Form \FormInterface ;
28
+ use Symfony \Component \Form \FormBuilderInterface ;
28
29
use Symfony \Component \Routing \Generator \UrlGeneratorInterface ;
29
- use Doctrine \Bundle \DoctrineBundle \Registry ;
30
30
31
31
/**
32
32
* Common features needed in controllers.
@@ -42,45 +42,33 @@ trait ControllerTrait
42
42
/**
43
43
* Returns true if the service id is defined.
44
44
*
45
- * @param string $id The service id
46
- *
47
- * @return bool true if the service id is defined, false otherwise
48
- *
49
45
* @final since version 3.4
50
46
*/
51
- protected function has ($ id )
47
+ protected function has (string $ id ): bool
52
48
{
53
49
return $ this ->container ->has ($ id );
54
50
}
55
51
56
52
/**
57
53
* Gets a container service by its id.
58
54
*
59
- * @param string $id The service id
60
- *
61
55
* @return object The service
62
56
*
63
57
* @final since version 3.4
64
58
*/
65
- protected function get ($ id )
59
+ protected function get (string $ id )
66
60
{
67
61
return $ this ->container ->get ($ id );
68
62
}
69
63
70
64
/**
71
65
* Generates a URL from the given parameters.
72
66
*
73
- * @param string $route The name of the route
74
- * @param mixed $parameters An array of parameters
75
- * @param int $referenceType The type of reference (one of the constants in UrlGeneratorInterface)
76
- *
77
- * @return string The generated URL
78
- *
79
67
* @see UrlGeneratorInterface
80
68
*
81
69
* @final since version 3.4
82
70
*/
83
- protected function generateUrl ($ route , $ parameters = array (), $ referenceType = UrlGeneratorInterface::ABSOLUTE_PATH )
71
+ protected function generateUrl (string $ route , array $ parameters = array (), int $ referenceType = UrlGeneratorInterface::ABSOLUTE_PATH ): string
84
72
{
85
73
return $ this ->container ->get ('router ' )->generate ($ route , $ parameters , $ referenceType );
86
74
}
@@ -89,14 +77,10 @@ protected function generateUrl($route, $parameters = array(), $referenceType = U
89
77
* Forwards the request to another controller.
90
78
*
91
79
* @param string $controller The controller name (a string like BlogBundle:Post:index)
92
- * @param array $path An array of path parameters
93
- * @param array $query An array of query parameters
94
- *
95
- * @return Response A Response instance
96
80
*
97
81
* @final since version 3.4
98
82
*/
99
- protected function forward ($ controller , array $ path = array (), array $ query = array ())
83
+ protected function forward (string $ controller , array $ path = array (), array $ query = array ()): Response
100
84
{
101
85
$ request = $ this ->container ->get ('request_stack ' )->getCurrentRequest ();
102
86
$ path ['_forwarded ' ] = $ request ->attributes ;
@@ -109,47 +93,29 @@ protected function forward($controller, array $path = array(), array $query = ar
109
93
/**
110
94
* Returns a RedirectResponse to the given URL.
111
95
*
112
- * @param string $url The URL to redirect to
113
- * @param int $status The status code to use for the Response
114
- *
115
- * @return RedirectResponse
116
- *
117
96
* @final since version 3.4
118
97
*/
119
- protected function redirect ($ url , $ status = 302 )
98
+ protected function redirect (string $ url , int $ status = 302 ): RedirectResponse
120
99
{
121
100
return new RedirectResponse ($ url , $ status );
122
101
}
123
102
124
103
/**
125
104
* Returns a RedirectResponse to the given route with the given parameters.
126
105
*
127
- * @param string $route The name of the route
128
- * @param array $parameters An array of parameters
129
- * @param int $status The status code to use for the Response
130
- *
131
- * @return RedirectResponse
132
- *
133
106
* @final since version 3.4
134
107
*/
135
- protected function redirectToRoute ($ route , array $ parameters = array (), $ status = 302 )
108
+ protected function redirectToRoute (string $ route , array $ parameters = array (), int $ status = 302 ): RedirectResponse
136
109
{
137
110
return $ this ->redirect ($ this ->generateUrl ($ route , $ parameters ), $ status );
138
111
}
139
112
140
113
/**
141
114
* Returns a JsonResponse that uses the serializer component if enabled, or json_encode.
142
115
*
143
- * @param mixed $data The response data
144
- * @param int $status The status code to use for the Response
145
- * @param array $headers Array of extra headers to add
146
- * @param array $context Context to pass to serializer when using serializer component
147
- *
148
- * @return JsonResponse
149
- *
150
116
* @final since version 3.4
151
117
*/
152
- protected function json ($ data , $ status = 200 , $ headers = array (), $ context = array ())
118
+ protected function json ($ data , int $ status = 200 , array $ headers = array (), array $ context = array ()): JsonResponse
153
119
{
154
120
if ($ this ->container ->has ('serializer ' )) {
155
121
$ json = $ this ->container ->get ('serializer ' )->serialize ($ data , 'json ' , array_merge (array (
@@ -165,15 +131,11 @@ protected function json($data, $status = 200, $headers = array(), $context = arr
165
131
/**
166
132
* Returns a BinaryFileResponse object with original or customized file name and disposition header.
167
133
*
168
- * @param \SplFileInfo|string $file File object or path to file to be sent as response
169
- * @param string|null $fileName File name to be sent to response or null (will use original file name)
170
- * @param string $disposition Disposition of response ("attachment" is default, other type is "inline")
171
- *
172
- * @return BinaryFileResponse
134
+ * @param \SplFileInfo|string $file File object or path to file to be sent as response
173
135
*
174
136
* @final since version 3.4
175
137
*/
176
- protected function file ($ file , $ fileName = null , $ disposition = ResponseHeaderBag::DISPOSITION_ATTACHMENT )
138
+ protected function file ($ file , string $ fileName = null , string $ disposition = ResponseHeaderBag::DISPOSITION_ATTACHMENT ): BinaryFileResponse
177
139
{
178
140
$ response = new BinaryFileResponse ($ file );
179
141
$ response ->setContentDisposition ($ disposition , null === $ fileName ? $ response ->getFile ()->getFilename () : $ fileName );
@@ -184,14 +146,11 @@ protected function file($file, $fileName = null, $disposition = ResponseHeaderBa
184
146
/**
185
147
* Adds a flash message to the current session for type.
186
148
*
187
- * @param string $type The type
188
- * @param string $message The message
189
- *
190
149
* @throws \LogicException
191
150
*
192
151
* @final since version 3.4
193
152
*/
194
- protected function addFlash ($ type , $ message )
153
+ protected function addFlash (string $ type , string $ message )
195
154
{
196
155
if (!$ this ->container ->has ('session ' )) {
197
156
throw new \LogicException ('You can not use the addFlash method if sessions are disabled. ' );
@@ -203,16 +162,11 @@ protected function addFlash($type, $message)
203
162
/**
204
163
* Checks if the attributes are granted against the current authentication token and optionally supplied subject.
205
164
*
206
- * @param mixed $attributes The attributes
207
- * @param mixed $subject The subject
208
- *
209
- * @return bool
210
- *
211
165
* @throws \LogicException
212
166
*
213
167
* @final since version 3.4
214
168
*/
215
- protected function isGranted ($ attributes , $ subject = null )
169
+ protected function isGranted ($ attributes , $ subject = null ): bool
216
170
{
217
171
if (!$ this ->container ->has ('security.authorization_checker ' )) {
218
172
throw new \LogicException ('The SecurityBundle is not registered in your application. ' );
@@ -225,15 +179,11 @@ protected function isGranted($attributes, $subject = null)
225
179
* Throws an exception unless the attributes are granted against the current authentication token and optionally
226
180
* supplied subject.
227
181
*
228
- * @param mixed $attributes The attributes
229
- * @param mixed $subject The subject
230
- * @param string $message The message passed to the exception
231
- *
232
182
* @throws AccessDeniedException
233
183
*
234
184
* @final since version 3.4
235
185
*/
236
- protected function denyAccessUnlessGranted ($ attributes , $ subject = null , $ message = 'Access Denied. ' )
186
+ protected function denyAccessUnlessGranted ($ attributes , $ subject = null , string $ message = 'Access Denied. ' )
237
187
{
238
188
if (!$ this ->isGranted ($ attributes , $ subject )) {
239
189
$ exception = $ this ->createAccessDeniedException ($ message );
@@ -247,14 +197,9 @@ protected function denyAccessUnlessGranted($attributes, $subject = null, $messag
247
197
/**
248
198
* Returns a rendered view.
249
199
*
250
- * @param string $view The view name
251
- * @param array $parameters An array of parameters to pass to the view
252
- *
253
- * @return string The rendered view
254
- *
255
200
* @final since version 3.4
256
201
*/
257
- protected function renderView ($ view , array $ parameters = array ())
202
+ protected function renderView (string $ view , array $ parameters = array ()): string
258
203
{
259
204
if ($ this ->container ->has ('templating ' )) {
260
205
return $ this ->container ->get ('templating ' )->render ($ view , $ parameters );
@@ -270,15 +215,9 @@ protected function renderView($view, array $parameters = array())
270
215
/**
271
216
* Renders a view.
272
217
*
273
- * @param string $view The view name
274
- * @param array $parameters An array of parameters to pass to the view
275
- * @param Response $response A response instance
276
- *
277
- * @return Response A Response instance
278
- *
279
218
* @final since version 3.4
280
219
*/
281
- protected function render ($ view , array $ parameters = array (), Response $ response = null )
220
+ protected function render (string $ view , array $ parameters = array (), Response $ response = null ): Response
282
221
{
283
222
if ($ this ->container ->has ('templating ' )) {
284
223
$ content = $ this ->container ->get ('templating ' )->render ($ view , $ parameters );
@@ -300,15 +239,9 @@ protected function render($view, array $parameters = array(), Response $response
300
239
/**
301
240
* Streams a view.
302
241
*
303
- * @param string $view The view name
304
- * @param array $parameters An array of parameters to pass to the view
305
- * @param StreamedResponse $response A response instance
306
- *
307
- * @return StreamedResponse A StreamedResponse instance
308
- *
309
242
* @final since version 3.4
310
243
*/
311
- protected function stream ($ view , array $ parameters = array (), StreamedResponse $ response = null )
244
+ protected function stream (string $ view , array $ parameters = array (), StreamedResponse $ response = null ): StreamedResponse
312
245
{
313
246
if ($ this ->container ->has ('templating ' )) {
314
247
$ templating = $ this ->container ->get ('templating ' );
@@ -342,14 +275,9 @@ protected function stream($view, array $parameters = array(), StreamedResponse $
342
275
*
343
276
* throw $this->createNotFoundException('Page not found!');
344
277
*
345
- * @param string $message A message
346
- * @param \Exception|null $previous The previous exception
347
- *
348
- * @return NotFoundHttpException
349
- *
350
278
* @final since version 3.4
351
279
*/
352
- protected function createNotFoundException ($ message = 'Not Found ' , \Exception $ previous = null )
280
+ protected function createNotFoundException (string $ message = 'Not Found ' , \Exception $ previous = null ): NotFoundHttpException
353
281
{
354
282
return new NotFoundHttpException ($ message , $ previous );
355
283
}
@@ -361,59 +289,41 @@ protected function createNotFoundException($message = 'Not Found', \Exception $p
361
289
*
362
290
* throw $this->createAccessDeniedException('Unable to access this page!');
363
291
*
364
- * @param string $message A message
365
- * @param \Exception|null $previous The previous exception
366
- *
367
- * @return AccessDeniedException
368
- *
369
292
* @final since version 3.4
370
293
*/
371
- protected function createAccessDeniedException ($ message = 'Access Denied. ' , \Exception $ previous = null )
294
+ protected function createAccessDeniedException (string $ message = 'Access Denied. ' , \Exception $ previous = null ): AccessDeniedException
372
295
{
373
296
return new AccessDeniedException ($ message , $ previous );
374
297
}
375
298
376
299
/**
377
300
* Creates and returns a Form instance from the type of the form.
378
301
*
379
- * @param string $type The fully qualified class name of the form type
380
- * @param mixed $data The initial data for the form
381
- * @param array $options Options for the form
382
- *
383
- * @return Form
384
- *
385
302
* @final since version 3.4
386
303
*/
387
- protected function createForm ($ type , $ data = null , array $ options = array ())
304
+ protected function createForm (string $ type , $ data = null , array $ options = array ()): FormInterface
388
305
{
389
306
return $ this ->container ->get ('form.factory ' )->create ($ type , $ data , $ options );
390
307
}
391
308
392
309
/**
393
310
* Creates and returns a form builder instance.
394
311
*
395
- * @param mixed $data The initial data for the form
396
- * @param array $options Options for the form
397
- *
398
- * @return FormBuilder
399
- *
400
312
* @final since version 3.4
401
313
*/
402
- protected function createFormBuilder ($ data = null , array $ options = array ())
314
+ protected function createFormBuilder ($ data = null , array $ options = array ()): FormBuilderInterface
403
315
{
404
316
return $ this ->container ->get ('form.factory ' )->createBuilder (FormType::class, $ data , $ options );
405
317
}
406
318
407
319
/**
408
320
* Shortcut to return the Doctrine Registry service.
409
321
*
410
- * @return Registry
411
- *
412
322
* @throws \LogicException If DoctrineBundle is not available
413
323
*
414
324
* @final since version 3.4
415
325
*/
416
- protected function getDoctrine ()
326
+ protected function getDoctrine (): ManagerRegistry
417
327
{
418
328
if (!$ this ->container ->has ('doctrine ' )) {
419
329
throw new \LogicException ('The DoctrineBundle is not registered in your application. ' );
@@ -457,11 +367,9 @@ protected function getUser()
457
367
* @param string $id The id used when generating the token
458
368
* @param string $token The actual token sent with the request that should be validated
459
369
*
460
- * @return bool
461
- *
462
370
* @final since version 3.4
463
371
*/
464
- protected function isCsrfTokenValid ($ id , $ token )
372
+ protected function isCsrfTokenValid (string $ id , string $ token ): bool
465
373
{
466
374
if (!$ this ->container ->has ('security.csrf.token_manager ' )) {
467
375
throw new \LogicException ('CSRF protection is not enabled in your application. ' );
0 commit comments