|
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\HttpFoundation\Request; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Custom ExceptionController that uses the view layer and supports HTTP response status code mapping. |
|
19
|
|
|
* It additionally is able to prepare the template parameters for the core EngineInterface. |
|
20
|
|
|
*/ |
|
21
|
|
|
class TwigExceptionController extends TemplatingExceptionController |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* {@inheritdoc} |
|
25
|
|
|
*/ |
|
26
|
4 |
|
protected function createView(\Exception $exception, $code, array $templateData, Request $request, $showException) |
|
27
|
|
|
{ |
|
28
|
4 |
|
$view = parent::createView($exception, $code, $templateData, $request, $showException); |
|
29
|
4 |
|
$view->setTemplate($this->findTemplate($request, $code, $showException)); |
|
30
|
|
|
|
|
31
|
4 |
|
return $view; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* {@inheritdoc} |
|
36
|
|
|
* |
|
37
|
|
|
* This code is inspired by TwigBundle and should be synchronized on a regular basis |
|
38
|
|
|
* see src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php |
|
39
|
|
|
*/ |
|
40
|
4 |
|
protected function findTemplate(Request $request, $statusCode, $showException) |
|
41
|
|
|
{ |
|
42
|
4 |
|
$format = $request->getRequestFormat(); |
|
43
|
|
|
|
|
44
|
4 |
|
$name = $showException ? 'exception' : 'error'; |
|
45
|
4 |
|
if ($showException && 'html' == $format) { |
|
46
|
1 |
|
$name = 'exception_full'; |
|
47
|
1 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
// when not in debug, try to find a template for the specific HTTP status code and format |
|
50
|
4 |
|
if (!$showException) { |
|
51
|
3 |
|
$template = new TemplateReference('TwigBundle', 'Exception', $name.$statusCode, $format, 'twig'); |
|
52
|
3 |
|
if ($this->templating->exists($template)) { |
|
53
|
|
|
return $template; |
|
54
|
|
|
} |
|
55
|
3 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
// try to find a template for the given format |
|
58
|
4 |
|
$template = new TemplateReference('TwigBundle', 'Exception', $name, $format, 'twig'); |
|
59
|
4 |
|
if ($this->templating->exists($template)) { |
|
60
|
4 |
|
return $template; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
// default to a generic HTML exception |
|
64
|
|
|
$request->setRequestFormat('html'); |
|
65
|
|
|
|
|
66
|
|
|
return new TemplateReference('TwigBundle', 'Exception', $showException ? 'exception_full' : $name, 'html', 'twig'); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|