Skip to content

Commit

Permalink
[11.x] Named static methods for middleware (#1695)
Browse files Browse the repository at this point in the history
* Standardise of `using` for middleware

* Fix

* Use `func_get_args`

* Formatting

* formatting

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
michaelnabil230 and taylorotwell authored Nov 2, 2023
1 parent 4017301 commit cf06f97
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Http/Middleware/CheckCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ public function __construct(ResourceServer $server, TokenRepository $repository)
$this->repository = $repository;
}

/**
* Specify the scopes for the middleware.
*
* @param array|string $scopes
* @return string
*/
public static function using(...$scopes)
{
if (is_array($scopes[0])) {
return static::class.':'.implode(',', $scopes[0]);
} else {
return static::class.':'.implode(',', $scopes);
}
}

/**
* Handle an incoming request.
*
Expand Down
15 changes: 15 additions & 0 deletions src/Http/Middleware/CheckForAnyScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@

class CheckForAnyScope
{
/**
* Specify the scopes for the middleware.
*
* @param array|string $scopes
* @return string
*/
public static function using(...$scopes)
{
if (is_array($scopes[0])) {
return static::class.':'.implode(',', $scopes[0]);
} else {
return static::class.':'.implode(',', $scopes);
}
}

/**
* Handle the incoming request.
*
Expand Down
15 changes: 15 additions & 0 deletions src/Http/Middleware/CheckScopes.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@

class CheckScopes
{
/**
* Specify the scopes for the middleware.
*
* @param array|string $scopes
* @return string
*/
public static function using(...$scopes)
{
if (is_array($scopes[0])) {
return static::class.':'.implode(',', $scopes[0]);
} else {
return static::class.':'.implode(',', $scopes);
}
}

/**
* Handle the incoming request.
*
Expand Down
13 changes: 13 additions & 0 deletions src/Http/Middleware/CreateFreshApiToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ public function __construct(ApiTokenCookieFactory $cookieFactory)
$this->cookieFactory = $cookieFactory;
}

/**
* Specify the guard for the middleware.
*
* @param string|null $guard
* @return string
*/
public static function using($guard = null)
{
$guard = is_null($guard) ? '' : ':'.$guard;

return static::class.$guard;
}

/**
* Handle an incoming request.
*
Expand Down
15 changes: 15 additions & 0 deletions tests/Feature/ActingAsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ public function testActingAsWhenTheRouteIsProtectedByCheckScopesMiddleware()
$response->assertSee('bar');
}

public function testItCanGenerateDefinitionViaStaticMethod()
{
$signature = (string) CheckScopes::using('admin');
$this->assertSame('Laravel\Passport\Http\Middleware\CheckScopes:admin', $signature);

$signature = (string) CheckScopes::using('admin', 'footest');
$this->assertSame('Laravel\Passport\Http\Middleware\CheckScopes:admin,footest', $signature);

$signature = (string) CheckForAnyScope::using('admin');
$this->assertSame('Laravel\Passport\Http\Middleware\CheckForAnyScope:admin', $signature);

$signature = (string) CheckForAnyScope::using('admin', 'footest');
$this->assertSame('Laravel\Passport\Http\Middleware\CheckForAnyScope:admin,footest', $signature);
}

public function testActingAsWhenTheRouteIsProtectedByCheckForAnyScopeMiddleware()
{
$this->withoutExceptionHandling();
Expand Down

0 comments on commit cf06f97

Please sign in to comment.