Build up initial test, add Request, hardcode SegmentRouter

This commit is contained in:
Sky Johnson 2024-09-19 18:37:23 -05:00
parent df03105542
commit d01ec64879
4 changed files with 130 additions and 4 deletions

8
composer.lock generated
View File

@ -8,11 +8,11 @@
"packages": [
{
"name": "sharkk/router",
"version": "1.1.2",
"version": "1.1.4",
"dist": {
"type": "zip",
"url": "https://git.sharkk.net/api/packages/PHP/composer/files/sharkk%2Frouter/1.1.2/sharkk-router.1.1.2.zip",
"shasum": "2e0cc4d87050fceae3fd60b0a4687aac62257bad"
"url": "https://git.sharkk.net/api/packages/PHP/composer/files/sharkk%2Frouter/1.1.4/sharkk-router.1.1.4.zip",
"shasum": "d7f339e9184f2415ef439c4406e706354b1cf5d5"
},
"type": "library",
"autoload": {
@ -30,7 +30,7 @@
}
],
"description": "A simple node-based router.",
"time": "2024-09-13T17:45:53-05:00"
"time": "2024-09-19T12:46:57-05:00"
}
],
"packages-dev": [],

47
src/App.php Normal file
View File

@ -0,0 +1,47 @@
<?php
namespace Sharkk\Web;
use Sharkk\Router\Router;
use Sharkk\Router\SegmentRouter;
class App
{
private Router $router;
private Request $request;
public function __construct()
{
$this->request = new Request;
$this->router = new SegmentRouter;
}
public function get(string $route, callable $handler): void
{
$this->router->add('GET', $route, $handler);
}
public function post(string $route, callable $handler): void
{
$this->router->add('POST', $route, $handler);
}
public function run(string $method, string $uri): void
{
$l = $this->router->lookup($method, $uri);
if ($l['code'] === 404) {
http_response_code(404);
echo "Not Found\n";
return;
}
if ($l['code'] === 405) {
http_response_code(405);
echo "Method Not Allowed\n";
return;
}
$l['handler'](...$l['params'] ?? []);
}
}

48
src/Request.php Normal file
View File

@ -0,0 +1,48 @@
<?php
namespace Sharkk\Web;
class Request
{
public array $uri;
public string $method;
public array $headers;
public int $status;
public bool $htmx;
public function __construct()
{
// Get the URI and trim slashes, then break it into pieces
$uri = parse_url($_SERVER['REQUEST_URI'])['path'];
$uri = '/'.trim(trim($uri), '/');
$this->uri = explode('/', $uri);
array_shift($this->uri);
// Get the request method
$this->method = $_SERVER['REQUEST_METHOD'];
// Get all headers
$this->headers = $this->getAllHeaders();
// Determine if this is an htmx request
$this->htmx = isset($this->headers['Hx-Request']);
}
public function uri(int $index): string|false
{
if ($index == 0 && empty($this->uri[0])) return '/';
return $this->uri[$index] ?? false;
}
private function getAllHeaders(): array
{
$headers = [];
foreach($_SERVER as $k => $v) {
if (substr($k, 0, 5) <> 'HTTP_') continue;
$header = str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($k, 5)))));
$headers[$header] = $v;
}
return $headers;
}
}

31
tests/test.php Normal file
View File

@ -0,0 +1,31 @@
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use Sharkk\Web\App;
use Sharkk\Router\SegmentRouter;
// Make sure an HTTP method and URI are provided
if ($argc < 3) {
echo 'Usage: php test.php <method> <uri>';
exit(1);
}
// Ensure the method is a valid HTTP method
$method = strtoupper($argv[1]);
if (!in_array($method, ['GET', 'POST'])) {
echo 'Invalid HTTP method';
exit(1);
}
$app = new App(new SegmentRouter);
$app->get('/', function () {
echo "Hello, World!\n";
});
$app->get('/about/:daniel', function ($d) {
echo "What about $d?\n";
});
$app->run($method, $argv[2]);