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