private function addMiddleware($middleware, $args, $push = true)
    {
        $instance = $this->resolve($middleware);

        $types = $this->getTypes($instance);

        if (!count($types)) {
            throw new InvalidArgumentException('The first argument must be an instance of PrepareableInterface, RespondableInterface or CloseableInterface.');
        }

        //Replace middleware definition with instance
        $args[0] = $instance;

        foreach ($types as $type) {
            if ($push) {
                array_push($this->{$type . 'Middlewares'}, $args);
            } else {
                array_unshift($this->{$type . 'Middlewares'}, $args);
            }
        }

    }