From a826c6dc124507a0c833f30a60b62cb4f62bacf8 Mon Sep 17 00:00:00 2001 From: Lukasz Lupa Date: Sat, 26 Aug 2023 00:00:50 +0200 Subject: [PATCH 1/3] Create a config file --- config/router.php | 10 ++++++++++ src/LaravelRouterServiceProvider.php | 16 ++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 config/router.php create mode 100644 src/LaravelRouterServiceProvider.php diff --git a/config/router.php b/config/router.php new file mode 100644 index 0000000..f6568b0 --- /dev/null +++ b/config/router.php @@ -0,0 +1,10 @@ + [env('ACCEPTABLE_VERSIONS', Versioning::NEUTRAL->value)], +]; \ No newline at end of file diff --git a/src/LaravelRouterServiceProvider.php b/src/LaravelRouterServiceProvider.php new file mode 100644 index 0000000..b965052 --- /dev/null +++ b/src/LaravelRouterServiceProvider.php @@ -0,0 +1,16 @@ +mergeConfigFrom( + path: __DIR__ . '/../config/router.php', + key: 'router', + ); + } +} \ No newline at end of file From 22006455a8f0cee3e87f47552657271bf1cf4adb Mon Sep 17 00:00:00 2001 From: Lukasz Lupa Date: Sat, 26 Aug 2023 00:01:16 +0200 Subject: [PATCH 2/3] Add a version param to the annotations classes --- src/Annotations/Method.php | 10 ++++++++++ src/Annotations/Router.php | 8 ++++++++ 2 files changed, 18 insertions(+) diff --git a/src/Annotations/Method.php b/src/Annotations/Method.php index 32be9ca..7bfe310 100644 --- a/src/Annotations/Method.php +++ b/src/Annotations/Method.php @@ -6,6 +6,10 @@ use Twirelab\LaravelRouter\Enums\Methods; +use Twirelab\LaravelRouter\Enums\Versioning; + +use const http\Client\Curl\VERSIONS; + /** * Annotation class @Method() * @@ -22,6 +26,7 @@ public function __construct( private readonly ?string $name = null, private readonly string|array|null $middlewares = null, private readonly ?array $where = null, + private readonly ?string $version = null, ) { } @@ -49,4 +54,9 @@ public function getWhere(): ?array { return $this->where; } + + public function getVersion(): string + { + return $this->version ?? Versioning::NEUTRAL->value; + } } diff --git a/src/Annotations/Router.php b/src/Annotations/Router.php index 3068f48..301d370 100644 --- a/src/Annotations/Router.php +++ b/src/Annotations/Router.php @@ -4,6 +4,8 @@ namespace Twirelab\LaravelRouter\Annotations; +use Twirelab\LaravelRouter\Enums\Versioning; + /** * Annotation class @Loader() * @@ -19,6 +21,7 @@ public function __construct( private readonly ?string $prefix = null, private readonly ?string $domain = null, private readonly string|array|null $middlewares = [], + private readonly ?string $version = null ) { } @@ -41,4 +44,9 @@ public function getMiddlewares(): array|string|null { return $this->middlewares; } + + public function getVersion(): ?string + { + return $this->version ?? Versioning::NEUTRAL->value; + } } From c53cf91dd81ed40e6957b0206fcaf36666c98a33 Mon Sep 17 00:00:00 2001 From: Lukasz Lupa Date: Sat, 26 Aug 2023 00:45:19 +0200 Subject: [PATCH 3/3] Create a function to add version into uri --- composer.json | 7 +++++ src/Enums/Versioning.php | 8 +++++ src/Traits/Loader.php | 66 ++++++++++++++++++++++++++++------------ 3 files changed, 61 insertions(+), 20 deletions(-) create mode 100644 src/Enums/Versioning.php diff --git a/composer.json b/composer.json index 6a716d3..c2b3f0b 100644 --- a/composer.json +++ b/composer.json @@ -17,6 +17,13 @@ "require-dev": { "laravel/pint": "^1.10" }, + "extra": { + "laravel": { + "providers": [ + "Twirelab\\LaravelRouter\\LaravelRouterServiceProvider" + ] + } + }, "autoload": { "psr-4": { "Twirelab\\LaravelRouter\\": "src" diff --git a/src/Enums/Versioning.php b/src/Enums/Versioning.php new file mode 100644 index 0000000..82d841a --- /dev/null +++ b/src/Enums/Versioning.php @@ -0,0 +1,8 @@ +getController($class); - Route::group( - $controller, - fn (LaravelRouter $router) => $this->loadMethods( - router: $router, - methods: $class->getMethods(), - data: $controller, - class: $class - ) + if(!in_array( + needle: $controller['version'], + haystack: Arr::wrap(Config::get('router.acceptable_versions')) + )) { + return; + } + + $this->loadMethods( + methods: $class->getMethods(), + controller: $controller, + class: $class ); } /** * Set a controller data. */ - private function setControllerData(string $name = null, string $prefix = null, string $domain = null, string|array $middlewares = null): array + private function setControllerData( + string $name = null, + string $prefix = null, + string $domain = null, + string|array $middlewares = null, + string $version = null + ): array { - return compact('name', 'prefix', 'domain', 'middlewares'); + return compact('name', 'prefix', 'domain', 'middlewares', 'version'); } /** @@ -72,7 +84,8 @@ private function getController(ReflectionClass $class): array name: $annotation->getName(), prefix: $annotation->getPrefix(), domain: $annotation->getDomain(), - middlewares: $annotation->getMiddlewares() + middlewares: $annotation->getMiddlewares(), + version: $annotation->getVersion() ); } @@ -82,14 +95,20 @@ private function getController(ReflectionClass $class): array /** * Load methods from a controller. */ - private function loadMethods(LaravelRouter $router, array $methods, array $data, ReflectionClass $class): void + private function loadMethods(array $methods, array $controller, ReflectionClass $class): void { foreach ($methods as $method) { foreach ($this->loadAnnotationMethods($method) as $annotation) { + if(in_array( + needle: $annotation->getVersion(), + haystack: Arr::wrap(Config::get('router.acceptable_versions')) + )) { + continue; + } + $this->addRoute( - router: $router, annotation: $annotation, - data: $data, + controller: $controller, class: $class, method: $method ); @@ -110,17 +129,24 @@ private function loadAnnotationMethods(ReflectionClass|ReflectionMethod $reflect /** * Add route. */ - private function addRoute(LaravelRouter $router, Method $annotation, array $data, ReflectionClass $class, ReflectionMethod $method): void + private function addRoute(Method $annotation, array $controller, ReflectionClass $class, ReflectionMethod $method): void { - $name = $data['name'].($annotation->getName() ?? Str::snake($method->getName())); + $methodVersion = $annotation->getVersion() === Versioning::NEUTRAL->value ? null : $annotation->getVersion(); + $controllerVersion = $controller['version'] === Versioning::NEUTRAL->value ? null : $annotation['version']; + $version = $methodVersion ?: $controllerVersion ?: null; + $uri = $version . $annotation->getUri(); + + $name = ($version ? Str::snake($version) . '.' : null) .$controller['name'].($annotation->getName() ?? Str::snake($method->getName())); - $router - ->{$annotation->getMethod()}($annotation->getUri(), [$class->getName(), $method->getName()]) + $route = Route::{$annotation->getMethod()}($uri, [$class->getName(), $method->getName()]) ->name($name) - ->middleware($annotation->getMiddlewares()); + ->middleware([ + ...$controller['middlewares'], + ...$annotation->getMiddlewares(), + ]); if ($annotation->getWhere()) { - $router->where($annotation->getWhere()); + $route->where($annotation->getWhere()); } } }