1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* It's free open-source software released under the MIT License. |
5
|
|
|
* |
6
|
|
|
* @author Anatoly Fenric <[email protected]> |
7
|
|
|
* @copyright Copyright (c) 2018, Anatoly Fenric |
8
|
|
|
* @license https://github.com/sunrise-php/http-router/blob/master/LICENSE |
9
|
|
|
* @link https://github.com/sunrise-php/http-router |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sunrise\Http\Router\Exception; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Import functions |
16
|
|
|
*/ |
17
|
|
|
use function sprintf; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* ExceptionFactory |
21
|
|
|
*/ |
22
|
|
|
class ExceptionFactory |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Used by a routes loader when the given resource wasn't found as a file |
27
|
|
|
* |
28
|
|
|
* @param mixed $resource |
29
|
|
|
* @param array $context |
30
|
|
|
* |
31
|
|
|
* @return InvalidLoaderResourceException |
32
|
|
|
*/ |
33
|
3 |
|
public function invalidLoaderFileResource($resource, array $context = []) : InvalidLoaderResourceException |
34
|
|
|
{ |
35
|
3 |
|
$message = sprintf('The resource "%s" is not found.', $resource); |
36
|
|
|
|
37
|
3 |
|
$context['resource'] = $resource; |
38
|
|
|
|
39
|
3 |
|
return new InvalidLoaderResourceException($message, $context); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Used when the given method isn't allowed |
44
|
|
|
* |
45
|
|
|
* @param string $method |
46
|
|
|
* @param array $allowed |
47
|
|
|
* @param array $context |
48
|
|
|
* |
49
|
|
|
* @return MethodNotAllowedException |
50
|
|
|
*/ |
51
|
4 |
|
public function methodNotAllowed(string $method, array $allowed, array $context = []) : MethodNotAllowedException |
52
|
|
|
{ |
53
|
4 |
|
$message = sprintf('The method "%s" is not allowed.', $method); |
54
|
|
|
|
55
|
4 |
|
$context['method'] = $method; |
56
|
4 |
|
$context['allowed'] = $allowed; |
57
|
|
|
|
58
|
4 |
|
return new MethodNotAllowedException($message, $context); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Used when a middleware with the given hash already exists in a stack |
63
|
|
|
* |
64
|
|
|
* It is recommended to add a middleware instance to the context. |
65
|
|
|
* |
66
|
|
|
* @param string $hash |
67
|
|
|
* @param array $context |
68
|
|
|
* |
69
|
|
|
* @return MiddlewareAlreadyExistsException |
70
|
|
|
*/ |
71
|
2 |
|
public function middlewareAlreadyExists(string $hash, array $context = []) : MiddlewareAlreadyExistsException |
72
|
|
|
{ |
73
|
2 |
|
$message = sprintf('A middleware with the hash "%s" already exists.', $hash); |
74
|
|
|
|
75
|
2 |
|
$context['hash'] = $hash; |
76
|
|
|
|
77
|
2 |
|
return new MiddlewareAlreadyExistsException($message, $context); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Used when a route with the given name already exists in a stack |
82
|
|
|
* |
83
|
|
|
* It is recommended to add a route instance to the context. |
84
|
|
|
* |
85
|
|
|
* @param string $name |
86
|
|
|
* @param array $context |
87
|
|
|
* |
88
|
|
|
* @return RouteAlreadyExistsException |
89
|
|
|
*/ |
90
|
2 |
|
public function routeAlreadyExists(string $name, array $context = []) : RouteAlreadyExistsException |
91
|
|
|
{ |
92
|
2 |
|
$message = sprintf('A route with the name "%s" already exists.', $name); |
93
|
|
|
|
94
|
2 |
|
$context['name'] = $name; |
95
|
|
|
|
96
|
2 |
|
return new RouteAlreadyExistsException($message, $context); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Used when trying to find a route by the given name |
101
|
|
|
* |
102
|
|
|
* @param string $name |
103
|
|
|
* @param array $context |
104
|
|
|
* |
105
|
|
|
* @return RouteNotFoundException |
106
|
|
|
*/ |
107
|
2 |
|
public function routeNotFoundByName(string $name, array $context = []) : RouteNotFoundException |
108
|
|
|
{ |
109
|
2 |
|
$message = sprintf('No route found for the name "%s".', $name); |
110
|
|
|
|
111
|
2 |
|
$context['name'] = $name; |
112
|
|
|
|
113
|
2 |
|
return new RouteNotFoundException($message, $context); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Used when trying to find a route by the given URI |
118
|
|
|
* |
119
|
|
|
* @param string $uri |
120
|
|
|
* @param array $context |
121
|
|
|
* |
122
|
|
|
* @return RouteNotFoundException |
123
|
|
|
*/ |
124
|
4 |
|
public function routeNotFoundByUri(string $uri, array $context = []) : RouteNotFoundException |
125
|
|
|
{ |
126
|
4 |
|
$message = sprintf('No route found for the URI "%s".', $uri); |
127
|
|
|
|
128
|
4 |
|
$context['uri'] = $uri; |
129
|
|
|
|
130
|
4 |
|
return new RouteNotFoundException($message, $context); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|