Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add versioning to annotations #4

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
"require-dev": {
"laravel/pint": "^1.10"
},
"extra": {
"laravel": {
"providers": [
"Twirelab\\LaravelRouter\\LaravelRouterServiceProvider"
]
}
},
"autoload": {
"psr-4": {
"Twirelab\\LaravelRouter\\": "src"
Expand Down
10 changes: 10 additions & 0 deletions config/router.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

use Twirelab\LaravelRouter\Enums\Versioning;

return [
/**
* What versions of endpoints are acceptable.
*/
'acceptable_versions' => [env('ACCEPTABLE_VERSIONS', Versioning::NEUTRAL->value)],
];
10 changes: 10 additions & 0 deletions src/Annotations/Method.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

use Twirelab\LaravelRouter\Enums\Methods;

use Twirelab\LaravelRouter\Enums\Versioning;

use const http\Client\Curl\VERSIONS;

/**
* Annotation class @Method()
*
Expand All @@ -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,
) {
}

Expand Down Expand Up @@ -49,4 +54,9 @@ public function getWhere(): ?array
{
return $this->where;
}

public function getVersion(): string
{
return $this->version ?? Versioning::NEUTRAL->value;
}
}
8 changes: 8 additions & 0 deletions src/Annotations/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Twirelab\LaravelRouter\Annotations;

use Twirelab\LaravelRouter\Enums\Versioning;

/**
* Annotation class @Loader()
*
Expand All @@ -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
) {
}

Expand All @@ -41,4 +44,9 @@ public function getMiddlewares(): array|string|null
{
return $this->middlewares;
}

public function getVersion(): ?string
{
return $this->version ?? Versioning::NEUTRAL->value;
}
}
8 changes: 8 additions & 0 deletions src/Enums/Versioning.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Twirelab\LaravelRouter\Enums;

enum Versioning: string
{
case NEUTRAL = 'NEUTRAL';
}
16 changes: 16 additions & 0 deletions src/LaravelRouterServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Twirelab\LaravelRouter;

use Illuminate\Support\ServiceProvider;

class LaravelRouterServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->mergeConfigFrom(
path: __DIR__ . '/../config/router.php',
key: 'router',
);
}
}
66 changes: 46 additions & 20 deletions src/Traits/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Twirelab\LaravelRouter\Traits;

use Illuminate\Routing\Router as LaravelRouter;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Str;
use InvalidArgumentException;
Expand All @@ -13,6 +15,7 @@
use ReflectionMethod;
use Twirelab\LaravelRouter\Annotations\Method;
use Twirelab\LaravelRouter\Annotations\Router;
use Twirelab\LaravelRouter\Enums\Versioning;

trait Loader
{
Expand All @@ -36,23 +39,32 @@ public function loadController(string $source): void

$controller = $this->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');
}

/**
Expand All @@ -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()
);
}

Expand All @@ -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
);
Expand All @@ -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());
}
}
}