|
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\EngineInterface; |
|
15
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference; |
|
16
|
|
|
use Symfony\Component\Debug\Exception\FlattenException; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
18
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
19
|
|
|
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Custom ExceptionController that uses the view layer and supports HTTP response status code mapping. |
|
23
|
|
|
* It additionally is able to prepare the template parameters for the core EngineInterface. |
|
24
|
|
|
*/ |
|
25
|
|
|
class TwigExceptionController extends ExceptionController |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var EngineInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
private $templating; |
|
31
|
|
|
|
|
32
|
|
|
public function setTemplating(EngineInterface $templating) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->templating = $templating; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function getTemplating() |
|
38
|
|
|
{ |
|
39
|
|
|
if (!$this->templating instanceof EngineInterface) { |
|
40
|
|
|
throw new \RuntimeException('No templating engine set'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return $this->templating; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* {inheritDoc}. |
|
48
|
|
|
*/ |
|
49
|
|
|
protected function createView($format, FlattenException $exception, $code, $parameters, Request $request, $showException) |
|
50
|
|
|
{ |
|
51
|
|
|
$view = parent::createView($format, $exception, $code, $parameters, $request, $showException); |
|
52
|
|
|
|
|
53
|
|
|
if ($this->getViewHandler()->isFormatTemplating($format)) { |
|
54
|
|
|
$view->setTemplate($this->findTemplate($request, $format, $code, $showException)); |
|
55
|
|
|
$view->setData($parameters); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $view; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* {inheritDoc}. |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function getParameters($currentContent, $code, $exception, DebugLoggerInterface $logger = null, $format = 'html') |
|
65
|
|
|
{ |
|
66
|
|
|
$parameters = parent::getParameters($currentContent, $code, $exception, $logger, $format); |
|
67
|
|
|
|
|
68
|
|
|
if ($this->getViewHandler()->isFormatTemplating($format)) { |
|
69
|
|
|
$parameters['logger'] = $logger; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $parameters; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Finds the template for the given format and status code. |
|
77
|
|
|
* |
|
78
|
|
|
* Note this method needs to be overridden in case another |
|
79
|
|
|
* engine than Twig should be supported; |
|
80
|
|
|
* |
|
81
|
|
|
* This code is inspired by TwigBundle and should be synchronized on a regular basis |
|
82
|
|
|
* see src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php |
|
83
|
|
|
* |
|
84
|
|
|
* @param Request $request |
|
85
|
|
|
* @param string $format |
|
86
|
|
|
* @param int $statusCode |
|
87
|
|
|
* @param bool $showException |
|
88
|
|
|
* |
|
89
|
|
|
* @return TemplateReference |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function findTemplate(Request $request, $format, $statusCode, $showException) |
|
92
|
|
|
{ |
|
93
|
|
|
$name = $showException ? 'exception' : 'error'; |
|
94
|
|
|
if ($showException && 'html' == $format) { |
|
95
|
|
|
$name = 'exception_full'; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
// when not in debug, try to find a template for the specific HTTP status code and format |
|
99
|
|
|
if (!$showException) { |
|
100
|
|
|
$template = new TemplateReference('TwigBundle', 'Exception', $name.$statusCode, $format, 'twig'); |
|
101
|
|
|
if ($this->getTemplating()->exists($template)) { |
|
102
|
|
|
return $template; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
// try to find a template for the given format |
|
107
|
|
|
$template = new TemplateReference('TwigBundle', 'Exception', $name, $format, 'twig'); |
|
108
|
|
|
if ($this->getTemplating()->exists($template)) { |
|
109
|
|
|
return $template; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
// default to a generic HTML exception |
|
113
|
|
|
$request->setRequestFormat('html'); |
|
114
|
|
|
|
|
115
|
|
|
return new TemplateReference('TwigBundle', 'Exception', $showException ? 'exception_full' : $name, 'html', 'twig'); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|