Skip to content
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

Add the ability to append and prepend middleware priority from the application builder #53326

Merged
12 changes: 12 additions & 0 deletions src/Illuminate/Foundation/Configuration/ApplicationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,18 @@ public function withMiddleware(?callable $callback = null)
if ($priorities = $middleware->getMiddlewarePriority()) {
$kernel->setMiddlewarePriority($priorities);
}

if ($priorityAppends = $middleware->getMiddlewarePriorityAppends()) {
foreach ($priorityAppends as $middleware => $after) {
$kernel->addToMiddlewarePriorityAfter($after, $middleware);
}
}

if ($priorityPrepends = $middleware->getMiddlewarePriorityPrepends()) {
foreach ($priorityPrepends as $middleware => $before) {
$kernel->addToMiddlewarePriorityBefore($before, $middleware);
}
}
Comment on lines +273 to +283
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When using both prependToPriorityList() and appendToPriorityList() the $middleware variable is overridden on line 274. getMiddlewarePriorityPrepends() is then called on a string.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah this seems to be a bug either prepending!

});

return $this;
Expand Down
62 changes: 62 additions & 0 deletions src/Illuminate/Foundation/Configuration/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,20 @@ class Middleware
*/
protected $priority = [];

/**
* The middleware to prepend to the middleware priority definition.
*
* @var array
*/
protected $prependPriority = [];

/**
* The middleware to append to the middleware priority definition.
*
* @var array
*/
protected $appendPriority = [];

/**
* Prepend middleware to the application's global middleware stack.
*
Expand Down Expand Up @@ -400,6 +414,34 @@ public function priority(array $priority)
return $this;
}

/**
* Prepend middleware to the priority middleware.
*
* @param array|string $before
* @param string $prepend
* @return $this
*/
public function prependToPriorityList($before, $prepend)
{
$this->prependPriority[$prepend] = $before;

return $this;
}

/**
* Append middleware to the priority middleware.
*
* @param array|string $after
* @param string $append
* @return $this
*/
public function appendToPriorityList($after, $append)
{
$this->appendPriority[$append] = $after;

return $this;
}

/**
* Get the global middleware.
*
Expand Down Expand Up @@ -766,4 +808,24 @@ public function getMiddlewarePriority()
{
return $this->priority;
}

/**
* Get the middleware to prepend to the middleware priority definition.
*
* @return array
*/
public function getMiddlewarePriorityPrepends()
{
return $this->prependPriority;
}

/**
* Get the middleware to append to the middleware priority definition.
*
* @return array
*/
public function getMiddlewarePriorityAppends()
{
return $this->appendPriority;
}
}