Skip to content

Commit 3746fb4

Browse files
authored
Merge pull request #151 from mikield/main
Add WithoutMiddleware option to route
2 parents 4071f88 + f2289a6 commit 3746fb4

File tree

13 files changed

+123
-22
lines changed

13 files changed

+123
-22
lines changed

src/Attributes/Any.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ public function __construct(
1212
string $uri,
1313
?string $name = null,
1414
array | string $middleware = [],
15+
array | string $withoutMiddleware = [],
1516
) {
1617
parent::__construct(
1718
methods: Router::$verbs,
1819
uri: $uri,
1920
name: $name,
2021
middleware: $middleware,
22+
withoutMiddleware: $withoutMiddleware
2123
);
2224
}
2325
}

src/Attributes/Delete.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ public function __construct(
1111
string $uri,
1212
?string $name = null,
1313
array | string $middleware = [],
14+
array | string $withoutMiddleware = [],
1415
) {
1516
parent::__construct(
1617
methods: ['delete'],
1718
uri: $uri,
1819
name: $name,
1920
middleware: $middleware,
21+
withoutMiddleware: $withoutMiddleware
2022
);
2123
}
2224
}

src/Attributes/Get.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ public function __construct(
1111
string $uri,
1212
?string $name = null,
1313
array | string $middleware = [],
14+
array | string $withoutMiddleware = [],
1415
) {
1516
parent::__construct(
1617
methods: ['get'],
1718
uri: $uri,
1819
name: $name,
1920
middleware: $middleware,
21+
withoutMiddleware: $withoutMiddleware
2022
);
2123
}
2224
}

src/Attributes/Patch.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ public function __construct(
1111
string $uri,
1212
?string $name = null,
1313
array | string $middleware = [],
14+
array | string $withoutMiddleware = [],
1415
) {
1516
parent::__construct(
1617
methods: ['patch'],
1718
uri: $uri,
1819
name: $name,
1920
middleware: $middleware,
21+
withoutMiddleware: $withoutMiddleware
2022
);
2123
}
2224
}

src/Attributes/Post.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ public function __construct(
1111
string $uri,
1212
?string $name = null,
1313
array | string $middleware = [],
14+
array | string $withoutMiddleware = [],
1415
) {
1516
parent::__construct(
1617
methods: ['post'],
1718
uri: $uri,
1819
name: $name,
1920
middleware: $middleware,
21+
withoutMiddleware: $withoutMiddleware
2022
);
2123
}
2224
}

src/Attributes/Put.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ public function __construct(
1111
string $uri,
1212
?string $name = null,
1313
array | string $middleware = [],
14+
array | string $withoutMiddleware = [],
1415
) {
1516
parent::__construct(
1617
methods: ['put'],
1718
uri: $uri,
1819
name: $name,
1920
middleware: $middleware,
21+
withoutMiddleware: $withoutMiddleware
2022
);
2123
}
2224
}

src/Attributes/Route.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ class Route implements RouteAttribute
1313

1414
public array $middleware;
1515

16+
public array $withoutMiddleware;
17+
1618
public function __construct(
1719
array | string $methods,
1820
public string $uri,
1921
public ?string $name = null,
2022
array | string $middleware = [],
23+
array | string $withoutMiddleware = [],
2124
) {
2225
$this->methods = array_map(
2326
static fn (string $verb) => in_array(
@@ -29,5 +32,6 @@ public function __construct(
2932
Arr::wrap($methods)
3033
);
3134
$this->middleware = Arr::wrap($middleware);
35+
$this->withoutMiddleware = Arr::wrap($withoutMiddleware);
3236
}
3337
}

src/RouteRegistrar.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,19 +116,19 @@ protected function processAttributes(string $className): void
116116
$class = new ReflectionClass($className);
117117

118118
$classRouteAttributes = new ClassRouteAttributes($class);
119-
119+
120120
$groups = $classRouteAttributes->groups();
121-
121+
122122
foreach ($groups as $group) {
123123
$router = $this->router;
124124
$router->group($group, fn () => $this->registerRoutes($class, $classRouteAttributes));
125125
}
126-
126+
127127
if ($classRouteAttributes->resource()) {
128128
$this->registerResource($class, $classRouteAttributes);
129129
}
130130

131-
131+
132132
}
133133

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

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

177+
$this->addWithoutMiddlewareToRoute($classRouteAttributes, $attributeClass, $route);
178+
179+
177180
$this->setWithTrashedIfAvailable($classRouteAttributes, $withTrashedAttribute, $route);
178181

179182

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

270+
/**
271+
* @param ClassRouteAttributes $classRouteAttributes
272+
* @param Route $attributeClass
273+
* @param \Illuminate\Routing\Route $route
274+
* @return void
275+
*/
276+
private function addWithoutMiddlewareToRoute(ClassRouteAttributes $classRouteAttributes, Route $attributeClass, \Illuminate\Routing\Route $route): void
277+
{
278+
$methodWithoutMiddleware = $attributeClass->withoutMiddleware;
279+
$route->withoutMiddleware($methodWithoutMiddleware);
280+
}
281+
267282
/**
268283
* @param ClassRouteAttributes $classRouteAttributes
269284
* @param mixed $defaultAttributes

tests/AttributeTests/FallbackAttributeTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ public function it_can_register_a_route_as_fallback()
1414

1515
$this
1616
->assertRegisteredRoutesCount(1)
17-
->assertRouteRegistered(FallbackTestController::class, 'myFallbackMethod', 'get', 'my-fallback-method', [], null, null, [], true);
17+
->assertRouteRegistered(
18+
controller: FallbackTestController::class,
19+
controllerMethod: 'myFallbackMethod',
20+
httpMethods: 'get',
21+
uri: 'my-fallback-method',
22+
isFallback: true
23+
);
1824
}
1925
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Spatie\RouteAttributes\Tests\AttributeTests;
4+
5+
use Spatie\RouteAttributes\Tests\TestCase;
6+
use Spatie\RouteAttributes\Tests\TestClasses\Controllers\WithoutMiddlewareTestController;
7+
use Spatie\RouteAttributes\Tests\TestClasses\Middleware\SkippedMiddleware;
8+
9+
class WithoutMiddlewareAttributeTest extends TestCase
10+
{
11+
/** @test */
12+
public function it_can_skip_middleware_added_to_class()
13+
{
14+
$this->routeRegistrar->registerClass(WithoutMiddlewareTestController::class);
15+
16+
17+
$this
18+
->assertRegisteredRoutesCount(1)
19+
->assertRouteRegistered(
20+
WithoutMiddlewareTestController::class,
21+
controllerMethod: 'withoutMiddleware',
22+
uri: 'without-middleware',
23+
withoutMiddleware: [SkippedMiddleware::class],
24+
);
25+
}
26+
}

0 commit comments

Comments
 (0)