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 WithoutMiddleware option to route #151

Merged
merged 1 commit into from
Dec 9, 2024
Merged
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
2 changes: 2 additions & 0 deletions src/Attributes/Any.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ public function __construct(
string $uri,
?string $name = null,
array | string $middleware = [],
array | string $withoutMiddleware = [],
) {
parent::__construct(
methods: Router::$verbs,
uri: $uri,
name: $name,
middleware: $middleware,
withoutMiddleware: $withoutMiddleware
);
}
}
2 changes: 2 additions & 0 deletions src/Attributes/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ public function __construct(
string $uri,
?string $name = null,
array | string $middleware = [],
array | string $withoutMiddleware = [],
) {
parent::__construct(
methods: ['delete'],
uri: $uri,
name: $name,
middleware: $middleware,
withoutMiddleware: $withoutMiddleware
);
}
}
2 changes: 2 additions & 0 deletions src/Attributes/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ public function __construct(
string $uri,
?string $name = null,
array | string $middleware = [],
array | string $withoutMiddleware = [],
) {
parent::__construct(
methods: ['get'],
uri: $uri,
name: $name,
middleware: $middleware,
withoutMiddleware: $withoutMiddleware
);
}
}
2 changes: 2 additions & 0 deletions src/Attributes/Patch.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ public function __construct(
string $uri,
?string $name = null,
array | string $middleware = [],
array | string $withoutMiddleware = [],
) {
parent::__construct(
methods: ['patch'],
uri: $uri,
name: $name,
middleware: $middleware,
withoutMiddleware: $withoutMiddleware
);
}
}
2 changes: 2 additions & 0 deletions src/Attributes/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ public function __construct(
string $uri,
?string $name = null,
array | string $middleware = [],
array | string $withoutMiddleware = [],
) {
parent::__construct(
methods: ['post'],
uri: $uri,
name: $name,
middleware: $middleware,
withoutMiddleware: $withoutMiddleware
);
}
}
2 changes: 2 additions & 0 deletions src/Attributes/Put.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ public function __construct(
string $uri,
?string $name = null,
array | string $middleware = [],
array | string $withoutMiddleware = [],
) {
parent::__construct(
methods: ['put'],
uri: $uri,
name: $name,
middleware: $middleware,
withoutMiddleware: $withoutMiddleware
);
}
}
4 changes: 4 additions & 0 deletions src/Attributes/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ class Route implements RouteAttribute

public array $middleware;

public array $withoutMiddleware;

public function __construct(
array | string $methods,
public string $uri,
public ?string $name = null,
array | string $middleware = [],
array | string $withoutMiddleware = [],
) {
$this->methods = array_map(
static fn (string $verb) => in_array(
Expand All @@ -29,5 +32,6 @@ public function __construct(
Arr::wrap($methods)
);
$this->middleware = Arr::wrap($middleware);
$this->withoutMiddleware = Arr::wrap($withoutMiddleware);
}
}
23 changes: 19 additions & 4 deletions src/RouteRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,19 @@ protected function processAttributes(string $className): void
$class = new ReflectionClass($className);

$classRouteAttributes = new ClassRouteAttributes($class);

$groups = $classRouteAttributes->groups();

foreach ($groups as $group) {
$router = $this->router;
$router->group($group, fn () => $this->registerRoutes($class, $classRouteAttributes));
}

if ($classRouteAttributes->resource()) {
$this->registerResource($class, $classRouteAttributes);
}


}

