<commit_before><?php
/**
 * Spiral Framework.
 *
 * @license   MIT
 * @author    <NAME> (Wolfy-J)
 */
namespace Spiral\Http;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Spiral\Core\Component;
use Spiral\Core\ContainerInterface;
use Spiral\Debug\Traits\BenchmarkTrait;
use Spiral\Http\Exceptions\HttpException;
use Spiral\Http\Responses\Emitter;
use Spiral\Http\Traits\MiddlewaresTrait;
use Zend\Diactoros\Response;
use Zend\Diactoros\Response\EmitterInterface;
use Zend\Diactoros\Response\SapiEmitter;

/**
 * Magically simple implementation of PRS7 Http core.
 */
class HttpCore extends Component implements HttpInterface
{
    /**
     * HttpDispatcher has embedded router and log it's errors.
     */
    use BenchmarkTrait, MiddlewaresTrait;

    /**
     * @var EmitterInterface
     */
    private $emitter = null;

    /**
     * Dispatcher endpoint.
     *
     * @var string|callable|null
     */
    private $endpoint = null;

    /**
     * @invisible
     * @var ContainerInterface
     */
    protected $container = null;

    /**
     * @param ContainerInterface   $container
     * @param array                $middlewares
     * @param callable|null|string $endpoint
     */
    public function __construct(
        ContainerInterface $container,
        array $middlewares = [],
        callable $endpoint = null
    ) {
        $this->container = $container;
        $this->middlewares = $middlewares;

        $this->endpoint = $endpoint;
    }

    /**
     * @param EmitterInterface $emitter
     */
    public function setEmitter(EmitterInterface $emitter)
    {
        $this->emitter = $emitter;
    }

    /**
     * Set endpoint as callable function or invokable class name (will be resolved using container).
     *
     * @param callable $endpoint
     * @return $this
     */
    public function setEndpoint(callable $endpoint)
    {
        $this->endpoint = $endpoint;

        return $this;
    }

    /**
     * Pass request thought all http middlewares to appropriate endpoint. Default endpoint will be
     * used as fallback. Can thrown an exception happen in internal code.
     *
     * @param ServerRequestInterface $request
     * @param ResponseInterface      $response
     * @param callable               $endpoint User specified endpoint.
     * @return ResponseInterface
     * @throws HttpException
     */
    public function perform(
        ServerRequestInterface $request,
        ResponseInterface $response = null,
        callable $endpoint = null
    ) {
        $endpoint = !empty($endpoint) ? $endpoint : $this->endpoint();
        $response = !empty($response) ? $response : $this->response();

        if (empty($endpoint)) {
            throw new HttpException("Unable to execute request without destination endpoint.");
        }

        $pipeline = new MiddlewarePipeline($this->middlewares, $this->container);

        $benchmark = $this->benchmark('request', $request->getUri());
        try {
            //Exceptions (including client one) must be handled by pipeline
            return $pipeline->target($endpoint)->run($request, $response);
        } finally {
            $this->benchmark($benchmark);
        }
    }

    /**
     * Running spiral as middleware.
     *
     * @param ServerRequestInterface $request
     * @param ResponseInterface      $response
     * @return ResponseInterface
     * @throws HttpException
     */
    public function __invoke(
        ServerRequestInterface $request,
        ResponseInterface $response
    ) {
        return $this->perform($request, $response);
    }

    /**
     * Dispatch response to client.
     *
     * @param ResponseInterface $response
     * @return null Specifically.
     */<commit_msg>Update HttpCore.php<commit_after><?php
/**
 * Spiral Framework.
 *
 * @license   MIT
 * @author    <NAME> (Wolfy-J)
 */
namespace Spiral\Http;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Spiral\Core\Component;
use Spiral\Core\ContainerInterface;
use Spiral\Debug\Traits\BenchmarkTrait;
use Spiral\Http\Exceptions\HttpException;
use Spiral\Http\Responses\Emitter;
use Spiral\Http\Traits\MiddlewaresTrait;
use Zend\Diactoros\Response;
use Zend\Diactoros\Response\EmitterInterface;
use Zend\Diactoros\Response\SapiEmitter;

/**
 * Magically simple implementation of PRS7 Http core.
 */
class HttpCore extends Component implements HttpInterface
{
    /**
     * HttpDispatcher has embedded router and log it's errors.
     */
    use BenchmarkTrait, MiddlewaresTrait;

    /**
     * @var EmitterInterface
     */
    private $emitter = null;

    /**
     * Dispatcher endpoint.
     *
     * @var string|callable|null
     */
    private $endpoint = null;

    /**
     * @invisible
     * @var ContainerInterface
     */
    protected $container = null;

    /**
     * @param ContainerInterface   $container
     * @param array                $middlewares
     * @param callable|null|string $endpoint
     */
    public function __construct(
        ContainerInterface $container,
        array $middlewares = [],
        callable $endpoint = null
    ) {
        $this->container = $container;
        $this->middlewares = $middlewares;

        $this->endpoint = $endpoint;
    }

    /**
     * @param EmitterInterface $emitter
     */
    public function setEmitter(EmitterInterface $emitter)
    {
        $this->emitter = $emitter;
    }

    /**
     * Set endpoint as callable function or invokable class name (will be resolved using container).
     *
     * @param callable $endpoint
     * @return $this
     */
    public function setEndpoint(callable $endpoint)
    {
        $this->endpoint = $endpoint;

        return $this;
    }

    /**
     * Pass request thought all http middlewares to appropriate endpoint. Default endpoint will be
     * used as fallback. Can thrown an exception happen in internal code.
     *
     * @param ServerRequestInterface $request
     * @param ResponseInterface      $response
     * @param callable               $endpoint User specified endpoint.
     * @return ResponseInterface
     * @throws HttpException
     */
    public function perform(
        ServerRequestInterface $request,
        ResponseInterface $response = null,
        callable $endpoint = null
    ) {
        $endpoint = !empty($endpoint) ? $endpoint : $this->endpoint();
        $response = !empty($response) ? $response : $this->response();

        if (empty($endpoint)) {
            throw new HttpException("Unable to execute request without destination endpoint.");
        }

        $pipeline = new MiddlewarePipeline($this->middlewares, $this->container);

        $benchmark = $this->benchmark('request', $request->getUri());
        try {
            //Exceptions (including client one) must be handled by pipeline
            return $pipeline->target($endpoint)->run($request, $response);
        } finally {
            $this->benchmark($benchmark);
        }
    }

    /**
     * Running spiral as middleware.
     *
     * @todo add ability to call $next on NotFound exceptions
     * @param ServerRequestInterface $request
     * @param ResponseInterface      $response
     * @return ResponseInterface
     * @throws HttpException
     */
    public function __invoke(
        ServerRequestInterface $request,
        ResponseInterface $response
    ) {
        return $this->perform($request, $response);
    }

    /**
     * Dispatch response to client.
     *
     * @param ResponseInterface $response
     * @return null Specifically.
     */
{'fraction_non_alphanumeric': 0.10544307125953195, 'fraction_numerical': 0.00026295030239284776, 'mean_word_length': 2.464025500910747, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 35, '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': '3605319', 'n_tokens_mistral': 2079, 'n_tokens_neox': 2020, 'n_words': 532}