|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Biurad opensource projects. |
|
5
|
|
|
* |
|
6
|
|
|
* @copyright 2019 Biurad Group (https://biurad.com/) |
|
7
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
|
8
|
|
|
* |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Flange\Event; |
|
14
|
|
|
|
|
15
|
|
|
use Flange\Application; |
|
16
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Allows to create a response for a thrown exception. |
|
20
|
|
|
* |
|
21
|
|
|
* Call setResponse() to set the response that will be returned for the |
|
22
|
|
|
* current request. The propagation of this event is stopped as soon as a |
|
23
|
|
|
* response is set. |
|
24
|
|
|
* |
|
25
|
|
|
* You can also call setThrowable() to replace the thrown exception. This |
|
26
|
|
|
* exception will be thrown if no response is set during processing of this |
|
27
|
|
|
* event. |
|
28
|
|
|
* |
|
29
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
|
30
|
|
|
*/ |
|
31
|
|
|
final class ExceptionEvent extends RequestEvent |
|
32
|
|
|
{ |
|
33
|
|
|
private \Throwable $throwable; |
|
34
|
|
|
private bool $allowCustomResponseCode = false; |
|
35
|
|
|
|
|
36
|
|
|
public function __construct(Application $kernel, Request $request, \Throwable $e) |
|
37
|
|
|
{ |
|
38
|
|
|
parent::__construct($kernel, $request); |
|
39
|
|
|
$this->setThrowable($e); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function getThrowable(): \Throwable |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->throwable; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Replaces the thrown exception. |
|
49
|
|
|
* |
|
50
|
|
|
* This exception will be thrown if no response is set in the event. |
|
51
|
|
|
*/ |
|
52
|
|
|
public function setThrowable(\Throwable $exception): void |
|
53
|
|
|
{ |
|
54
|
|
|
$this->throwable = $exception; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Mark the event as allowing a custom response code. |
|
59
|
|
|
*/ |
|
60
|
|
|
public function allowCustomResponseCode(): void |
|
61
|
|
|
{ |
|
62
|
|
|
$this->allowCustomResponseCode = true; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Returns true if the event allows a custom response code. |
|
67
|
|
|
*/ |
|
68
|
|
|
public function isAllowingCustomResponseCode(): bool |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->allowCustomResponseCode; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|