Skip to content

Conversation

@binaryfire
Copy link
Contributor

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

  // Insert middleware before existing middleware in priority list
  $kernel->addToMiddlewarePriorityBefore(StartSession::class, MyMiddleware::class);

  // Insert middleware after existing middleware in priority list
  $kernel->addToMiddlewarePriorityAfter(StartSession::class, MyMiddleware::class);

  // Also accepts an array (inserts before first/after last found)
  $kernel->addToMiddlewarePriorityAfter([StartSession::class, VerifyCsrfToken::class], MyMiddleware::class);

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:

  // In a package service provider
  public function boot(): void
  {
      $kernel = $this->app->get(MiddlewareContract::class);
      $kernel->appendMiddlewareToGroup('web', MyPackageMiddleware::class);
      $kernel->addToMiddlewarePriorityAfter(StartSession::class, MyPackageMiddleware::class);
  }

Additional Fix

Fixed a minor bug where $this->cachedMiddleware[] = [] was appending empty arrays instead of clearing the cache ($this->cachedMiddleware = []).

References

Copy link

Copilot AI left a 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 addToMiddlewarePriorityBefore and addToMiddlewarePriorityAfter public methods with array/string parameter support
  • Implements shared logic in addToMiddlewarePriorityRelative that 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);
Copy link

Copilot AI Dec 18, 2025

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.

Suggested change
$middlewareIndex = array_search($existingMiddleware, $this->middlewarePriority);
$middlewareIndex = array_search($existingMiddleware, $this->middlewarePriority, true);

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

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.

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)) {
Copy link

Copilot AI Dec 18, 2025

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.

Suggested change
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)) {

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

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.

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)) {
Copy link

Copilot AI Dec 18, 2025

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.

Suggested change
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)) {

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

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

@binaryfire
Copy link
Contributor Author

@albertcht I don't think any of Copilot's suggestions are worth implementing, but happy to make changes if you want. Let me know.

@binaryfire binaryfire changed the title Port addToMiddlewarePriorityBefore and addToMiddlewarePriorityAfter methods from Laravel feat: Port addToMiddlewarePriorityBefore and addToMiddlewarePriorityAfter methods from Laravel Dec 19, 2025
@binaryfire binaryfire changed the title feat: Port addToMiddlewarePriorityBefore and addToMiddlewarePriorityAfter methods from Laravel feat: port addToMiddlewarePriorityBefore and addToMiddlewarePriorityAfter methods from Laravel Dec 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant