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

Exclude method "middleware" from discovered routes #30

Merged
merged 5 commits into from
Jan 13, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,16 @@ public function transform(Collection $pendingRoutes): Collection
return $pendingRoutes->each(function (PendingRoute $pendingRoute) {
$pendingRoute->actions = $pendingRoute
->actions
->reject(fn (PendingRouteAction $pendingRouteAction) => in_array(
$pendingRouteAction->method->class,
$this->rejectMethodsInClasses
));
->reject(function (PendingRouteAction $pendingRouteAction) {
if($pendingRouteAction->method->name == "middleware" && is_subclass_of($pendingRouteAction->method->class, "Illuminate\\Routing\\Controllers\\HasMiddleware")){
return true;
}

return in_array(
$pendingRouteAction->method->class,
$this->rejectMethodsInClasses
);
});
});
}
}
33 changes: 31 additions & 2 deletions tests/RouteDiscoveryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Spatie\RouteDiscovery\Tests\Support\TestClasses\Controllers\Domain\DomainController;
use Spatie\RouteDiscovery\Tests\Support\TestClasses\Controllers\DoNotDiscoverController\DoNotDiscoverThisMethodController;
use Spatie\RouteDiscovery\Tests\Support\TestClasses\Controllers\DoNotDiscoverMethod\DoNotDiscoverMethodController;
use Spatie\RouteDiscovery\Tests\Support\TestClasses\Controllers\DoNotDiscoverMiddleware\DiscoverMiddlewareIfNotLaravelMethodController;
use Spatie\RouteDiscovery\Tests\Support\TestClasses\Controllers\DoNotDiscoverMiddleware\DoNotDiscoverMiddlewareController;
use Spatie\RouteDiscovery\Tests\Support\TestClasses\Controllers\Invokable\InvokableController;
use Spatie\RouteDiscovery\Tests\Support\TestClasses\Controllers\Middleware\MiddlewareOnControllerController;
use Spatie\RouteDiscovery\Tests\Support\TestClasses\Controllers\Middleware\MiddlewareOnMethodController;
Expand Down Expand Up @@ -360,7 +362,6 @@
);
});


it('can override the http method', function () {
$this
->routeRegistrar
Expand Down Expand Up @@ -447,6 +448,34 @@
);
});

it('can avoid discovering the laravel middleware method', function () {
if(!interface_exists('Illuminate\Routing\Controllers\HasMiddleware')) {
$this->markTestSkipped("Laravel 9 or up is required to run this test.");
}

$this
->routeRegistrar
->registerDirectory(controllersPath('DoNotDiscoverMiddleware'));

$this
->assertRegisteredRoutesCount(3)
->assertRouteRegistered(
DoNotDiscoverMiddlewareController::class,
controllerMethod: 'method',
uri: 'do-not-discover-middleware/method',
)
->assertRouteRegistered(
DiscoverMiddlewareIfNotLaravelMethodController::class,
controllerMethod: 'method',
uri: 'discover-middleware-if-not-laravel-method/method',
)
->assertRouteRegistered(
DiscoverMiddlewareIfNotLaravelMethodController::class,
controllerMethod: 'middleware',
uri: 'discover-middleware-if-not-laravel-method/middleware',
);
});

it('can avoid discovering a controller', function () {
$this
->routeRegistrar
Expand Down Expand Up @@ -550,7 +579,7 @@
$this->assertRegisteredRoutesCount(3);

$registeredUris = collect(app()->router->getRoutes())
->map(fn (Route $route) => $route->uri)
->map(fn(Route $route) => $route->uri)
->toArray();

expect($registeredUris)->toEqual([
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Spatie\RouteDiscovery\Tests\Support\TestClasses\Controllers\DoNotDiscoverMiddleware;

class DiscoverMiddlewareIfNotLaravelMethodController
{
public static function middleware(): array {
return [];
}

public function method()
{
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Spatie\RouteDiscovery\Tests\Support\TestClasses\Controllers\DoNotDiscoverMiddleware;

use Illuminate\Routing\Controllers\HasMiddleware;

class DoNotDiscoverMiddlewareController implements HasMiddleware
{
public static function middleware(): array {
return [];
}

public function method()
{
}

}
Loading