2024-07-15 14:27:57 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require "vendor/autoload.php";
|
|
|
|
|
|
|
|
use Dumbo\Dumbo;
|
2024-08-28 10:08:30 +01:00
|
|
|
use Dumbo\HTTPException;
|
2024-08-28 10:24:23 +01:00
|
|
|
// use Dumbo\Helpers\BearerAuth;
|
2024-07-15 14:27:57 +01:00
|
|
|
|
2024-07-16 22:50:59 +01:00
|
|
|
$app = new Dumbo();
|
|
|
|
$user = new Dumbo();
|
2024-07-15 16:18:47 +01:00
|
|
|
|
2024-08-26 19:54:49 +01:00
|
|
|
$protectedRoutes = new Dumbo();
|
|
|
|
$token = "mysupersecret";
|
|
|
|
|
2024-08-28 10:24:23 +01:00
|
|
|
// $app->use(BearerAuth::bearerAuth($token));
|
2024-08-26 19:54:49 +01:00
|
|
|
|
2024-07-15 16:18:47 +01:00
|
|
|
$userData = [
|
2024-07-15 19:11:45 +01:00
|
|
|
[
|
|
|
|
"id" => 1,
|
|
|
|
"name" => "Jamie Barton",
|
|
|
|
"email" => "jamie@notrab.dev",
|
|
|
|
],
|
2024-07-15 16:18:47 +01:00
|
|
|
];
|
|
|
|
|
2024-07-15 16:24:38 +01:00
|
|
|
$user->get("/", function ($c) use ($userData) {
|
|
|
|
return $c->json($userData);
|
2024-07-15 16:18:47 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
$user->get("/:id", function ($c) use ($userData) {
|
2024-07-15 18:59:04 +01:00
|
|
|
$id = (int) $c->req->param("id");
|
2024-07-15 16:18:47 +01:00
|
|
|
|
2024-07-15 19:11:45 +01:00
|
|
|
$user =
|
|
|
|
array_values(array_filter($userData, fn($u) => $u["id"] === $id))[0] ??
|
|
|
|
null;
|
|
|
|
|
|
|
|
if (!$user) {
|
2024-07-15 16:18:47 +01:00
|
|
|
return $c->json(["error" => "User not found"], 404);
|
|
|
|
}
|
|
|
|
|
2024-07-15 19:11:45 +01:00
|
|
|
return $c->json($user);
|
2024-07-15 16:18:47 +01:00
|
|
|
});
|
2024-07-15 14:27:57 +01:00
|
|
|
|
2024-07-15 19:11:45 +01:00
|
|
|
$user->post("/", function ($c) use ($userData) {
|
|
|
|
$body = $c->req->body();
|
|
|
|
|
|
|
|
if (!isset($body["name"]) || !isset($body["email"])) {
|
|
|
|
return $c->json(["error" => "Name and email are required"], 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
$newId = max(array_column($userData, "id")) + 1;
|
|
|
|
|
|
|
|
$newUserData = array_merge(["id" => $newId], $body);
|
|
|
|
|
|
|
|
return $c->json($newUserData, 201);
|
|
|
|
});
|
|
|
|
|
2024-07-15 21:09:07 +01:00
|
|
|
$user->delete("/:id", function ($c) use ($userData) {
|
2024-07-15 19:11:45 +01:00
|
|
|
$id = (int) $c->req->param("id");
|
|
|
|
|
2024-07-15 21:09:07 +01:00
|
|
|
$user =
|
|
|
|
array_values(array_filter($userData, fn($u) => $u["id"] === $id))[0] ??
|
|
|
|
null;
|
|
|
|
|
|
|
|
if (!$user) {
|
2024-07-15 19:11:45 +01:00
|
|
|
return $c->json(["error" => "User not found"], 404);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $c->json(["message" => "User deleted successfully"]);
|
2024-07-15 14:27:57 +01:00
|
|
|
});
|
|
|
|
|
2024-07-15 18:59:04 +01:00
|
|
|
$app->get("/greet/:greeting", function ($c) {
|
|
|
|
$greeting = $c->req->param("greeting");
|
|
|
|
$name = $c->req->query("name");
|
2024-07-15 16:18:47 +01:00
|
|
|
|
2024-07-15 14:27:57 +01:00
|
|
|
return $c->json([
|
|
|
|
"message" => "$greeting, $name!",
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2024-07-15 16:18:47 +01:00
|
|
|
$app->route("/users", $user);
|
|
|
|
|
2024-07-16 12:44:34 +01:00
|
|
|
$app->use(function ($ctx, $next) {
|
|
|
|
$ctx->set("message", "Dumbo");
|
|
|
|
return $next($ctx);
|
|
|
|
});
|
|
|
|
|
2024-07-15 16:42:42 +01:00
|
|
|
$app->use(function ($c, $next) {
|
2024-07-15 18:59:04 +01:00
|
|
|
$c->header("X-Powered-By", "Dumbo");
|
2024-07-15 16:42:42 +01:00
|
|
|
|
|
|
|
return $next($c);
|
|
|
|
});
|
|
|
|
|
2024-07-16 13:03:59 +01:00
|
|
|
$app->get("/redirect", function ($c) {
|
|
|
|
$message = $c->get("message");
|
|
|
|
|
2024-07-16 22:50:59 +01:00
|
|
|
return $c->redirect("/greet/hello?name=$message", 301);
|
2024-07-16 13:03:59 +01:00
|
|
|
});
|
|
|
|
|
2024-07-15 19:11:45 +01:00
|
|
|
$app->get("/", function ($c) {
|
2024-07-16 12:44:34 +01:00
|
|
|
$message = $c->get("message");
|
|
|
|
|
|
|
|
return $c->html("<h1>Hello from $message!</h1>", 200, [
|
2024-07-15 19:11:45 +01:00
|
|
|
"X-Hello" => "World",
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2024-08-28 10:08:30 +01:00
|
|
|
$app->get("/error", function ($c) {
|
|
|
|
$customResponse = $c->html("<h1>Something went wrong</h1>", 404);
|
|
|
|
throw new HTTPException(404, "Something went wrong", $customResponse);
|
|
|
|
});
|
|
|
|
|
2024-07-15 14:27:57 +01:00
|
|
|
$app->run();
|