|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Router; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class Route |
|
7
|
|
|
* @package Router |
|
8
|
|
|
*/ |
|
9
|
|
|
class Route |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var BaseRouter |
|
13
|
|
|
*/ |
|
14
|
|
|
private $router; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var |
|
18
|
|
|
*/ |
|
19
|
|
|
private static $route; |
|
20
|
|
|
/** |
|
21
|
|
|
* @var null |
|
22
|
|
|
*/ |
|
23
|
|
|
private static $match = null; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Route constructor. |
|
27
|
|
|
*/ |
|
28
|
|
|
private function __construct() |
|
29
|
|
|
{ |
|
30
|
|
|
$this->router = new BaseRouter(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @return Route |
|
35
|
|
|
*/ |
|
36
|
|
|
private static function route(): Route |
|
37
|
|
|
{ |
|
38
|
|
|
if (!isset(self::$route) || is_null(self::$route)) { |
|
39
|
|
|
self::$route = new Route; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return self::$route; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return null|BaseRoute |
|
47
|
|
|
*/ |
|
48
|
|
|
private static function match(): ?BaseRoute |
|
49
|
|
|
{ |
|
50
|
|
|
if (!isset(self::$match) || is_null(self::$match)) { |
|
51
|
|
|
self::$match = self::route()->router->handle(); |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return self::$match; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param string $method |
|
59
|
|
|
* @param string $uri |
|
60
|
|
|
* @param $callback |
|
61
|
|
|
* @return void |
|
62
|
|
|
*/ |
|
63
|
|
|
private function handle(string $method, string $uri, $callback): void |
|
64
|
|
|
{ |
|
65
|
|
|
$this->router->add([ |
|
66
|
|
|
'uri' => $uri, |
|
67
|
|
|
'name' => '', |
|
68
|
|
|
'method' => $method, |
|
69
|
|
|
'callback' => $callback |
|
70
|
|
|
]); |
|
71
|
|
|
|
|
72
|
|
|
$match = $this->match(); |
|
73
|
|
|
|
|
74
|
|
|
call_user_func_array($match->getCallback(), $match->getArgs()); |
|
75
|
|
|
|
|
76
|
|
|
exit; |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param string $uri |
|
81
|
|
|
* @param $callback |
|
82
|
|
|
*/ |
|
83
|
|
|
public static function get(string $uri, $callback): void |
|
84
|
|
|
{ |
|
85
|
|
|
self::route()->handle('get', $uri, $callback); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param string $uri |
|
90
|
|
|
* @param $callback |
|
91
|
|
|
*/ |
|
92
|
|
|
public static function post(string $uri, $callback): void |
|
93
|
|
|
{ |
|
94
|
|
|
self::route()->handle('post', $uri, $callback); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param string $uri |
|
99
|
|
|
* @param $callback |
|
100
|
|
|
*/ |
|
101
|
|
|
public static function put(string $uri, $callback): void |
|
102
|
|
|
{ |
|
103
|
|
|
self::route()->handle('put', $uri, $callback); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param string $uri |
|
108
|
|
|
* @param $callback |
|
109
|
|
|
*/ |
|
110
|
|
|
public static function patch(string $uri, $callback): void |
|
111
|
|
|
{ |
|
112
|
|
|
self::route()->handle('patch', $uri, $callback); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param string $uri |
|
117
|
|
|
* @param $callback |
|
118
|
|
|
*/ |
|
119
|
|
|
public static function delete(string $uri, $callback): void |
|
120
|
|
|
{ |
|
121
|
|
|
self::route()->handle('delete', $uri, $callback); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param string $uri |
|
126
|
|
|
* @param $callback |
|
127
|
|
|
*/ |
|
128
|
|
|
public static function options(string $uri, $callback): void |
|
129
|
|
|
{ |
|
130
|
|
|
self::route()->handle('options', $uri, $callback); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.