forked from mossodev/FOSRestBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwigExceptionController.php
100 lines (85 loc) · 3.37 KB
/
TwigExceptionController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
/*
* This file is part of the FOSRestBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\RestBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Templating\EngineInterface;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Loader\ExistsLoaderInterface;
/**
* Custom ExceptionController that uses the view layer and supports HTTP response status code mapping.
* It additionally is able to prepare the template parameters for the core EngineInterface.
*/
class TwigExceptionController extends TemplatingExceptionController
{
/**
* {@inheritdoc}
*/
protected function createView(\Exception $exception, $code, array $templateData, Request $request, $showException)
{
$view = parent::createView($exception, $code, $templateData, $request, $showException);
$view->setTemplate($this->findTemplate($request, $code, $showException));
return $view;
}
/**
* {@inheritdoc}
*
* This code is inspired by TwigBundle and should be synchronized on a regular basis
* see src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php
*/
protected function findTemplate(Request $request, $statusCode, $showException)
{
$format = $request->getRequestFormat();
$name = $showException ? 'exception' : 'error';
if ($showException && 'html' == $format) {
$name = 'exception_full';
}
// For error pages, try to find a template for the specific HTTP status code and format
if (!$showException) {
$template = sprintf('@Twig/Exception/%s%s.%s.twig', $name, $statusCode, $format);
if (
($this->templating instanceof EngineInterface && $this->templating->exists($template)) ||
($this->templating instanceof Environment && $this->templateExists($template))
) {
return $template;
}
}
// try to find a template for the given format
$template = sprintf('@Twig/Exception/%s.%s.twig', $name, $format);
if (
($this->templating instanceof EngineInterface && $this->templating->exists($template)) ||
($this->templating instanceof Environment && $this->templateExists($template))
) {
return $template;
}
// default to a generic HTML exception
$request->setRequestFormat('html');
return sprintf('@Twig/Exception/%s.html.twig', $showException ? 'exception_full' : $name);
}
/**
* See if a template exists using the modern Twig mechanism.
*
* This code is based on TwigBundle and should be removed when the minimum required
* version of Twig is >= 3.0. See src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php
*/
private function templateExists(string $template): bool
{
$loader = $this->templating->getLoader();
if ($loader instanceof ExistsLoaderInterface || method_exists($loader, 'exists')) {
return $loader->exists($template);
}
try {
$loader->getSourceContext($template)->getCode();
return true;
} catch (LoaderError $e) {
}
return false;
}
}