Merge the middleware group into the action.

@param  array  $action
@param  array  $middleware
@return array
481018
protected function mergeMiddlewareGroup(array $action, $middleware = null)
    {
        if (isset($middleware)) {
            if (isset($action['middleware'])) {
                $action['middleware'] = array_merge($middleware, $action['middleware']);
            } else {
                $action['middleware'] = $middleware;
            }
        }

        return $action;
    }
17409
public function addRoute($methods, $uri, $action)
    {
        if (\is_string($action)) {
            $action = ['uses' => $action, 'controller' => $action];
        } elseif ($action instanceof Closure) {
            $action = [$action];
        }

        $action = $this->mergeLastGroupAttributes($action);

        $action = $this->addControllerMiddlewareToRouteAction($action);

        $uri = $uri === '/' ? $uri : '/'.\trim($uri, '/');

        if (! empty($action['prefix'])) {
            $uri = '/'.\ltrim(\rtrim(\trim($action['prefix'], '/').'/'.\trim($uri, '/'), '/'), '/');

            unset($action['prefix']);
        }

        $action['uri'] = $uri;

        return $this->adapter->addRoute((array) $methods, $action['version'], $uri, $action);
    }
1333094
public function middleware($middlewares, bool $addToFront = false): MiddlewareAwareInterface
    {
        if (! is_array($middlewares)) {
            $middlewares = [$middlewares];
        }

        foreach ($middlewares as $middleware) {
            if ($addToFront) {
                //prepend Middleware
                array_unshift($this->middlewares, $middleware);
            } else {
                // append Middleware
                array_push($this->middlewares, $middleware);
            }
        }

        return $this;
    }
979768
public function middleware( $middleware = null )
	{
		if ( is_null( $middleware ) )
			return (array) Arr::get( $this->action, 'middleware', [] );

		if ( is_string( $middleware ) )
			$middleware = [$middleware];

		$this->action['middleware'] = array_merge( (array) Arr::get( $this->action, 'middleware', [] ), $middleware );

		return $this;
	}
13055
private function getMiddleWareFromGroup(RouteGroup $group, array $middleWareList = [])
    {
        if ($group->middleWare) {
            $middleWareList = \array_merge($group->middleWare, $middleWareList);
        }
        if (\is_null($group->parentGroup)) {
            return $middleWareList;
        }
        return $this->getMiddleWareFromGroup($group->parentGroup, $middleWareList);
    }
419286
public static function addStaticMiddlewares(...$middlewares): array
    {
        $stack = static::getStaticMiddlewareStack(true);
        foreach ($middlewares as $middleware) {
            $stack->append($middleware);
        }
        return $middlewares;
    }
1273901
function callMiddlewares(actionName, actionData, done) {
    let store = this;
    let index = -1;
    /**
     * Call next middleware or done
     * @function next
     * @param {*} actionData - An action data
     */
    function next(actionData) {
        index++;
        if(index === store.middlewares.length) {
            done(actionData);
        } else {
            store.middlewares[index](store, actionName, actionData, next);
        }
    }
    next(actionData);
}
91285
protected function orderMiddlewareByPriority(array $middleware): array
	{
		if(empty($this->middlewarePriority))
		{
			return $middleware;
		}

		$priority = array_intersect_key($this->middlewarePriority, $middleware) + array_fill_keys(array_keys(array_diff_key($middleware, $this->middlewarePriority)), static::MIDDLEWARE_DEFAULT_PRIORITY);

		// Sort the priority map using stable sorting

		$position = 0;

		foreach($priority as $key => $value)
		{
			$priority[$key] = [$position++, $value];
		}

		uasort($priority, function($a, $b)
		{
			return $a[1] === $b[1] ? ($a[0] > $b[0] ? 1 : -1) : ($a[1] > $b[1] ? 1 : -1);
		});

		foreach($priority as $key => $value)
		{
			$priority[$key] = $value[1];
		}

		// Return sorted middleware list

		return array_merge($priority, $middleware);
	}
1364559
public function withMiddleware($middleware = null)
    {
        if (is_null($middleware)) {
            unset($this->app['middleware.disable']);

            return $this;
        }

        foreach ((array) $middleware as $abstract) {
            unset($this->app[$abstract]);
        }

        return $this;
    }
928855
private function createActionMiddleware($action, array $params){
        $middlewares    = [];

        $middlewares[]  = $this->container->get("core.middleware.action")
            ->setAction($action)
            ->setParams($params)
        ;

        return $middlewares;
    }
1219920
protected function parseAction($action)
    {
        if (\is_string($action)) {
            return ['uses' => $action];
        } elseif (! \is_array($action)) {
            return [$action];
        }

        if (isset($action['middleware']) && \is_string($action['middleware'])) {
            $action['middleware'] = \explode('|', $action['middleware']);
        }

        return $action;
    }
143159
private function composeMiddleware(array $middlewares)
  {
    $curryedMiddlewares = array_map(function($middleware) {
      return function (\Closure $next) use ($middleware) {
        return function (MessageInterface $requestOrResponse) use ($middleware, $next) {
          return call_user_func($middleware, $next, $requestOrResponse);
        };
      };
    }, array_reverse($middlewares));

    return array_reduce($curryedMiddlewares, function($state, $middleware){
      return function(MessageInterface $requestOrResponse) use ($state, $middleware) {
        return $middleware($state)($requestOrResponse);
      };
    }, function($requestOrResponse) {
      return $requestOrResponse;
    });
  }
1115895
protected function moveMiddleware($middlewares, $from, $to)
    {
        array_splice($middlewares, $to, 0, $middlewares[$from]);

        unset($middlewares[$from + 1]);

        return $middlewares;
    }
1098161
public function middleware(...$middlewares) 
	{
		foreach ($middlewares as $middleware) {
			$this->currentContext->addMiddleware($middleware);
		}

		return $this;
	}
1281242
public function prepend($middleware)
    {
        if (is_array($middleware)) {
            $this->queue = array_merge($middleware, $this->queue);

            return $this;
        }
        array_unshift($this->queue, $middleware);

        return $this;
    }
431654
public function sortMiddleware( $middleware ) {
		$sorted = $middleware;

		usort( $sorted, function ( $a, $b ) use ( $middleware ) {
			$a_priority = $this->getMiddlewarePriorityForMiddleware( $a );
			$b_priority = $this->getMiddlewarePriorityForMiddleware( $b );
			$priority = $b_priority - $a_priority;

			if ( $priority !== 0 ) {
				return $priority;
			}

			// Keep relative order from original array.
			return array_search( $a, $middleware ) - array_search( $b, $middleware );
		} );

		return array_values( $sorted );
	}
474129
public function append(MiddlewareInterface $middleware): MiddlewarePipe
    {
        $stack = new MiddlewarePipe();

        /** @noinspection PhpParamsInspection */
        return $stack
            ->append($this)
            ->append($middleware);
    }