From ae6fa8b4bff48cc7c0da04cbd0c7a5b7b5f9a6f4 Mon Sep 17 00:00:00 2001 From: "Mathias V. Nielsen" <1547127+math280h@users.noreply.github.com> Date: Fri, 17 Feb 2023 00:46:33 +0100 Subject: [PATCH] Implemented route grouping, cleaned up public asset handling --- README.md | 20 ++++ src/Route.php | 111 +++++++++++++++++- src/Router.php | 46 +++----- tests/RouterTest.php | 2 +- .../{server/public => public/assets}/test.css | 0 tests/public/index.php | 33 ++++++ tests/server/index.php | 18 --- 7 files changed, 181 insertions(+), 49 deletions(-) rename tests/{server/public => public/assets}/test.css (100%) create mode 100644 tests/public/index.php delete mode 100644 tests/server/index.php diff --git a/README.md b/README.md index 13bb4b0..a247291 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,26 @@ $router->get('/', MyController::class . '::index') $router->post('/', MyController::class . '::index') ``` +### Route groups + +You can group routes together, this allows you to e.g. define a prefix for a colleciton of routes or apply +middleware to the collection of routes. + +You can define a route group like so: +```php +use Math280h\PhpRouter\Route; + +$router->group(["prefix" => "test"], [ + Route::get('/1', function () { + echo 'Hello World'; + }), + Route::get('/2', function () { + echo 'Hello World'; + }), +]); +``` +This example results in two routes with the paths `/test/1` and `/test/2` + ### Adding middleware The router supports middleware than runs after the connection is accepted but before the request diff --git a/src/Route.php b/src/Route.php index e95a5a1..1b6d19e 100644 --- a/src/Route.php +++ b/src/Route.php @@ -84,4 +84,113 @@ private function makeCallable(): void } } } -} \ No newline at end of file + + /** + * Route construction + */ + + /** + * @param string $path + * @param callable|string $callback + * @param array $middleware + * @return Route + */ + public static function get( + string $path, + callable|string $callback, + array $middleware = [] + ): Route + { + return new Route($path, "GET", $callback, $middleware); + } + + /** + * @param string $path + * @param callable|string $callback + * @param array $middleware + * @return Route + */ + public static function post( + string $path, + callable|string $callback, + array $middleware = [] + ): Route + { + return new Route($path, "POST", $callback, $middleware); + } + + /** + * @param string $path + * @param callable|string $callback + * @param array $middleware + * @return Route + */ + public static function put( + string $path, + callable|string $callback, + array $middleware = [] + ): Route + { + return new Route($path, "PUT", $callback, $middleware); + } + + /** + * @param string $path + * @param callable|string $callback + * @param array $middleware + * @return Route + */ + public static function delete( + string $path, + callable|string $callback, + array $middleware = [] + ): Route + { + return new Route($path, "DELETE", $callback, $middleware); + } + + /** + * @param string $path + * @param callable|string $callback + * @param array $middleware + * @return Route + */ + public static function options( + string $path, + callable|string $callback, + array $middleware = [] + ): Route + { + return new Route($path, "OPTIONS", $callback, $middleware); + } + + /** + * @param string $path + * @param callable|string $callback + * @param array $middleware + * @return Route + */ + public static function head( + string $path, + callable|string $callback, + array $middleware = [] + ): Route + { + return new Route($path, "HEAD", $callback, $middleware); + } + + /** + * @param string $path + * @param callable|string $callback + * @param array $middleware + * @return Route + */ + public static function patch( + string $path, + callable|string $callback, + array $middleware = [] + ): Route + { + return new Route($path, "PATCH", $callback, $middleware); + } +} diff --git a/src/Router.php b/src/Router.php index b36a41a..e8a10a8 100644 --- a/src/Router.php +++ b/src/Router.php @@ -23,9 +23,24 @@ class Router */ private array $middlewares; - public function __construct(string $public_directory = "") + /** + * Create route group + * + * @param array $attributes + * @param array $routes + * @return void + */ + public function group(array $attributes, array $routes): void { - $this->loadPublic($public_directory); + foreach ($routes as $route) { + if (array_key_exists("prefix", $attributes)) { + $route->path = (!str_starts_with($attributes["prefix"], "/") ? "/" . $attributes["prefix"]:$attributes["prefix"]) . $route->path; + } + if (array_key_exists("middleware", $attributes)) { + $route->middleware[] = $attributes["middleware"]; + } + $this->addRoute($route); + } } /** @@ -142,33 +157,6 @@ public function addMiddleware(string $name, Closure $middleware): void $this->middlewares[$name] = $middleware; } - /** - * Load resource in public folder - * - * @param string $public_directory - * @return void - */ - private function loadPublic(string $public_directory = ""): void - { - if ($public_directory === "") { - $public_directory = dirname(__DIR__, 2) . '/public'; - } - - $public_resources = new RecursiveTreeIterator( - new RecursiveDirectoryIterator( - $public_directory, - FilesystemIterator::SKIP_DOTS)); - - foreach ($public_resources as $public_resource) { - $path = explode('public/', $public_resource)[1]; - if (!str_ends_with($path, '.php')) { - $this->get($path, function() use ($public_resource) { - echo file_get_contents($public_resource); - }); - } - } - } - /** * Run Router * diff --git a/tests/RouterTest.php b/tests/RouterTest.php index db5e3f5..ef9a8a4 100644 --- a/tests/RouterTest.php +++ b/tests/RouterTest.php @@ -11,7 +11,7 @@ final class RouterTest extends TestCase public function setUp(): void { - $path = getcwd() . "/server"; + $path = getcwd() . "/public"; $this->process = new Process(["php", "-S", "localhost:8080", "-t", $path]); $this->process->start(); diff --git a/tests/server/public/test.css b/tests/public/assets/test.css similarity index 100% rename from tests/server/public/test.css rename to tests/public/assets/test.css diff --git a/tests/public/index.php b/tests/public/index.php new file mode 100644 index 0000000..95f3ea9 --- /dev/null +++ b/tests/public/index.php @@ -0,0 +1,33 @@ +get('/', function () { + echo 'Hello World'; +}); + +$router->get('/yeet', function () { + echo 'Hello World'; +}); + + +$router->group(["prefix" => "test"], [ + Route::get('/1', function () { + echo 'Hello World'; + }), + Route::get('/2', function () { + echo 'Hello World'; + }), +]); + +$router->addMiddleware("log", function ($request) { + print_r($request); +}); + +$router->run(); diff --git a/tests/server/index.php b/tests/server/index.php deleted file mode 100644 index 68612bf..0000000 --- a/tests/server/index.php +++ /dev/null @@ -1,18 +0,0 @@ -get('/', function () { - echo 'Hello World'; -}); - -$router->addMiddleware("log", function ($request) { - print_r($request); -}); - -$router->run();