1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Alpixel\Bundle\CMSBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Symfony\Bundle\TwigBundle\Controller\ExceptionController as BaseController; |
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
7
|
|
|
use Symfony\Component\HttpFoundation\Response; |
8
|
|
|
use Symfony\Component\Debug\Exception\FlattenException; |
9
|
|
|
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface; |
10
|
|
|
|
11
|
|
|
class ExceptionController extends BaseController |
12
|
|
|
{ |
13
|
|
|
protected $exceptionTemplate; |
14
|
|
|
|
15
|
|
|
public function __construct(\Twig_Environment $twig, $debug, $exceptionTemplate) |
16
|
|
|
{ |
17
|
|
|
$this->exceptionTemplate = $exceptionTemplate; |
18
|
|
|
parent::__construct($twig, $debug); |
|
|
|
|
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function showAction(Request $request, FlattenException $exception, DebugLoggerInterface $logger = null) |
22
|
|
|
{ |
23
|
|
|
$currentContent = $this->getAndCleanOutputBuffering($request->headers->get('X-Php-Ob-Level', -1)); |
|
|
|
|
24
|
|
|
$showException = $request->attributes->get('showException', $this->debug); // As opposed to an additional parameter, this maintains BC |
25
|
|
|
|
26
|
|
|
$code = $exception->getStatusCode(); |
27
|
|
|
|
28
|
|
|
if ($showException) { |
29
|
|
|
$statusCode = Response::HTTP_ACCEPTED; |
30
|
|
|
} else { |
31
|
|
|
$statusCode = $code; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return new Response($this->twig->render( |
35
|
|
|
(string) $this->findTemplate($request, $request->getRequestFormat(), $code, $showException), |
36
|
|
|
[ |
37
|
|
|
'status_code' => $code, |
38
|
|
|
'status_text' => isset(Response::$statusTexts[$code]) ? Response::$statusTexts[$code] : '', |
39
|
|
|
'exception' => $exception, |
40
|
|
|
'logger' => $logger, |
41
|
|
|
'currentContent' => $currentContent, |
42
|
|
|
] |
43
|
|
|
), $statusCode); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param Request $request |
48
|
|
|
* @param string $format |
49
|
|
|
* @param int $code An HTTP response status code |
50
|
|
|
* @param bool $showException |
51
|
|
|
* |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
protected function findTemplate(Request $request, $format, $code, $showException) |
55
|
|
|
{ |
56
|
|
|
if (!$showException) { |
57
|
|
|
if ($this->templateExists($this->exceptionTemplate)) { |
58
|
|
|
return $this->exceptionTemplate; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return parent::findTemplate($request, $format, $code, $showException); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.