1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Majima\Services; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\DependencyInjection\Container; |
6
|
|
|
use Symfony\Component\HttpFoundation\Response; |
7
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; |
8
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
9
|
|
|
use Symfony\Component\Routing\RouterInterface; |
10
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken; |
11
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class ExceptionHandler |
15
|
|
|
* @package Golli\Services |
16
|
|
|
*/ |
17
|
|
|
class ExceptionHandler implements ExceptionHandlerInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var Container |
21
|
|
|
*/ |
22
|
|
|
private $container; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var RouterInterface |
26
|
|
|
*/ |
27
|
|
|
private $router; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* ExceptionHandler constructor. |
31
|
|
|
* @param Container $container |
32
|
|
|
* @param RouterInterface $router |
33
|
|
|
*/ |
34
|
|
|
public function __construct( |
35
|
|
|
Container $container, |
36
|
|
|
RouterInterface $router |
37
|
|
|
) |
38
|
|
|
{ |
39
|
|
|
$this->container = $container; |
40
|
|
|
$this->router = $router; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var GetResponseForExceptionEvent $event |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
|
|
public function onKernelException(GetResponseForExceptionEvent $event) |
48
|
|
|
{ |
49
|
|
|
/** @var DwooEngineFactory $engine */ |
50
|
|
|
$engine = $this->container->get('dwoo.engine'); |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var TokenStorage $tokenStorage |
54
|
|
|
*/ |
55
|
|
|
$tokenStorage = $this->container->get('security.token_storage'); |
56
|
|
|
$tokenStorage->setToken(new AnonymousToken('default', 'anon.')); |
57
|
|
|
|
58
|
|
|
// If not a HttpNotFoundException ignore |
59
|
|
|
if ($event->getException() instanceof NotFoundHttpException) { |
60
|
|
|
$response = $engine->render('Exception/404.tpl'); |
61
|
|
|
$response->setStatusCode(400); |
62
|
|
|
$event->setResponse( |
63
|
|
|
$response |
64
|
|
|
); |
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$engine->setData([ |
69
|
|
|
"errorMessage" => $event->getException()->getMessage(), |
70
|
|
|
"exception" => $event->getException()->getTraceAsString() |
71
|
|
|
]); |
72
|
|
|
$response = $engine->render('Exception/error.tpl'); |
73
|
|
|
$response->setStatusCode(500); |
74
|
|
|
$event->setResponse( |
75
|
|
|
$response |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
} |