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
12namespace Symfony\Component\HttpKernel\Event;
13
14use Symfony\Component\HttpFoundation\Request;
15use 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 */
30final class ExceptionEvent extends RequestEvent
31{
32    private $throwable;
33
34    /**
35     * @var bool
36     */
37    private $allowCustomResponseCode = false;
38
39    public function __construct(HttpKernelInterface $kernel, Request $request, int $requestType, \Throwable $e)
40    {
41        parent::__construct($kernel, $request, $requestType);
42
43        $this->setThrowable($e);
44    }
45
46    public function getThrowable(): \Throwable
47    {
48        return $this->throwable;
49    }
50
51    /**
52     * Replaces the thrown exception.
53     *
54     * This exception will be thrown if no response is set in the event.
55     */
56    public function setThrowable(\Throwable $exception): void
57    {
58        $this->throwable = $exception;
59    }
60
61    /**
62     * Mark the event as allowing a custom response code.
63     */
64    public function allowCustomResponseCode(): void
65    {
66        $this->allowCustomResponseCode = true;
67    }
68
69    /**
70     * Returns true if the event allows a custom response code.
71     */
72    public function isAllowingCustomResponseCode(): bool
73    {
74        return $this->allowCustomResponseCode;
75    }
76}
77