Skip to content

Commit

Permalink
pkp#10342 Resolve PHP warning
Browse files Browse the repository at this point in the history
  • Loading branch information
asmecher committed Sep 12, 2024
1 parent d2f7fa5 commit 4d49e24
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions classes/middleware/traits/HasRequiredMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ abstract public function requiredMiddleware(): array;
public function hasRequiredMiddleware(Request $request, int $matchingCriteria = self::MIDDLEWARE_MATCH_STRICT): bool
{
$requiredMiddleware = collect($this->requiredMiddleware());

if ($requiredMiddleware->count() === 0) {
throw new Exception(
sprintf(
Expand All @@ -52,22 +52,23 @@ public function hasRequiredMiddleware(Request $request, int $matchingCriteria =

$currentRoute = PKPBaseController::getRequestedRoute($request);
$routeMiddleware = collect($currentRoute?->middleware() ?? []);

$router = app('router'); /** @var \Illuminate\Routing\Router $router */
$routerMiddleware = $router->getMiddleware();

// need to replace the alias name with full class path
$routeMiddleware = $routeMiddleware->map(function (string $middleware) use($routerMiddleware): string {
$routeMiddleware = $routeMiddleware->map(function (string $middleware) use ($routerMiddleware): string {
// extract the middleware class or alias name if in format
// such as `has.roles:1|16|17` or `PKP\middleware\HasRoles:1|16|17`
$middleware = array_shift(array_pad(explode(":", $middleware, 2), 2, []));
$fragments = array_pad(explode(':', $middleware, 2), 2, []);
$middleware = array_shift($fragments);

if (class_exists($middleware)) {
return $middleware;
}

return $routerMiddleware[$middleware] ?? $middleware;
});
});

return match($matchingCriteria) {
static::MIDDLEWARE_MATCH_STRICT
Expand Down

0 comments on commit 4d49e24

Please sign in to comment.