Skip to content

Commit cf06f97

Browse files
[11.x] Named static methods for middleware (#1695)
* Standardise of `using` for middleware * Fix * Use `func_get_args` * Formatting * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 4017301 commit cf06f97

File tree

5 files changed

+73
-0
lines changed

5 files changed

+73
-0
lines changed

src/Http/Middleware/CheckCredentials.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,21 @@ public function __construct(ResourceServer $server, TokenRepository $repository)
3939
$this->repository = $repository;
4040
}
4141

42+
/**
43+
* Specify the scopes for the middleware.
44+
*
45+
* @param array|string $scopes
46+
* @return string
47+
*/
48+
public static function using(...$scopes)
49+
{
50+
if (is_array($scopes[0])) {
51+
return static::class.':'.implode(',', $scopes[0]);
52+
} else {
53+
return static::class.':'.implode(',', $scopes);
54+
}
55+
}
56+
4257
/**
4358
* Handle an incoming request.
4459
*

src/Http/Middleware/CheckForAnyScope.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,21 @@
77

88
class CheckForAnyScope
99
{
10+
/**
11+
* Specify the scopes for the middleware.
12+
*
13+
* @param array|string $scopes
14+
* @return string
15+
*/
16+
public static function using(...$scopes)
17+
{
18+
if (is_array($scopes[0])) {
19+
return static::class.':'.implode(',', $scopes[0]);
20+
} else {
21+
return static::class.':'.implode(',', $scopes);
22+
}
23+
}
24+
1025
/**
1126
* Handle the incoming request.
1227
*

src/Http/Middleware/CheckScopes.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,21 @@
77

88
class CheckScopes
99
{
10+
/**
11+
* Specify the scopes for the middleware.
12+
*
13+
* @param array|string $scopes
14+
* @return string
15+
*/
16+
public static function using(...$scopes)
17+
{
18+
if (is_array($scopes[0])) {
19+
return static::class.':'.implode(',', $scopes[0]);
20+
} else {
21+
return static::class.':'.implode(',', $scopes);
22+
}
23+
}
24+
1025
/**
1126
* Handle the incoming request.
1227
*

src/Http/Middleware/CreateFreshApiToken.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ public function __construct(ApiTokenCookieFactory $cookieFactory)
3535
$this->cookieFactory = $cookieFactory;
3636
}
3737

38+
/**
39+
* Specify the guard for the middleware.
40+
*
41+
* @param string|null $guard
42+
* @return string
43+
*/
44+
public static function using($guard = null)
45+
{
46+
$guard = is_null($guard) ? '' : ':'.$guard;
47+
48+
return static::class.$guard;
49+
}
50+
3851
/**
3952
* Handle an incoming request.
4053
*

tests/Feature/ActingAsTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,21 @@ public function testActingAsWhenTheRouteIsProtectedByCheckScopesMiddleware()
4747
$response->assertSee('bar');
4848
}
4949

50+
public function testItCanGenerateDefinitionViaStaticMethod()
51+
{
52+
$signature = (string) CheckScopes::using('admin');
53+
$this->assertSame('Laravel\Passport\Http\Middleware\CheckScopes:admin', $signature);
54+
55+
$signature = (string) CheckScopes::using('admin', 'footest');
56+
$this->assertSame('Laravel\Passport\Http\Middleware\CheckScopes:admin,footest', $signature);
57+
58+
$signature = (string) CheckForAnyScope::using('admin');
59+
$this->assertSame('Laravel\Passport\Http\Middleware\CheckForAnyScope:admin', $signature);
60+
61+
$signature = (string) CheckForAnyScope::using('admin', 'footest');
62+
$this->assertSame('Laravel\Passport\Http\Middleware\CheckForAnyScope:admin,footest', $signature);
63+
}
64+
5065
public function testActingAsWhenTheRouteIsProtectedByCheckForAnyScopeMiddleware()
5166
{
5267
$this->withoutExceptionHandling();

0 commit comments

Comments
 (0)