Completed
Push — master ( 044f4e...849b11 )
by Lukas Kahwe
04:34
created

TwigExceptionController::setTemplating()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
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
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
18
19
/**
20
 * Custom ExceptionController that uses the view layer and supports HTTP response status code mapping.
21
 * It additionally is able to prepare the template parameters for the core EngineInterface.
22
 */
23
class TwigExceptionController extends TemplatingExceptionController
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    protected function createView($format, FlattenException $exception, $code, $parameters, Request $request, $showException)
29
    {
30
        $view = parent::createView($format, $exception, $code, $parameters, $request, $showException);
31
32
        if ($this->getViewHandler()->isFormatTemplating($format)) {
33
            $view->setTemplate($this->findTemplate($request, $format, $code, $showException));
34
            $view->setData($parameters);
35
        }
36
37
        return $view;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    protected function getParameters($currentContent, $code, $exception, DebugLoggerInterface $logger = null, $format = 'html')
44
    {
45
        $parameters = parent::getParameters($currentContent, $code, $exception, $logger, $format);
46
47
        if ($this->getViewHandler()->isFormatTemplating($format)) {
48
            $parameters['logger'] = $logger;
49
        }
50
51
        return $parameters;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     *
57
     * This code is inspired by TwigBundle and should be synchronized on a regular basis
58
     * see src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php
59
     */
60
    protected function findTemplate(Request $request, $format, $statusCode, $showException)
61
    {
62
        $name = $showException ? 'exception' : 'error';
63
        if ($showException && 'html' == $format) {
64
            $name = 'exception_full';
65
        }
66
67
        // when not in debug, try to find a template for the specific HTTP status code and format
68
        if (!$showException) {
69
            $template = new TemplateReference('TwigBundle', 'Exception', $name.$statusCode, $format, 'twig');
70
            if ($this->templating->exists($template)) {
71
                return $template;
72
            }
73
        }
74
75
        // try to find a template for the given format
76
        $template = new TemplateReference('TwigBundle', 'Exception', $name, $format, 'twig');
77
        if ($this->templating->exists($template)) {
78
            return $template;
79
        }
80
81
        // default to a generic HTML exception
82
        $request->setRequestFormat('html');
83
84
        return new TemplateReference('TwigBundle', 'Exception', $showException ? 'exception_full' : $name, 'html', 'twig');
85
    }
86
}
87