forked from kutny/tracy-bundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KernelExceptionListener.php
67 lines (55 loc) · 2.08 KB
/
KernelExceptionListener.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
namespace Kutny\TracyBundle;
use Exception;
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Tracy\Debugger;
class KernelExceptionListener
{
private $storeUsernameInServerVariable;
private $tokenStorage;
/** @var array */
private $ignoredExceptions;
public function __construct($storeUsernameInServerVariable, TokenStorageInterface $tokenStorage = null, array $ignoredExceptions)
{
$this->storeUsernameInServerVariable = $storeUsernameInServerVariable;
$this->tokenStorage = $tokenStorage;
$this->ignoredExceptions = $ignoredExceptions;
}
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
if (Debugger::isEnabled() && !$this->isIgnoredException($exception)) {
if ($this->storeUsernameInServerVariable && $this->tokenStorage !== null) {
$this->storeUsernameInServerVariable();
}
if (Debugger::$productionMode === true) {
Debugger::log($exception, Debugger::ERROR);
}
else if (Debugger::$productionMode === false) {
ob_start();
Debugger::exceptionHandler($exception, true);
$event->setResponse(new Response(ob_get_contents()));
ob_clean();
}
}
}
public function onConsoleException(ConsoleExceptionEvent $event)
{
$exception = $event->getException();
Debugger::log($exception, Debugger::ERROR);
}
private function isIgnoredException(Exception $exception)
{
return isset($this->ignoredExceptions[get_class($exception)]);
}
private function storeUsernameInServerVariable()
{
$token = $this->tokenStorage->getToken();
if ($token) {
$_SERVER['SYMFONY_USERNAME'] = $token->getUsername();
}
}
}