Skip to content

Commit

Permalink
Merge pull request #30 from LWlook/main
Browse files Browse the repository at this point in the history
Exclude method "middleware" from discovered routes
  • Loading branch information
freekmurze authored Jan 13, 2025
2 parents 2016769 + f80c42c commit 6c7ab74
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 6 deletions.
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()
{
}

}

0 comments on commit 6c7ab74

Please sign in to comment.