-
-
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?
feat: port addToMiddlewarePriorityBefore and addToMiddlewarePriorityAfter methods from Laravel
#296
Conversation
…ethods from Laravel
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.
Pull request overview
This PR ports two middleware priority management methods from Laravel (addToMiddlewarePriorityBefore and addToMiddlewarePriorityAfter), enabling packages to programmatically position middleware relative to existing middleware in the priority list. Additionally, it fixes a bug where cache clearing was incorrectly appending empty arrays instead of resetting the cache.
- Adds
addToMiddlewarePriorityBeforeandaddToMiddlewarePriorityAfterpublic methods with array/string parameter support - Implements shared logic in
addToMiddlewarePriorityRelativethat finds the appropriate insertion point - Corrects cache clearing across multiple methods (replacing
$this->cachedMiddleware[] = []with$this->cachedMiddleware = [])
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
src/foundation/src/Http/Contracts/MiddlewareContract.php |
Adds interface definitions for the two new middleware priority methods |
src/foundation/src/Http/Traits/HasMiddleware.php |
Implements the new methods, fixes cache clearing bug throughout the trait |
tests/Foundation/Http/KernelTest.php |
Adds comprehensive test coverage for both methods including edge cases (arrays, not found, duplicates, positioning) |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| foreach ((array) $existing as $existingMiddleware) { | ||
| if (in_array($existingMiddleware, $this->middlewarePriority)) { | ||
| $middlewareIndex = array_search($existingMiddleware, $this->middlewarePriority); |
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.
| if (! in_array($middleware, $this->middlewarePriority)) { | ||
| $index = $after ? 0 : count($this->middlewarePriority); | ||
|
|
||
| foreach ((array) $existing as $existingMiddleware) { | ||
| if (in_array($existingMiddleware, $this->middlewarePriority)) { |
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 in_array for better type safety. This ensures type-safe comparisons when checking if middleware exists in the priority list.
| 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 Same as above - Laravel's implementation doesn't use strict comparison for these in_array calls, and neither do the existing calls in this file (lines 195, 271, 285). The $middleware param is typed string and $middlewarePriority is string[], 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.
| if (! in_array($middleware, $this->middlewarePriority)) { | ||
| $index = $after ? 0 : count($this->middlewarePriority); | ||
|
|
||
| foreach ((array) $existing as $existingMiddleware) { | ||
| if (in_array($existingMiddleware, $this->middlewarePriority)) { |
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 in_array for better type safety. This ensures type-safe comparisons when checking if middleware already exists in the priority list before adding it.
| 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
|
@albertcht I don't think any of Copilot's suggestions are worth implementing, but happy to make changes if you want. Let me know. |
addToMiddlewarePriorityBefore and addToMiddlewarePriorityAfter methods from LaraveladdToMiddlewarePriorityBefore and addToMiddlewarePriorityAfter methods from Laravel
addToMiddlewarePriorityBefore and addToMiddlewarePriorityAfter methods from LaraveladdToMiddlewarePriorityBefore and addToMiddlewarePriorityAfter methods from Laravel
This PR ports 2 missing middleware priority methods from Laravel, enabling packages to programmatically insert middleware into the priority list relative to existing middleware.
Methods Added
Use Case
Packages that add middleware often need to control execution order relative to framework middleware. Previously, packages could only prepend/append to the priority list. Now they can position middleware precisely:
Additional Fix
Fixed a minor bug where
$this->cachedMiddleware[] = []was appending empty arrays instead of clearing the cache ($this->cachedMiddleware = []).References