55use App \Http \HttpException ;
66use App \Http \Request ;
77use App \Http \Response ;
8- use Exception ;
98
10- class Route
9+ class Route extends RouteCollector
1110{
12- /**
13- * An array of the routes.
14- *
15- * @var array
16- */
17- protected static $ routes = [];
1811
1912 /**
2013 * The parameter names for the route.
@@ -42,78 +35,18 @@ class Route
4235 *
4336 * @var string[]
4437 */
45- protected $ verbs = ['GET ' , 'POST ' , 'PUT ' , 'DELETE ' ];
46-
47- public function __construct ($ method , $ path )
48- {
49- $ this ->method = $ method ;
50- $ this ->path = $ path ;
51- }
52-
53- /**
54- * Adds a route to the collection.
55- *
56- * @param string $method
57- * @param string $uri
58- * @param mixed $callback
59- */
60- private static function addRoute (string $ method , string $ uri , $ callback )
61- {
62- $ group = ["method " => $ method , "uri " => $ uri , "callback " => $ callback ];
63- array_push (self ::$ routes , $ group );
64- }
65-
66- /**
67- * Adds a GET route to the collection
68- *
69- * @param string $uri
70- * @param mixed $callable
71- */
72- public static function get (string $ uri , $ callable )
73- {
74- self ::addRoute ("GET " , $ uri , $ callable );
75- }
38+ protected $ verbs = ["GET " , "POST " , "PUT " , "DELETE " ];
7639
77- /**
78- * Adds a POST route to the collection
79- *
80- * @param string $uri
81- * @param mixed $callable
82- */
83- public static function post (string $ uri , $ callable )
40+ public function __construct ()
8441 {
85- self ::addRoute ("POST " , $ uri , $ callable );
86- }
87-
88- /**
89- * Adds a PUT route to the collection
90- *
91- * @param string $uri
92- * @param mixed $callable
93- */
94- public static function put (string $ uri , $ callable )
95- {
96- self ::addRoute ("PUT " , $ uri , $ callable );
97- }
98-
99- /**
100- * Adds a DELETE route to the collection
101- *
102- * @param string $uri
103- * @param mixed $callable
104- */
105- public static function delete (string $ uri , $ callable )
106- {
107- self ::addRoute ("DELETE " , $ uri , $ callable );
42+ $ this ->req = $ this ->request ();
43+ $ this ->method = $ this ->req ->method ;
44+ $ this ->path = $ this ->req ->path ;
45+ $ this ->query = $ this ->req ->query ;
10846 }
10947
11048 public function call ()
11149 {
112-
113- if (!in_array (strtoupper ($ this ->method ), $ this ->verbs )) {
114- return Response::json (HttpException::HttpMethodNotAllowedException ());
115- }
116-
11750 $ routes = $ this ->filter_routes_by_method (self ::$ routes , $ this ->method );
11851
11952 foreach ($ routes as $ value ) {
@@ -127,7 +60,10 @@ public function call()
12760 return Response::json (HttpException::HttpNotFoundException ());
12861 }
12962
130- $ args = (object )["params " => $ this ->params , "query " => (object ) $ this ->query ];
63+ $ args = (object )[
64+ "params " => $ this ->params ,
65+ "query " => (object ) $ this ->query
66+ ];
13167
13268 return call_user_func ($ this ->currentRoute ->callback , (new Request ($ args )));
13369 }
@@ -157,14 +93,10 @@ protected function filter_routes_by_method(array $routes, string $method)
15793 */
15894 private function matches (string $ uri , string $ path )
15995 {
160- $ url = parse_url ($ path );
161- $ path = $ url ["path " ];
162-
163- $ regex = '~^ ' . preg_replace ('/\{(.*?)\}/ ' , '(?<$1>[a-zA-Z0-9_\-\@\.]+) ' , $ uri ) . '$~ ' ;
96+ $ regex = "~^ " . preg_replace ("/\{(.*?)\}/ " , "(?<$1>[a-zA-Z0-9_\-\@\.]+) " , $ uri ) . "$~ " ;
16497
16598 if (preg_match ($ regex , $ path , $ parameter )) {
16699
167- parse_str ($ url ["query " ] ?? '' , $ this ->query );
168100 $ this ->params = (object ) array_filter ($ parameter , function ($ k ) {
169101 return is_string ($ k );
170102 }, ARRAY_FILTER_USE_KEY );
@@ -173,4 +105,27 @@ private function matches(string $uri, string $path)
173105 }
174106 return false ;
175107 }
108+
109+ private function request ()
110+ {
111+ $ httpMethod = $ _SERVER ["REQUEST_METHOD " ] ?? null ;
112+
113+ if (!in_array (strtoupper ($ httpMethod ), $ this ->verbs )) {
114+ return Response::json (HttpException::HttpMethodNotAllowedException ());
115+ }
116+
117+ $ URL = (isset ($ _SERVER ["HTTPS " ]) ? "https " : "http " ) . ":// $ _SERVER [HTTP_HOST ]$ _SERVER [REQUEST_URI ]" ;
118+ $ URL = filter_var ($ URL , FILTER_SANITIZE_URL );
119+ if (!filter_var ($ URL , FILTER_VALIDATE_URL )) {
120+ return Response::json (HttpException::HttpBadRequestException ());
121+ }
122+
123+ parse_str ($ _SERVER ["QUERY_STRING " ], $ qs );
124+
125+ return ((object ) array_merge (parse_url ($ URL ), [
126+ "method " => $ httpMethod ,
127+ "query " => (object )$ qs ,
128+ "contentType " => ($ _SERVER ["CONTENT_TYPE " ]) ?? null
129+ ]));
130+ }
176131}
0 commit comments