1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Valkyrja Framework package. |
7
|
|
|
* |
8
|
|
|
* (c) Melech Mizrachi <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Valkyrja\Routing\Support; |
15
|
|
|
|
16
|
|
|
use Valkyrja\Http\Constants\StatusCode; |
17
|
|
|
use Valkyrja\Http\Exceptions\HttpException; |
18
|
|
|
use Valkyrja\Http\Exceptions\HttpRedirectException; |
19
|
|
|
use Valkyrja\Http\Response; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class Abort. |
23
|
|
|
* |
24
|
|
|
* @author Melech Mizrachi |
25
|
|
|
*/ |
26
|
|
|
class Abort |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Abort with a 404. |
30
|
|
|
* |
31
|
|
|
* @param array|null $headers [optional] The headers |
32
|
|
|
* @param Response|null $response [optional] The response to send |
33
|
|
|
* |
34
|
|
|
* @return void |
35
|
|
|
*/ |
36
|
|
|
public static function abort404(array $headers = null, Response $response = null): void |
37
|
|
|
{ |
38
|
|
|
static::abort(StatusCode::NOT_FOUND, '404', $headers, $response); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Abort. |
43
|
|
|
* |
44
|
|
|
* @param int|null $statusCode [optional] The status code |
45
|
|
|
* @param string|null $message [optional] The message |
46
|
|
|
* @param array|null $headers [optional] The headers |
47
|
|
|
* @param Response|null $response [optional] The response to send |
48
|
|
|
* |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
|
|
public static function abort( |
52
|
|
|
int $statusCode = null, |
53
|
|
|
string $message = null, |
54
|
|
|
array $headers = null, |
55
|
|
|
Response $response = null |
56
|
|
|
): void { |
57
|
|
|
throw new HttpException($statusCode, $message, $headers, $response); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Abort with a response. |
62
|
|
|
* |
63
|
|
|
* @param Response $response The response |
64
|
|
|
* |
65
|
|
|
* @return void |
66
|
|
|
*/ |
67
|
|
|
public static function response(Response $response): void |
68
|
|
|
{ |
69
|
|
|
self::abort(null, null, null, $response); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Redirect to a given uri, and abort. |
74
|
|
|
* |
75
|
|
|
* @param string|null $uri [optional] The URI to redirect to |
76
|
|
|
* @param int|null $statusCode [optional] The response status code |
77
|
|
|
* @param array|null $headers [optional] An array of response headers |
78
|
|
|
* |
79
|
|
|
* @throws HttpRedirectException |
80
|
|
|
* |
81
|
|
|
* @return void |
82
|
|
|
*/ |
83
|
|
|
public static function redirect(string $uri = null, int $statusCode = null, array $headers = null): void |
84
|
|
|
{ |
85
|
|
|
throw new HttpRedirectException($statusCode, $uri, $headers); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|