<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\TwigBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* ExceptionController.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class ExceptionController
{
protected $twig;
protected $debug;
public function __construct(\Twig_Environment $twig, $debug)
{
$this->twig = $twig;
$this->debug = $debug;
}
/**
* Converts an Exception to a Response.
*
* @param Request $request The request
* @param FlattenException $exception A FlattenException instance
* @param DebugLoggerInterface $logger A DebugLoggerInterface instance
* @param string $_format The format to use for rendering (html, xml, ...)
*
* @return Response
*
* @throws \InvalidArgumentException When the exception template does not exist
*/
public function showAction(Request $request, FlattenException $exception, DebugLoggerInterface $logger = null, $_format = 'html')
{
$currentContent = $this->getAndCleanOutputBuffering($request->headers->get('X-Php-Ob-Level', -1));
$code = $exception->getStatusCode();
return new Response($this->twig->render(
$this->findTemplate($request, $_format, $code, $this->debug),
array(
'status_code' => $code,
'status_text' => isset(Response::$statusTexts[$code]) ? Response::$statusTexts[$code] : '',
'exception' => $exception,
'logger' => $logger,
'currentContent' => $currentContent,
)
));
}
/**
* @param integer $startObLevel
*
* @return string
*/
protected function getAndCleanOutputBuffering($startObLevel)
{
// ob_get_level() never returns 0 on some Windows configurations, so if
// the level is the same two times in a row, the loop should be stopped.
$previousObLevel = null;
$currentContent = '';
while (($obLevel = ob_get_level()) > $startObLevel && $obLevel !== $previousObLevel) {
$previousObLevel = $obLevel;
$currentContent .= ob_get_clean();
}
return $currentContent;
}
/**
* @param Request $request
* @param string $format
* @param integer $code An HTTP response status code
* @param Boolean $debug
*
* @return TemplateReference
*/
protected function findTemplate(Request $request, $format, $code, $debug)
{
$name = $debug ? 'exception' : 'error';
if ($debug && 'html' == $format) {
$name = 'exception_full';
}
// when not in debug, try to find a template for the specific HTTP status code and format
if (!$debug) {
$template = new TemplateReference('TwigBundle', 'Exception', $name.$code, $format, 'twig');
if ($this->templateExists($template)) {
return $template;
}
}
// try to find a template for the given format
$template = new TemplateReference('TwigBundle', 'Exception', $name, $format, 'twig');
if ($this->templateExists($template)) {
return $template;
}
// default to a generic HTML exception
$request->setRequestFormat('html');
return new TemplateReference('TwigBundle', 'Exception', $name, 'html', 'twig');
}
// to be removed when the minimum required version of Twig is >= 2.0
protected function templateExists($template)
{
$loader = $this->twig->getLoader();
if ($loader instanceof \Twig_ExistsLoaderInterface) {
return $loader->exists($template);
}
try {
$loader->getSource($template);
return true;
} catch (\Twig_Error_Loader $e) {
}
return false;
}
}
class ExceptionController2 extends ExceptionController
{
/**
* Converts an Exception to a Response.
*
* @param Request $request The request
* @param FlattenException $exception A FlattenException instance
* @param DebugLoggerInterface $logger A DebugLoggerInterface instance
* @param string $_format The format to use for rendering (html, xml, ...)
*
* @return Response
*
* @throws \InvalidArgumentException When the exception template does not exist
*/
public function showAction(Request $request, Symfony\Component\HttpKernel\Exception\FlattenException $exception, DebugLoggerInterface $logger = null, $_format = 'html')
{
$currentContent = $this->getAndCleanOutputBuffering($request->headers->get('X-Php-Ob-Level', -1));
$code = $exception->getStatusCode();
return new Response($this->twig->render(
$this->findTemplate($request, $_format, $code, $this->debug),
array(
'status_code' => $code,
'status_text' => isset(Response::$statusTexts[$code]) ? Response::$statusTexts[$code] : '',
'exception' => $exception,
'logger' => $logger,
'currentContent' => $currentContent,
)
));
}
}
Fatal error: Could not check compatibility between Symfony\Bundle\TwigBundle\Controller\ExceptionController2::showAction(Symfony\Component\HttpFoundation\Request $request, Symfony\Bundle\TwigBundle\Controller\Symfony\Component\HttpKernel\Exception\FlattenException $exception, ?Symfony\Component\HttpKernel\Log\DebugLoggerInterface $logger = null, $_format = 'html') and Symfony\Bundle\TwigBundle\Controller\ExceptionController::showAction(Symfony\Component\HttpFoundation\Request $request, Symfony\Component\Debug\Exception\FlattenException $exception, ?Symfony\Component\HttpKernel\Log\DebugLoggerInterface $logger = null, $_format = 'html'), because class Symfony\Component\Debug\Exception\FlattenException is not available in /in/2qNdW on line 156
Process exited with code 255.
Output for 7.4.0 - 7.4.33
Warning: Could not check compatibility between Symfony\Bundle\TwigBundle\Controller\ExceptionController2::showAction(Symfony\Component\HttpFoundation\Request $request, Symfony\Bundle\TwigBundle\Controller\Symfony\Component\HttpKernel\Exception\FlattenException $exception, ?Symfony\Component\HttpKernel\Log\DebugLoggerInterface $logger = NULL, $_format = 'html') and Symfony\Bundle\TwigBundle\Controller\ExceptionController::showAction(Symfony\Component\HttpFoundation\Request $request, Symfony\Component\Debug\Exception\FlattenException $exception, ?Symfony\Component\HttpKernel\Log\DebugLoggerInterface $logger = NULL, $_format = 'html'), because class Symfony\Component\Debug\Exception\FlattenException is not available in /in/2qNdW on line 156
Warning: Declaration of Symfony\Bundle\TwigBundle\Controller\ExceptionController2::showAction(Symfony\Component\HttpFoundation\Request $request, Symfony\Bundle\TwigBundle\Controller\Symfony\Component\HttpKernel\Exception\FlattenException $exception, ?Symfony\Component\HttpKernel\Log\DebugLoggerInterface $logger = NULL, $_format = 'html') should be compatible with Symfony\Bundle\TwigBundle\Controller\ExceptionController::showAction(Symfony\Component\HttpFoundation\Request $request, Symfony\Component\Debug\Exception\FlattenException $exception, ?Symfony\Component\HttpKernel\Log\DebugLoggerInterface $logger = NULL, $_format = 'html') in /in/2qNdW on line 174
Output for 7.0.0 - 7.0.20
Warning: Declaration of Symfony\Bundle\TwigBundle\Controller\ExceptionController2::showAction(Symfony\Component\HttpFoundation\Request $request, Symfony\Bundle\TwigBundle\Controller\Symfony\Component\HttpKernel\Exception\FlattenException $exception, Symfony\Component\HttpKernel\Log\DebugLoggerInterface $logger = NULL, $_format = 'html') should be compatible with Symfony\Bundle\TwigBundle\Controller\ExceptionController::showAction(Symfony\Component\HttpFoundation\Request $request, Symfony\Component\Debug\Exception\FlattenException $exception, Symfony\Component\HttpKernel\Log\DebugLoggerInterface $logger = NULL, $_format = 'html') in /in/2qNdW on line 174
Strict Standards: Declaration of Symfony\Bundle\TwigBundle\Controller\ExceptionController2::showAction() should be compatible with Symfony\Bundle\TwigBundle\Controller\ExceptionController::showAction(Symfony\Component\HttpFoundation\Request $request, Symfony\Component\Debug\Exception\FlattenException $exception, Symfony\Component\HttpKernel\Log\DebugLoggerInterface $logger = NULL, $_format = 'html') in /in/2qNdW on line 174
Output for 5.3.0 - 5.3.29
Strict Standards: Declaration of Symfony\Bundle\TwigBundle\Controller\ExceptionController2::showAction() should be compatible with that of Symfony\Bundle\TwigBundle\Controller\ExceptionController::showAction() in /in/2qNdW on line 174