From d01ec648796ef30b110bcfbcc7fe8fe1bed65b93 Mon Sep 17 00:00:00 2001 From: Sky Johnson Date: Thu, 19 Sep 2024 18:37:23 -0500 Subject: [PATCH] Build up initial test, add Request, hardcode SegmentRouter --- composer.lock | 8 ++++---- src/App.php | 47 +++++++++++++++++++++++++++++++++++++++++++++++ src/Request.php | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ tests/test.php | 31 +++++++++++++++++++++++++++++++ 4 files changed, 130 insertions(+), 4 deletions(-) create mode 100644 src/App.php create mode 100644 src/Request.php create mode 100644 tests/test.php diff --git a/composer.lock b/composer.lock index f55cf2d..e49d4e6 100644 --- a/composer.lock +++ b/composer.lock @@ -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": [], diff --git a/src/App.php b/src/App.php new file mode 100644 index 0000000..f06c3fb --- /dev/null +++ b/src/App.php @@ -0,0 +1,47 @@ +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'] ?? []); + } +} diff --git a/src/Request.php b/src/Request.php new file mode 100644 index 0000000..a68aafd --- /dev/null +++ b/src/Request.php @@ -0,0 +1,48 @@ +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; + } +} diff --git a/tests/test.php b/tests/test.php new file mode 100644 index 0000000..4177069 --- /dev/null +++ b/tests/test.php @@ -0,0 +1,31 @@ + '; + 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]);