-
-
Notifications
You must be signed in to change notification settings - Fork 14
feat: port addToMiddlewarePriorityBefore and addToMiddlewarePriorityAfter methods from Laravel
#296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -204,7 +204,7 @@ public function prependMiddleware(string $middleware): static | |||||||||||||||||||||
| array_unshift($this->middleware, $middleware); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| $this->cachedMiddleware[] = []; | ||||||||||||||||||||||
| $this->cachedMiddleware = []; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return $this; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
@@ -218,7 +218,7 @@ public function pushMiddleware(string $middleware): static | |||||||||||||||||||||
| $this->middleware[] = $middleware; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| $this->cachedMiddleware[] = []; | ||||||||||||||||||||||
| $this->cachedMiddleware = []; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return $this; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
@@ -238,7 +238,7 @@ public function prependMiddlewareToGroup(string $group, string $middleware): sta | |||||||||||||||||||||
| array_unshift($this->middlewareGroups[$group], $middleware); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| $this->cachedMiddleware[] = []; | ||||||||||||||||||||||
| $this->cachedMiddleware = []; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return $this; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
@@ -258,7 +258,7 @@ public function appendMiddlewareToGroup(string $group, string $middleware): stat | |||||||||||||||||||||
| $this->middlewareGroups[$group][] = $middleware; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| $this->cachedMiddleware[] = []; | ||||||||||||||||||||||
| $this->cachedMiddleware = []; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return $this; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
@@ -272,7 +272,7 @@ public function prependToMiddlewarePriority(string $middleware): static | |||||||||||||||||||||
| array_unshift($this->middlewarePriority, $middleware); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| $this->cachedMiddleware[] = []; | ||||||||||||||||||||||
| $this->cachedMiddleware = []; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return $this; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
@@ -286,7 +286,63 @@ public function appendToMiddlewarePriority(string $middleware): static | |||||||||||||||||||||
| $this->middlewarePriority[] = $middleware; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| $this->cachedMiddleware[] = []; | ||||||||||||||||||||||
| $this->cachedMiddleware = []; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return $this; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * Add the given middleware to the middleware priority list before other middleware. | ||||||||||||||||||||||
| * | ||||||||||||||||||||||
| * @param array<int, string>|string $before | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| public function addToMiddlewarePriorityBefore(string|array $before, string $middleware): static | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| return $this->addToMiddlewarePriorityRelative($before, $middleware, after: false); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * Add the given middleware to the middleware priority list after other middleware. | ||||||||||||||||||||||
| * | ||||||||||||||||||||||
| * @param array<int, string>|string $after | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| public function addToMiddlewarePriorityAfter(string|array $after, string $middleware): static | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| return $this->addToMiddlewarePriorityRelative($after, $middleware, after: true); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * Add the given middleware to the middleware priority list relative to other middleware. | ||||||||||||||||||||||
| * | ||||||||||||||||||||||
| * @param array<int, string>|string $existing | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| protected function addToMiddlewarePriorityRelative(string|array $existing, string $middleware, bool $after = true): static | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| if (! in_array($middleware, $this->middlewarePriority)) { | ||||||||||||||||||||||
| $index = $after ? 0 : count($this->middlewarePriority); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| foreach ((array) $existing as $existingMiddleware) { | ||||||||||||||||||||||
| if (in_array($existingMiddleware, $this->middlewarePriority)) { | ||||||||||||||||||||||
|
Comment on lines
+321
to
+325
|
||||||||||||||||||||||
| if (! in_array($middleware, $this->middlewarePriority)) { | |
| $index = $after ? 0 : count($this->middlewarePriority); | |
| foreach ((array) $existing as $existingMiddleware) { | |
| if (in_array($existingMiddleware, $this->middlewarePriority)) { | |
| if (! in_array($middleware, $this->middlewarePriority, true)) { | |
| $index = $after ? 0 : count($this->middlewarePriority); | |
| foreach ((array) $existing as $existingMiddleware) { | |
| if (in_array($existingMiddleware, $this->middlewarePriority, true)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@albertcht This seems to be a duplicate of the last suggestion
Copilot
AI
Dec 18, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider using strict comparison (third parameter true) in array_search for better type safety. This ensures that the search uses strict type checking (===) rather than loose comparison (==), which is particularly important for string-based middleware class names that could be confused with numeric indices.
| $middlewareIndex = array_search($existingMiddleware, $this->middlewarePriority); | |
| $middlewareIndex = array_search($existingMiddleware, $this->middlewarePriority, true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@albertcht Laravel's implementation doesn't use strict comparison here either - their addToMiddlewarePriorityRelative uses plain array_search(). The type safety issue Copilot raises (string class names confused with numeric indices) isn't applicable since both $existingMiddleware and the array values are strings. The existing array_search calls in this file (lines 156, 203, 217, 237, 257) also don't use strict comparison, so adding it here would be inconsistent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider using strict comparison (third parameter true) in in_array for better type safety. This ensures type-safe comparisons when checking if middleware exists in the priority list.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@albertcht Same as above - Laravel's implementation doesn't use strict comparison for these
in_arraycalls, and neither do the existing calls in this file (lines 195, 271, 285). The$middlewareparam is typedstringand$middlewarePriorityisstring[], so there's no type coercion risk. If strict comparison is important, I think it should be a separate PR that updates all the calls consistently instead of just these new methods.