Skip to content

Commit 3ab3048

Browse files
committed
.
1 parent 62ee694 commit 3ab3048

File tree

6 files changed

+139
-108
lines changed

6 files changed

+139
-108
lines changed

app/Application.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
namespace App;
44

5-
use App\Http\Request;
65
use App\Routing\Route;
76

87
class Application
98
{
9+
1010
public function __construct()
1111
{
12-
$this->route = new Route($_SERVER["REQUEST_METHOD"], $_SERVER["REQUEST_URI"]);
12+
$this->route = new Route();
1313
}
1414

1515
public function run()

app/Http/HttpException.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,15 @@ public static function HttpForbiddenException()
4848
"description" => "You are not permitted to perform the requested operation."
4949
];
5050
}
51+
52+
public static function HttpBadRequestException()
53+
{
54+
http_response_code(400);
55+
56+
return [
57+
"code" => 400,
58+
"message" => "Bad request.",
59+
"description" => "The server cannot or will not process the request due to an apparent client error."
60+
];
61+
}
5162
}

app/Routing/Route.php

Lines changed: 35 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,9 @@
55
use App\Http\HttpException;
66
use App\Http\Request;
77
use 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
}

app/Routing/RouteCollector.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace App\Routing;
4+
5+
6+
class RouteCollector
7+
{
8+
9+
/**
10+
* An array of the routes.
11+
*
12+
* @var array
13+
*/
14+
protected static $routes = [];
15+
16+
/**
17+
* Adds a route to the collection.
18+
*
19+
* @param string $method
20+
* @param string $uri
21+
* @param mixed $callback
22+
*/
23+
private static function addRoute(string $method, string $uri, $callback)
24+
{
25+
$group = ["method" => $method, "uri" => $uri, "callback" => $callback];
26+
array_push(self::$routes, $group);
27+
}
28+
29+
/**
30+
* Adds a GET route to the collection
31+
*
32+
* @param string $uri
33+
* @param mixed $callable
34+
*/
35+
public static function get(string $uri, $callable)
36+
{
37+
self::addRoute("GET", $uri, $callable);
38+
}
39+
40+
/**
41+
* Adds a POST route to the collection
42+
*
43+
* @param string $uri
44+
* @param mixed $callable
45+
*/
46+
public static function post(string $uri, $callable)
47+
{
48+
self::addRoute("POST", $uri, $callable);
49+
}
50+
51+
/**
52+
* Adds a PUT route to the collection
53+
*
54+
* @param string $uri
55+
* @param mixed $callable
56+
*/
57+
public static function put(string $uri, $callable)
58+
{
59+
self::addRoute("PUT", $uri, $callable);
60+
}
61+
62+
/**
63+
* Adds a DELETE route to the collection
64+
*
65+
* @param string $uri
66+
* @param mixed $callable
67+
*/
68+
public static function delete(string $uri, $callable)
69+
{
70+
self::addRoute("DELETE", $uri, $callable);
71+
}
72+
}

composer.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
{
2-
"name": "admin/api",
2+
"name": "lightweight/framework",
33
"authors": [
44
{
55
"name": "Yusfuu",
66
"email": "[email protected]"
77
}
88
],
9+
"scripts": {
10+
"env": [
11+
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
12+
],
13+
"serve": [
14+
"@php -S localhost:8080"
15+
]
16+
},
917
"autoload": {
1018
"psr-4": {
1119
"App\\": "app/"
@@ -15,4 +23,4 @@
1523
"vlucas/phpdotenv": "^5.3",
1624
"firebase/php-jwt": "^5.2"
1725
}
18-
}
26+
}

public/index.php

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use App\Application;
44
use App\Routing\Route;
55
use App\Controllers\UserController;
6-
use App\Http\Middleware\JWTAuth;
76
use App\Http\Request;
87
use App\Http\Response;
98

@@ -23,30 +22,16 @@
2322
|
2423
*/
2524

26-
Route::post('/user', function (Request $request) {
27-
$bb = $request->json();
28-
$jj = new JWTAuth();
29-
// $token = $jj->create($bb);
30-
$x = $jj->verify($request->authorization);
31-
// Response::json($x);
32-
// echo 'hello ' . $request->params->name . ' have id : ' . $request->query->id;
33-
});
34-
25+
Route::get('/users', [UserController::class, 'index']);
26+
Route::get('/users/{id}', [UserController::class, 'show']);
27+
Route::post('/users', [UserController::class, 'store']);
28+
Route::put('/users/{id}', [UserController::class, 'update']);
29+
Route::delete('/users/{id}', [UserController::class, 'destroy']);
3530

36-
// Route::get('/hello/{name}', function (Request $request) {
37-
// });
38-
// Route::get('/user/{id}', function (Request $request) {
39-
// });
40-
// Route::get('/posts', function (Request $request) {
41-
// });
42-
// Route::post('/likes/{id}', function (Request $request) {
43-
// });
44-
45-
// Route::get('/users', [UserController::class, 'index']);
46-
// Route::get('/users/{id}', [UserController::class, 'show']);
47-
// Route::post('/users', [UserController::class, 'store']);
48-
// Route::put('/users/{id}', [UserController::class, 'update']);
49-
// Route::delete('/users/{id}', [UserController::class, 'destroy']);
31+
Route::get('/{username}', function (Request $request) {
32+
Response::json($request);
33+
echo "👋👋 " . $request->params->username . " i 🧡 " . $request->query->city;
34+
});
5035

5136

5237
$app->run();

0 commit comments

Comments
 (0)