|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Symfony package. |
|
|
5 |
* |
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.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 Symfony\Bundle\TwigBundle\Controller; |
|
|
13 |
|
|
|
14 |
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference; |
|
|
15 |
use Symfony\Component\DependencyInjection\ContainerAware; |
|
|
16 |
use Symfony\Component\HttpKernel\Exception\FlattenException; |
|
|
17 |
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface; |
|
|
18 |
use Symfony\Component\HttpFoundation\Response; |
|
|
19 |
|
|
|
20 |
/** |
|
|
21 |
* ExceptionController. |
|
|
22 |
* |
|
|
23 |
* @author Fabien Potencier <fabien@symfony.com> |
|
|
24 |
*/ |
|
|
25 |
class ExceptionController extends ContainerAware |
|
|
26 |
{ |
|
|
27 |
/** |
|
|
28 |
* Converts an Exception to a Response. |
|
|
29 |
* |
|
|
30 |
* @param FlattenException $exception A FlattenException instance |
|
|
31 |
* @param DebugLoggerInterface $logger A DebugLoggerInterface instance |
|
|
32 |
* @param string $format The format to use for rendering (html, xml, ...) |
|
|
33 |
* |
|
|
34 |
* @throws \InvalidArgumentException When the exception template does not exist |
|
|
35 |
*/ |
|
|
36 |
public function showAction(FlattenException $exception, DebugLoggerInterface $logger = null, $format = 'html') |
|
|
37 |
{ |
|
|
38 |
$this->container->get('request')->setRequestFormat($format); |
|
|
39 |
|
|
|
40 |
// the count variable avoids an infinite loop on |
|
|
41 |
// some Windows configurations where ob_get_level() |
|
|
42 |
// never reaches 0 |
|
|
43 |
$count = 100; |
|
|
44 |
$currentContent = ''; |
|
|
45 |
while (ob_get_level() && --$count) { |
|
|
46 |
$currentContent .= ob_get_clean(); |
|
|
47 |
} |
|
|
48 |
|
|
|
49 |
$templating = $this->container->get('templating'); |
|
|
50 |
$code = $exception->getStatusCode(); |
|
|
51 |
|
|
|
52 |
$response = $templating->renderResponse( |
|
|
53 |
$this->findTemplate($templating, $format, $code, $this->container->get('kernel')->isDebug()), |
|
|
54 |
array( |
|
|
55 |
'status_code' => $code, |
|
|
56 |
'status_text' => Response::$statusTexts[$code], |
|
|
57 |
'exception' => $exception, |
|
|
58 |
'logger' => $logger, |
|
|
59 |
'currentContent' => $currentContent, |
|
|
60 |
) |
|
|
61 |
); |
|
|
62 |
|
|
|
63 |
$response->setStatusCode($code); |
|
|
64 |
$response->headers->replace($exception->getHeaders()); |
|
|
65 |
|
|
|
66 |
return $response; |
|
|
67 |
} |
|
|
68 |
|
|
|
69 |
protected function findTemplate($templating, $format, $code, $debug) |
|
|
70 |
{ |
|
|
71 |
$name = $debug ? 'exception' : 'error'; |
|
|
72 |
if ($debug && 'html' == $format) { |
|
|
73 |
$name = 'exception_full'; |
|
|
74 |
} |
|
|
75 |
|
|
|
76 |
// when not in debug, try to find a template for the specific HTTP status code and format |
|
|
77 |
if (!$debug) { |
|
|
78 |
$template = new TemplateReference('TwigBundle', 'Exception', $name.$code, $format, 'twig'); |
|
|
79 |
if ($templating->exists($template)) { |
|
|
80 |
return $template; |
|
|
81 |
} |
|
|
82 |
} |
|
|
83 |
|
|
|
84 |
// try to find a template for the given format |
|
|
85 |
$template = new TemplateReference('TwigBundle', 'Exception', $name, $format, 'twig'); |
|
|
86 |
if ($templating->exists($template)) { |
|
|
87 |
return $template; |
|
|
88 |
} |
|
|
89 |
|
|
|
90 |
// default to a generic HTML exception |
|
|
91 |
$this->container->get('request')->setRequestFormat('html'); |
|
|
92 |
|
|
|
93 |
return new TemplateReference('TwigBundle', 'Exception', $name, 'html', 'twig'); |
|
|
94 |
} |
|
|
95 |
} |