Skip to content

Commit

Permalink
Improve
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Nov 3, 2023
1 parent a0c7867 commit 3694008
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
9 changes: 3 additions & 6 deletions src/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use InvalidArgumentException;
use RuntimeException;
use Yiisoft\Router\Internal\MiddlewareFilter;

use function in_array;

Expand All @@ -18,6 +19,7 @@ final class Group

/**
* @var array[]|callable[]|string[]
* @psalm-var list<array|callable|string>
*/
private array $middlewares = [];

Expand Down Expand Up @@ -209,12 +211,7 @@ private function getEnabledMiddlewares(): array
return $this->enabledMiddlewaresCache;
}

$this->enabledMiddlewaresCache = array_values(
array_filter(
$this->middlewares,
fn ($definition) => !in_array($definition, $this->disabledMiddlewares, true)
)
);
$this->enabledMiddlewaresCache = MiddlewareFilter::filter($this->middlewares, $this->disabledMiddlewares);

return $this->enabledMiddlewaresCache;
}
Expand Down
33 changes: 33 additions & 0 deletions src/Internal/MiddlewareFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Router\Internal;

/**
* @internal
*/
final class MiddlewareFilter
{
/**
* @param array[]|callable[]|string[] $middlewares
* @return array[]|callable[]|string[]
*
* @psalm-param list<array|callable|string> $middlewares
* @psalm-return list<array|callable|string>
*/
public static function filter(array $middlewares, array $disabledMiddlewares): array
{
$result = [];

foreach ($middlewares as $middleware) {
if (in_array($middleware, $disabledMiddlewares, true)) {
continue;
}

$result[] = $middleware;
}

return $result;
}
}
8 changes: 2 additions & 6 deletions src/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use RuntimeException;
use Stringable;
use Yiisoft\Http\Method;
use Yiisoft\Router\Internal\MiddlewareFilter;

use function in_array;

Expand Down Expand Up @@ -309,12 +310,7 @@ private function getEnabledMiddlewares(): array
return $this->enabledMiddlewaresCache;

Check warning on line 310 in src/Route.php

View check run for this annotation

Codecov / codecov/patch

src/Route.php#L310

Added line #L310 was not covered by tests
}

$this->enabledMiddlewaresCache = array_values(
array_filter(
$this->middlewares,
fn ($definition) => !in_array($definition, $this->disabledMiddlewares, true)
)
);
$this->enabledMiddlewaresCache = MiddlewareFilter::filter($this->middlewares, $this->disabledMiddlewares);

return $this->enabledMiddlewaresCache;
}
Expand Down

0 comments on commit 3694008

Please sign in to comment.