From 6f33feba124d4a7ff2af4f3ed18583d67fb68f7c Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 18 Dec 2019 12:13:11 -0600 Subject: [PATCH] add some methods for manipulating middleware --- src/Illuminate/Foundation/Http/Kernel.php | 71 +++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/src/Illuminate/Foundation/Http/Kernel.php b/src/Illuminate/Foundation/Http/Kernel.php index dc99782cddf2..e527a88be5a1 100644 --- a/src/Illuminate/Foundation/Http/Kernel.php +++ b/src/Illuminate/Foundation/Http/Kernel.php @@ -10,6 +10,7 @@ use Illuminate\Routing\Pipeline; use Illuminate\Routing\Router; use Illuminate\Support\Facades\Facade; +use InvalidArgumentException; use Symfony\Component\Debug\Exception\FatalThrowableError; use Throwable; @@ -294,6 +295,76 @@ public function pushMiddleware($middleware) return $this; } + /** + * Prepend the given middleware to the given middleware group. + * + * @param string $group + * @param string $middleware + * @return $this + */ + public function prependMiddlewareToGroup($group, $middleware) + { + if (! isset($this->middlewareGroups[$group])) { + throw new InvalidArgumentException("The [{$group}] middleware group has not been defined."); + } + + if (array_search($middleware, $this->middlewareGroups[$group]) === false) { + array_unshift($this->middlewareGroups[$group], $middleware); + } + + return $this; + } + + /** + * Append the given middleware to the given middleware group. + * + * @param string $group + * @param string $middleware + * @return $this + */ + public function appendMiddlewareToGroup($group, $middleware) + { + if (! isset($this->middlewareGroups[$group])) { + throw new InvalidArgumentException("The [{$group}] middleware group has not been defined."); + } + + if (array_search($middleware, $this->middlewareGroups[$group]) === false) { + $this->middlewareGroups[$group][] = $middleware; + } + + return $this; + } + + /** + * Prepend the given middleware to the middleware priority list. + * + * @param string $middleware + * @return $this + */ + public function prependToMiddlewarePriority($middleware) + { + if (! in_array($middleware, $this->middlewarePriority)) { + array_unshift($this->middlewarePriority, $middleware); + } + + return $this; + } + + /** + * Append the given middleware to the middleware priority list. + * + * @param string $middleware + * @return $this + */ + public function appendToMiddlewarePriority($middleware) + { + if (! in_array($middleware, $this->middlewarePriority)) { + $this->middlewarePriority[] = $middleware; + } + + return $this; + } + /** * Get the bootstrap classes for the application. *