mirror of
https://github.com/Combodo/iTop.git
synced 2026-02-26 21:54:13 +01:00
Package operations: 2 installs, 23 updates, 0 removals - Updating psr/log (1.1.0 => 1.1.2) - Updating symfony/debug (v3.4.30 => v3.4.35) - Updating symfony/console (v3.4.30 => v3.4.35) - Updating symfony/dotenv (v3.4.30 => v3.4.35) - Updating symfony/routing (v3.4.30 => v3.4.35) - Updating symfony/finder (v3.4.30 => v3.4.35) - Updating symfony/filesystem (v3.4.30 => v3.4.35) - Installing symfony/polyfill-util (v1.12.0) - Installing symfony/polyfill-php56 (v1.12.0) - Updating symfony/http-foundation (v3.4.30 => v3.4.35) - Updating symfony/event-dispatcher (v3.4.30 => v3.4.35) - Updating symfony/http-kernel (v3.4.30 => v3.4.35) - Updating symfony/config (v3.4.30 => v3.4.35) - Updating symfony/dependency-injection (v3.4.30 => v3.4.35) - Updating symfony/class-loader (v3.4.30 => v3.4.35) - Updating symfony/cache (v3.4.30 => v3.4.35) - Updating symfony/framework-bundle (v3.4.30 => v3.4.35) - Updating twig/twig (v1.42.2 => v1.42.4) - Updating symfony/twig-bridge (v3.4.30 => v3.4.35) - Updating symfony/twig-bundle (v3.4.30 => v3.4.35) - Updating symfony/yaml (v3.4.30 => v3.4.35) - Updating symfony/stopwatch (v3.4.30 => v3.4.35) - Updating symfony/var-dumper (v3.4.30 => v3.4.35) - Updating symfony/web-profiler-bundle (v3.4.30 => v3.4.35) - Updating symfony/css-selector (v3.4.30 => v3.4.35)
149 lines
4.7 KiB
PHP
149 lines
4.7 KiB
PHP
<?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\Component\Debug\Exception\FlattenException;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
|
|
use Twig\Environment;
|
|
use Twig\Error\LoaderError;
|
|
use Twig\Loader\ExistsLoaderInterface;
|
|
use Twig\Loader\SourceContextLoaderInterface;
|
|
|
|
/**
|
|
* ExceptionController renders error or exception pages for a given
|
|
* FlattenException.
|
|
*
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
|
* @author Matthias Pigulla <mp@webfactory.de>
|
|
*/
|
|
class ExceptionController
|
|
{
|
|
protected $twig;
|
|
protected $debug;
|
|
|
|
/**
|
|
* @param bool $debug Show error (false) or exception (true) pages by default
|
|
*/
|
|
public function __construct(Environment $twig, $debug)
|
|
{
|
|
$this->twig = $twig;
|
|
$this->debug = $debug;
|
|
}
|
|
|
|
/**
|
|
* Converts an Exception to a Response.
|
|
*
|
|
* A "showException" request parameter can be used to force display of an error page (when set to false) or
|
|
* the exception page (when true). If it is not present, the "debug" value passed into the constructor will
|
|
* be used.
|
|
*
|
|
* @return Response
|
|
*
|
|
* @throws \InvalidArgumentException When the exception template does not exist
|
|
*/
|
|
public function showAction(Request $request, FlattenException $exception, DebugLoggerInterface $logger = null)
|
|
{
|
|
$currentContent = $this->getAndCleanOutputBuffering($request->headers->get('X-Php-Ob-Level', -1));
|
|
$showException = $request->attributes->get('showException', $this->debug); // As opposed to an additional parameter, this maintains BC
|
|
|
|
$code = $exception->getStatusCode();
|
|
|
|
return new Response($this->twig->render(
|
|
(string) $this->findTemplate($request, $request->getRequestFormat(), $code, $showException),
|
|
[
|
|
'status_code' => $code,
|
|
'status_text' => isset(Response::$statusTexts[$code]) ? Response::$statusTexts[$code] : '',
|
|
'exception' => $exception,
|
|
'logger' => $logger,
|
|
'currentContent' => $currentContent,
|
|
]
|
|
), 200, ['Content-Type' => $request->getMimeType($request->getRequestFormat()) ?: 'text/html']);
|
|
}
|
|
|
|
/**
|
|
* @param int $startObLevel
|
|
*
|
|
* @return string
|
|
*/
|
|
protected function getAndCleanOutputBuffering($startObLevel)
|
|
{
|
|
if (ob_get_level() <= $startObLevel) {
|
|
return '';
|
|
}
|
|
|
|
Response::closeOutputBuffers($startObLevel + 1, true);
|
|
|
|
return ob_get_clean();
|
|
}
|
|
|
|
/**
|
|
* @param string $format
|
|
* @param int $code An HTTP response status code
|
|
* @param bool $showException
|
|
*
|
|
* @return string
|
|
*/
|
|
protected function findTemplate(Request $request, $format, $code, $showException)
|
|
{
|
|
$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, $code, $format);
|
|
if ($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->templateExists($template)) {
|
|
return $template;
|
|
}
|
|
|
|
// default to a generic HTML exception
|
|
$request->setRequestFormat('html');
|
|
|
|
return sprintf('@Twig/Exception/%s.html.twig', $showException ? 'exception_full' : $name);
|
|
}
|
|
|
|
// to be removed when the minimum required version of Twig is >= 2.0
|
|
protected function templateExists($template)
|
|
{
|
|
$template = (string) $template;
|
|
|
|
$loader = $this->twig->getLoader();
|
|
|
|
if (1 === Environment::MAJOR_VERSION && !$loader instanceof ExistsLoaderInterface) {
|
|
try {
|
|
if ($loader instanceof SourceContextLoaderInterface) {
|
|
$loader->getSourceContext($template);
|
|
} else {
|
|
$loader->getSource($template);
|
|
}
|
|
|
|
return true;
|
|
} catch (LoaderError $e) {
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
return $loader->exists($template);
|
|
}
|
|
}
|