protected function registerResource(ReflectionClass $class, ClassRouteAttributes $classRouteAttributes): void
Expand Down Expand Up @@ -174,6 +174,9 @@ protected function registerRoutes(ReflectionClass $class, ClassRouteAttributes $

$this->addMiddlewareToRoute($classRouteAttributes, $attributeClass, $route);

$this->addWithoutMiddlewareToRoute($classRouteAttributes, $attributeClass, $route);


$this->setWithTrashedIfAvailable($classRouteAttributes, $withTrashedAttribute, $route);


Expand Down Expand Up @@ -264,6 +267,18 @@ public function addMiddlewareToRoute(ClassRouteAttributes $classRouteAttributes,
$route->middleware([...$this->middleware, ...$classMiddleware, ...$methodMiddleware]);
}

/**
* @param ClassRouteAttributes $classRouteAttributes
* @param Route $attributeClass
* @param \Illuminate\Routing\Route $route
* @return void
*/
private function addWithoutMiddlewareToRoute(ClassRouteAttributes $classRouteAttributes, Route $attributeClass, \Illuminate\Routing\Route $route): void
{
$methodWithoutMiddleware = $attributeClass->withoutMiddleware;
$route->withoutMiddleware($methodWithoutMiddleware);
}

/**
* @param ClassRouteAttributes $classRouteAttributes
* @param mixed $defaultAttributes
Expand Down
8 changes: 7 additions & 1 deletion tests/AttributeTests/FallbackAttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ public function it_can_register_a_route_as_fallback()

$this
->assertRegisteredRoutesCount(1)
->assertRouteRegistered(FallbackTestController::class, 'myFallbackMethod', 'get', 'my-fallback-method', [], null, null, [], true);
->assertRouteRegistered(
controller: FallbackTestController::class,
controllerMethod: 'myFallbackMethod',
httpMethods: 'get',
uri: 'my-fallback-method',
isFallback: true
);
}
}
26 changes: 26 additions & 0 deletions tests/AttributeTests/WithoutMiddlewareAttributeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Spatie\RouteAttributes\Tests\AttributeTests;

use Spatie\RouteAttributes\Tests\TestCase;
use Spatie\RouteAttributes\Tests\TestClasses\Controllers\WithoutMiddlewareTestController;
use Spatie\RouteAttributes\Tests\TestClasses\Middleware\SkippedMiddleware;

class WithoutMiddlewareAttributeTest extends TestCase
{
/** @test */
public function it_can_skip_middleware_added_to_class()
{
$this->routeRegistrar->registerClass(WithoutMiddlewareTestController::class);


$this
->assertRegisteredRoutesCount(1)
->assertRouteRegistered(
WithoutMiddlewareTestController::class,
controllerMethod: 'withoutMiddleware',
uri: 'without-middleware',
withoutMiddleware: [SkippedMiddleware::class],
);
}
}
44 changes: 27 additions & 17 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,34 @@ public function assertRegisteredRoutesCount(int $expectedNumber): self
}

public function assertRouteRegistered(
string $controller,
string $controllerMethod = 'myMethod',
string | array $httpMethods = ['get'],
string $uri = 'my-method',
string | array $middleware = [],
?string $name = null,
?string $domain = null,
?array $wheres = [],
?bool $isFallback = false,
?bool $enforcesScopedBindings = false,
?bool $preventsScopedBindings = false,
?array $defaults = [],
?bool $withTrashed = false,
): self {
if (! is_array($middleware)) {
string $controller,
string $controllerMethod = 'myMethod',
string|array $httpMethods = ['get'],
string $uri = 'my-method',
string|array $middleware = [],
string|array $withoutMiddleware = [],
?string $name = null,
?string $domain = null,
?array $wheres = [],
?bool $isFallback = false,
?bool $enforcesScopedBindings = false,
?bool $preventsScopedBindings = false,
?array $defaults = [],
?bool $withTrashed = false,
): self
{
if (!is_array($middleware)) {
$middleware = Arr::wrap($middleware);
}

if (!is_array($withoutMiddleware)) {
$withoutMiddleware = Arr::wrap($withoutMiddleware);
}

$routeRegistered = collect($this->getRouteCollection()->getRoutes())
->contains(function (Route $route) use ($name, $middleware, $controllerMethod, $controller, $uri, $httpMethods, $domain, $wheres, $isFallback, $enforcesScopedBindings, $preventsScopedBindings, $defaults, $withTrashed) {
->contains(function (Route $route) use ($name, $middleware, $withoutMiddleware, $controllerMethod, $controller, $uri, $httpMethods, $domain, $wheres, $isFallback, $enforcesScopedBindings, $preventsScopedBindings, $defaults, $withTrashed) {
foreach (Arr::wrap($httpMethods) as $httpMethod) {
if (! in_array(strtoupper($httpMethod), $route->methods)) {
if (!in_array(strtoupper($httpMethod), $route->methods)) {
return false;
}
}
Expand All @@ -84,6 +90,10 @@ public function assertRouteRegistered(
return false;
}

if (array_diff($withoutMiddleware, $route->excludedMiddleware())) {
return false;
}

if ($route->getName() !== $name) {
return false;
}
Expand Down
14 changes: 14 additions & 0 deletions tests/TestClasses/Controllers/WithoutMiddlewareTestController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Spatie\RouteAttributes\Tests\TestClasses\Controllers;

use Spatie\RouteAttributes\Attributes\Route;
use Spatie\RouteAttributes\Tests\TestClasses\Middleware\SkippedMiddleware;

class WithoutMiddlewareTestController
{
#[Route('get', 'without-middleware', withoutMiddleware: SkippedMiddleware::class)]
public function withoutMiddleware()
{
}
}
14 changes: 14 additions & 0 deletions tests/TestClasses/Middleware/SkippedMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Spatie\RouteAttributes\Tests\TestClasses\Middleware;

use Closure;
use Illuminate\Http\Request;

class SkippedMiddleware
{
public function handle(Request $request, Closure $next)
{
return $next($request);
}
}