| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\HttpKernel\Event;
|
|
|
13 |
|
|
|
14 |
use Symfony\Component\HttpFoundation\Request;
|
|
|
15 |
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Allows to create a response for a thrown exception.
|
|
|
19 |
*
|
|
|
20 |
* Call setResponse() to set the response that will be returned for the
|
|
|
21 |
* current request. The propagation of this event is stopped as soon as a
|
|
|
22 |
* response is set.
|
|
|
23 |
*
|
|
|
24 |
* You can also call setThrowable() to replace the thrown exception. This
|
|
|
25 |
* exception will be thrown if no response is set during processing of this
|
|
|
26 |
* event.
|
|
|
27 |
*
|
|
|
28 |
* @author Bernhard Schussek <bschussek@gmail.com>
|
|
|
29 |
*/
|
|
|
30 |
final class ExceptionEvent extends RequestEvent
|
|
|
31 |
{
|
|
|
32 |
private \Throwable $throwable;
|
|
|
33 |
private bool $allowCustomResponseCode = false;
|
|
|
34 |
|
|
|
35 |
public function __construct(HttpKernelInterface $kernel, Request $request, int $requestType, \Throwable $e)
|
|
|
36 |
{
|
|
|
37 |
parent::__construct($kernel, $request, $requestType);
|
|
|
38 |
|
|
|
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 |
}
|