1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSRestBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace FOS\RestBundle\Controller; |
13
|
|
|
|
14
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference; |
15
|
|
|
use Symfony\Component\Debug\Exception\FlattenException; |
16
|
|
|
use Symfony\Component\HttpFoundation\Request; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Custom ExceptionController that uses the view layer and supports HTTP response status code mapping. |
20
|
|
|
* It additionally is able to prepare the template parameters for the core EngineInterface. |
21
|
|
|
*/ |
22
|
|
|
class TwigExceptionController extends TemplatingExceptionController |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
|
|
protected function createView($format, FlattenException $exception, $code, $parameters, Request $request, $showException) |
28
|
|
|
{ |
29
|
|
|
$view = parent::createView($format, $exception, $code, $parameters, $request, $showException); |
|
|
|
|
30
|
|
|
$view->setTemplate($this->findTemplate($request, $format, $code, $showException)); |
31
|
|
|
|
32
|
|
|
return $view; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
* |
38
|
|
|
* This code is inspired by TwigBundle and should be synchronized on a regular basis |
39
|
|
|
* see src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php |
40
|
|
|
*/ |
41
|
|
|
protected function findTemplate(Request $request, $format, $statusCode, $showException) |
42
|
|
|
{ |
43
|
|
|
$name = $showException ? 'exception' : 'error'; |
44
|
|
|
if ($showException && 'html' == $format) { |
45
|
|
|
$name = 'exception_full'; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// when not in debug, try to find a template for the specific HTTP status code and format |
49
|
|
|
if (!$showException) { |
50
|
|
|
$template = new TemplateReference('TwigBundle', 'Exception', $name.$statusCode, $format, 'twig'); |
51
|
|
|
if ($this->templating->exists($template)) { |
52
|
|
|
return $template; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// try to find a template for the given format |
57
|
|
|
$template = new TemplateReference('TwigBundle', 'Exception', $name, $format, 'twig'); |
58
|
|
|
if ($this->templating->exists($template)) { |
59
|
|
|
return $template; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// default to a generic HTML exception |
63
|
|
|
$request->setRequestFormat('html'); |
64
|
|
|
|
65
|
|
|
return new TemplateReference('TwigBundle', 'Exception', $showException ? 'exception_full' : $name, 'html', 'twig'); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: