@param \Closure $task
@param null     $middlewares

@return mixed
509673
public function dispatch(\Closure $task, $middlewares = null)
    {
        is_array($middlewares) || $middlewares = $this->getMiddlewares();

        $beforeReturnValue = $this->before($middlewares);

        if ($beforeReturnValue === false || $beforeReturnValue !== true) {
            return $beforeReturnValue;
        }

        $this->setPayload($task());

        $afterReturnValue = $this->after(array_reverse($middlewares));

        if ($afterReturnValue === false || $afterReturnValue !== true) {
            return $afterReturnValue;
        }

        return $this->getPayload();
    }
1199517
function(middlewares, response, callback) {
        var that = this;
        var middleware = middlewares.shift();
        if (!middleware) {
            return callback(null, response);
        }
        var middleware_callback = function(retVal) {
            /**
             * got an error
             * break middleware chain and callback
             */
            if (retVal && retVal instanceof Error) {
                return callback(retVal, response);
            }
            /**
             * got a response object
             * break middleware chain and bypass request
             */
            if (retVal && typeof retVal == 'object') {
                return callback(null, retVal);
            }
            that._callResponseMiddlewares(middlewares, response, callback);
        };
        try {
            middleware(response, middleware_callback);
        } catch (e) {
            callback(e, response);
        }
    }
1170526
protected function runTimedTask(Closure $task)
    {
        $timer = new Stopwatch();
        $timer->start('subtask');

        $task();

        return $timer->stop('subtask')->getDuration();
    }
588876
public function get($uri, $handler, $middlewares = array())
    {
        return $this->add($this->route('GET', $uri, $handler, $middlewares));
    }
617438
protected function sendThroughPipeline(array $middleware, \Closure $then)
    {
        if (count($middleware) > 0) {
            return (new Pipeline($this))
                ->send($this)
                ->through($middleware)
                ->then($then);
        }

        return $then();
    }
1030732
public function use($middlewares) {
		$middlewares = (array)$middlewares;

		if ($this->namespace) {
			foreach ($middlewares as &$middleware) {
				$middleware = $this->namespace . $middleware;
			}
		}

		$this->middlewares = array_merge($this->middlewares, (array)$middlewares);

		return $this;
	}
1131908
public function connectMiddlewares($controller, $method, MiddleWares $middleWares, $decorators = [])
    {
        $isClosure = is_callable($controller);
        if ($middleWares->valid()) {
            if ($isClosure) {
                $next = $this->getInvokeMiddlewareNext($controller, $middleWares, $decorators);
            } else {
                $next = $this->getControllerMiddlewareNext($controller, $method, $middleWares, $decorators);
            }
            $middleWares->withNextArgs($next);
            $response = $this->injectionClass($middleWares->current(), 'handle', $middleWares->getNextArgs());
        } else {
            if ($isClosure) {
                $controller = $this->closureBinder($controller);
                $response = $this->injectionClosure($controller, $decorators);
            } else {
                $response = $this->injectionClass($controller, $method, [], $decorators);
            }
        }

        if ($response instanceof Response) {
            $this->sendByResponse($response);
        }
    }
522256
protected function executeMiddlewares($middlewares){
        //Analizo los filtros y los aplico en caso de que corresponda
        $middlewaresDefinition = $this->app->context->getMiddlewaresDefinition();
        foreach ($middlewares as $middlewareName) {
            if (! isset($middlewaresDefinition[$middlewareName])) {
                Error::general_error('Middleware Error', 'The middleware ' . $middlewareName . ' dont exists');
            }
            
            $middleware = $middlewaresDefinition[$middlewareName];
            
            $dir = $this->buildDir($middleware, 'middlewares');
            $class = $this->buildClass($middleware);
            if(!class_exists($class)){
                //Si la clase no existe intento cargarla
                if(file_exists($dir)){
                    require_once $dir;
                }else{
                    //Avisa que el archivo no existe
                    Error::general_error('Middleware Error', 'The middleware ' . $middleware['class'] . ' dont exists');
                } 
            }
            $middlewareIns= new $class();
            //Analizo si hay parametros en la configuracion
            if(isset($middleware['properties'])){
                $this->app->dependenciesEngine->injectProperties($middlewareIns, $middleware['properties']);
            }
            //Analiza si existe el metodo filtrar
            if(method_exists($middlewareIns, 'handle')){
                $rta= $middlewareIns->handle($this->httpRequest, $this->httpResponse);
                if($rta === false){
                    return false;
                }
            }
            else{
                Error::general_error('Middleware Error', 'The middleware ' . $middleware['class'] . ' dont implement the method handle()');
            }
        }
    }
442349
public static function resolve($name, $map, $middlewareGroups)
    {
        // When the middleware is simply a Closure, we will return this Closure instance
        // directly so that Closures can be registered as middleware inline, which is
        // convenient on occasions when the developers are experimenting with them.
        if ($name instanceof Closure) {
            return $name;
        }

        if (isset($map[$name]) && $map[$name] instanceof Closure) {
            return $map[$name];
        }

        // If the middleware is the name of a middleware group, we will return the array
        // of middlewares that belong to the group. This allows developers to group a
        // set of middleware under single keys that can be conveniently referenced.
        if (isset($middlewareGroups[$name])) {
            return static::parseMiddlewareGroup($name, $map, $middlewareGroups);
        }

        // Finally, when the middleware is simply a string mapped to a class name the
        // middleware name will get parsed into the full class name and parameters
        // which may be run using the Pipeline which accepts this string format.
        [$name, $parameters] = array_pad(explode(':', $name, 2), 2, null);

        return ($map[$name] ?? $name).(! is_null($parameters) ? ':'.$parameters : '');
    }
928855
private function createActionMiddleware($action, array $params){
        $middlewares    = [];

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

        return $middlewares;
    }
709352
protected function gatherInvokerMiddleware($middleware)
    {
        $nameMiddleware = $this->config['name_middleware'] ?? [];
        $middlewareGroups = $this->config['middleware_groups'] ?? [];

        return Collection::make($middleware)->map(function ($middleware) use ($nameMiddleware, $middlewareGroups) {
            return (array) MiddlewareNameResolver::resolve($middleware, $nameMiddleware, $middlewareGroups);
        })->flatten()->all();
    }
1148555
public function peel(object $object, array $parameters = [], array $middlewareParameters = [])
	{
		$next = $this->buildCoreClosure($object);

		foreach($this->layers as $layer)
		{
			$middleware = $this->middlewareFactory($layer, $middlewareParameters);

			$next = $this->buildLayerClosure($middleware, $next);
		}

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

		return $this;
	}
1040215
public function handle($request, Closure $next)
    {
        $this->kernel->setClosure($next);

        return $this->middleware->handle($request);
    }
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;
    });
  }
410256
public static function factory(array $middlewares = array())
    {
        $runner = null;
        foreach ($middlewares as $middleware) {
            $runner = $runner ? $runner->add($middleware) : new static($middleware);
        }
        return $runner;
    }
423769
public function middleware($middleware = null)
    {
        if (is_null($middleware)) {
            return $this->middleware;
        }

        if (is_string($middleware)) {
            $middleware = func_get_args();
        }

        $this->middleware = array_unique(array_merge($this->middleware, $middleware));

        return $this;
    }