Skip to content

Instantly share code, notes, and snippets.

@KevinSleegers
Created May 21, 2016 13:46
Show Gist options
  • Save KevinSleegers/8e01f3870df9a30f0cf0d75f053ec7be to your computer and use it in GitHub Desktop.
Save KevinSleegers/8e01f3870df9a30f0cf0d75f053ec7be to your computer and use it in GitHub Desktop.
use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\FlattenException;
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
use Symfony\Component\HttpKernel\HttpKernelInterface;
class ExceptionController extends \Symfony\Bundle\TwigBundle\Controller\ExceptionController
{
protected $container;
public function __construct(\Twig_Environment $twig, ContainerInterface $container)
{
$this->container = $container;
$this->debug = $this->container->get( 'kernel' )->getEnvironment() == 'dev' ? true : false;
parent::__construct($twig, $this->debug);
}
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();
if ($code != '404' /*|| $this->debug == true*/)
{
return new Response($this->twig->render(
$this->findTemplate($request, $request->getRequestFormat(), $code, true),
array(
'status_code' => $code,
'status_text' => isset(Response::$statusTexts[$code]) ? Response::$statusTexts[$code] : '',
'exception' => $exception,
'logger' => $logger,
'currentContent' => $currentContent,
)
));
}
else
{
/** @var EntityManager $em */
$em = $this->container->get('doctrine')->getManager();
$errorNodeTranslation = $em->getRepository('KunstmaanNodeBundle:NodeTranslation')->getNodeTranslationByLanguageAndInternalName('nl', 'error');
if ($errorNodeTranslation)
{
if ($this->container->get('security.context')->getToken() === null)
{
$token = new AnonymousToken('1234567', 'anon.', array());
$this->container->get('security.context')->setToken($token);
}
$params = array(
'url' => $errorNodeTranslation->getUrl(),
'request' => $request
);
return $this->forward('KunstmaanNodeBundle:Slug:slug', $params);
}
else
{
return new Response($this->twig->render(
$this->findTemplate($request, $request->getRequestFormat(), $code, $showException),
array(
'status_code' => $code,
'status_text' => isset(Response::$statusTexts[$code]) ? Response::$statusTexts[$code] : '',
'exception' => $exception,
'logger' => $logger,
'currentContent' => $currentContent,
)
));
}
}
}
/**
* Forwards the request to another controller.
*
* @param string $controller The controller name (a string like BlogBundle:Post:index)
* @param array $path An array of path parameters
* @param array $query An array of query parameters
*
* @return Response A Response instance
*/
private function forward($controller, array $path = array(), array $query = array())
{
$path['_controller'] = $controller;
$subRequest = $this->container->get('request')->duplicate($query, null, $path);
return $this->container->get('http_kernel')->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment