Skip to content

Commit 942b5a0

Browse files
Add the ability to append and prepend middleware priority from the application builder (#53326)
* Add the ability to append and prepend middleware priority from the application builder * Style fixes * Update method signature, changing names and parameter order * Update parameter names to match change request * Fix parameter names for real this time * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent e63fdc7 commit 942b5a0

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

src/Illuminate/Foundation/Configuration/ApplicationBuilder.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,18 @@ public function withMiddleware(?callable $callback = null)
269269
if ($priorities = $middleware->getMiddlewarePriority()) {
270270
$kernel->setMiddlewarePriority($priorities);
271271
}
272+
273+
if ($priorityAppends = $middleware->getMiddlewarePriorityAppends()) {
274+
foreach ($priorityAppends as $middleware => $after) {
275+
$kernel->addToMiddlewarePriorityAfter($after, $middleware);
276+
}
277+
}
278+
279+
if ($priorityPrepends = $middleware->getMiddlewarePriorityPrepends()) {
280+
foreach ($priorityPrepends as $middleware => $before) {
281+
$kernel->addToMiddlewarePriorityBefore($before, $middleware);
282+
}
283+
}
272284
});
273285

274286
return $this;

src/Illuminate/Foundation/Configuration/Middleware.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,20 @@ class Middleware
145145
*/
146146
protected $priority = [];
147147

148+
/**
149+
* The middleware to prepend to the middleware priority definition.
150+
*
151+
* @var array
152+
*/
153+
protected $prependPriority = [];
154+
155+
/**
156+
* The middleware to append to the middleware priority definition.
157+
*
158+
* @var array
159+
*/
160+
protected $appendPriority = [];
161+
148162
/**
149163
* Prepend middleware to the application's global middleware stack.
150164
*
@@ -400,6 +414,34 @@ public function priority(array $priority)
400414
return $this;
401415
}
402416

417+
/**
418+
* Prepend middleware to the priority middleware.
419+
*
420+
* @param array|string $before
421+
* @param string $prepend
422+
* @return $this
423+
*/
424+
public function prependToPriorityList($before, $prepend)
425+
{
426+
$this->prependPriority[$prepend] = $before;
427+
428+
return $this;
429+
}
430+
431+
/**
432+
* Append middleware to the priority middleware.
433+
*
434+
* @param array|string $after
435+
* @param string $append
436+
* @return $this
437+
*/
438+
public function appendToPriorityList($after, $append)
439+
{
440+
$this->appendPriority[$append] = $after;
441+
442+
return $this;
443+
}
444+
403445
/**
404446
* Get the global middleware.
405447
*
@@ -766,4 +808,24 @@ public function getMiddlewarePriority()
766808
{
767809
return $this->priority;
768810
}
811+
812+
/**
813+
* Get the middleware to prepend to the middleware priority definition.
814+
*
815+
* @return array
816+
*/
817+
public function getMiddlewarePriorityPrepends()
818+
{
819+
return $this->prependPriority;
820+
}
821+
822+
/**
823+
* Get the middleware to append to the middleware priority definition.
824+
*
825+
* @return array
826+
*/
827+
public function getMiddlewarePriorityAppends()
828+
{
829+
return $this->appendPriority;
830+
}
769831
}

0 commit comments

Comments
 (0)