<commit_before><?php
namespace Exedra\Support\Psr7;

use Exedra\Exception\InvalidArgumentException;
use Exedra\Exception\NotFoundException;
use Exedra\Http\Stream;
use Exedra\Runtime\Context;
use Psr\Http\Message\ResponseInterface;

class BridgeMiddleware
{
    /**
     * Psr7 middlewares
     * @var array|callable[]
     */
    private $middlewares;

    public function __construct(array $middlewares)
    {
        $this->middlewares = $middlewares;
    }

    /**
     * @return mixed
     * @throws InvalidArgumentException
     */
    public function __invoke()
    {
        $args = func_get_args();

        $context = null;

        foreach($args as $arg)
            if(is_object($arg) && $arg instanceof Context)
                $context = $arg;

        if(!$context)
            throw new InvalidArgumentException('The previous middleware must at least pass a [Exedra\Runtime\Context]');

        if(!$context->hasRequest())
            throw new NotFoundException('Request is not available in this context.');

        $middlewares = $this->middlewares;

        $middlewares[] = function($request, $response) use($context, $args)
        {
            $contents = call_user_func_array($context->getNextCall(), $args);

            return $context->getResponse()->setBody(Stream::createFromContents($contents));
        };

        reset($middlewares);

        $first = current($middlewares);

        // expect a ResponseInterface
        $response = $first($context->getRequest(), $context->getResponse(), next($middlewares));

        return $response;
    }
}<commit_msg>Psr7 bridge middleware update<commit_after><?php
namespace Exedra\Support\Psr7;

use Exedra\Exception\InvalidArgumentException;
use Exedra\Exception\NotFoundException;
use Exedra\Http\Stream;
use Exedra\Runtime\Context;
use Psr\Http\Message\ResponseInterface;

class BridgeMiddleware
{
    /**
     * Psr7 middlewares
     * @var array|callable[]
     */
    private $middlewares;

    public function __construct(array $middlewares)
    {
        $this->middlewares = $middlewares;
    }

    /**
     * @return mixed
     * @throws InvalidArgumentException
     */
    public function __invoke()
    {
        $args = func_get_args();

        $context = null;

        foreach($args as $arg)
            if(is_object($arg) && $arg instanceof Context)
                $context = $arg;

        if(!$context)
            throw new InvalidArgumentException('The previous middleware must at least pass a [Exedra\Runtime\Context]');

        if(!$context->hasRequest())
            throw new NotFoundException('Request is not available in this context.');

        $context->setMutables(array(
            'request', 'response'
        ));

        $middlewares = $this->middlewares;

        // the last middleware
        $middlewares[] = function(ServerRequest $request, $response) use($context, $args)
        {
            $context->response = $response;

            // mutate the request object
            $context->request = $request;

            $contents = call_user_func_array($context->getNextCall(), $args);

            return $context->getResponse()->setBody(Stream::createFromContents($contents));
        };

        reset($middlewares);

        $next = function($request, $response) use(&$middlewares, &$next)
        {
            $call = next($middlewares);

            return $call($request, $response, $next);
        };

        $first = function($request, $response) use(&$middlewares, $next)
        {
            reset($middlewares);

            $call = current($middlewares);

            return $call($request, $response, $next);
        };

        // expect a ResponseInterface
        $response = $first($context->getRequest(), $context->getResponse(), $next);

        return $response;
    }
}
{'fraction_non_alphanumeric': 0.1271918345982727, 'fraction_numerical': 0.0013085579691180318, 'mean_word_length': 2.5064220183486237, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5437133', 'n_tokens_mistral': 1077, 'n_tokens_neox': 1039, 'n_words': 225}