<commit_before><?php
namespace ManaPHP\Rest;

use ManaPHP\Http\Response;
use ManaPHP\Router\NotFoundRouteException;

/**
 * Class ManaPHP\Rest\Swoole
 *
 * @package application
 * @property-read \ManaPHP\Http\RequestInterface       $request
 * @property-read \ManaPHP\Http\ResponseInterface      $response
 * @property-read \ManaPHP\RouterInterface             $router
 * @property-read \ManaPHP\DispatcherInterface         $dispatcher
 * @property-read \ManaPHP\Http\SessionInterface       $session
 * @property-read \ManaPHP\Swoole\Http\ServerInterface $swooleHttpServer
 */
class Swoole extends \ManaPHP\Application
{
    public function getDi()
    {
        if (!$this->_di) {
            $this->_di = new Factory();
            $this->_di->keepInstanceState(true);
            $this->_di->setShared('swooleHttpServer', 'ManaPHP\Swoole\Http\Server');
        }

        return $this->_di;
    }

    public function send()
    {
        $swoole = $this->swooleHttpServer;
        $response = $this->response;

        if (isset($_SERVER['HTTP_X_REQUEST_ID']) && !$response->hasHeader('X-Request-Id')) {
            $response->setHeader('X-Request-Id', $_SERVER['HTTP_X_REQUEST_ID']);
        }

        $response->setHeader('X-Response-Time', sprintf('%.3f', microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']));

        $this->eventsManager->fireEvent('response:beforeSend', $response);

        $swoole->setStatus($response->getStatusCode());
        $swoole->sendHeaders($response->getHeaders());

        if ($file = $response->getFile()) {
            $swoole->sendFile($file);
        } else {
            $swoole->sendContent($response->getContent());
        }

        $this->eventsManager->fireEvent('response:afterSend', $response);
    }

    public function handle()
    {
        try {
            $request_uri = $_SERVER['REQUEST_URI'];
            $_GET['_url'] = ($pos = strpos($request_uri, '?')) ? substr($request_uri, 0, $pos) : $request_uri;

            $this->fireEvent('app:beginRequest');

            $this->authenticate();

            if (!$this->router->match()) {
                throw new NotFoundRouteException(['router does not have matched route for `:uri`', 'uri' => $this->router->getRewriteUri()]);
            }

            $this->dispatcher->dispatch($this->router);
            $actionReturnValue = $this->dispatcher->getReturnedValue();
            if ($actionReturnValue !== null && !$actionReturnValue instanceof Response) {
                $this->response->setJsonContent($actionReturnValue);
            }
        } catch (\Exception $exception) {
            $this->handleException($exception);
        } catch (\Error $error) {
            $this->handleException($error);
        }

        $this->send();

        $this->fireEvent('app:endRequest');

        $this->_di->restoreInstancesState();
    }

    public function main()
    {
        $this->dotenv->load();
        $this->configure->load();

        $this->registerServices();

        $this->fireEvent('app:start');

        $this->swooleHttpServer->start([$this, 'handle']);<commit_msg>Modified   ManaPHP/Rest/Swoole.php<commit_after><?php
namespace ManaPHP\Rest;

use ManaPHP\Http\Response;
use ManaPHP\Router\NotFoundRouteException;

/**
 * Class ManaPHP\Rest\Swoole
 *
 * @package application
 * @property-read \ManaPHP\Http\RequestInterface       $request
 * @property-read \ManaPHP\Http\ResponseInterface      $response
 * @property-read \ManaPHP\RouterInterface             $router
 * @property-read \ManaPHP\DispatcherInterface         $dispatcher
 * @property-read \ManaPHP\Http\SessionInterface       $session
 * @property-read \ManaPHP\Swoole\Http\ServerInterface $swooleHttpServer
 */
class Swoole extends \ManaPHP\Application
{
    public function getDi()
    {
        if (!$this->_di) {
            $this->_di = new Factory();
            $this->_di->keepInstanceState(true);
            $this->_di->setShared('swooleHttpServer', 'ManaPHP\Swoole\Http\Server');
        }

        return $this->_di;
    }

    public function send()
    {
        $swoole = $this->swooleHttpServer;
        $response = $this->response;

        if (isset($_SERVER['HTTP_X_REQUEST_ID']) && !$response->hasHeader('X-Request-Id')) {
            $response->setHeader('X-Request-Id', $_SERVER['HTTP_X_REQUEST_ID']);
        }

        $response->setHeader('X-Response-Time', sprintf('%.3f', microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']));

        $this->eventsManager->fireEvent('response:beforeSend', $response);

        $swoole->setStatus($response->getStatusCode());
        $swoole->sendHeaders($response->getHeaders());

        if ($file = $response->getFile()) {
            $swoole->sendFile($file);
        } else {
            $swoole->sendContent($response->getContent());
        }

        $this->eventsManager->fireEvent('response:afterSend', $response);
    }

    public function handle()
    {
        try {
            $request_uri = $_SERVER['REQUEST_URI'];
            $_GET['_url'] = ($pos = strpos($request_uri, '?')) ? substr($request_uri, 0, $pos) : $request_uri;

            $this->eventsManager->fireEvent('app:beginRequest', $this);

            $this->authenticate();

            if (!$this->router->match()) {
                throw new NotFoundRouteException(['router does not have matched route for `:uri`', 'uri' => $this->router->getRewriteUri()]);
            }

            $this->dispatcher->dispatch($this->router);
            $actionReturnValue = $this->dispatcher->getReturnedValue();
            if ($actionReturnValue !== null && !$actionReturnValue instanceof Response) {
                $this->response->setJsonContent($actionReturnValue);
            }
        } catch (\Exception $exception) {
            $this->handleException($exception);
        } catch (\Error $error) {
            $this->handleException($error);
        }

        $this->send();

        $this->eventsManager->fireEvent('app:endRequest', $this);

        $this->_di->restoreInstancesState();
    }

    public function main()
    {
        $this->dotenv->load();
        $this->configure->load();

        $this->registerServices();

        $this->fireEvent('app:start');

        $this->swooleHttpServer->start([$this, 'handle']);
{'fraction_non_alphanumeric': 0.16259773416307644, 'fraction_numerical': 0.0006382639221318015, 'mean_word_length': 2.6210283073367995, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 109, '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': '1743080', 'n_tokens_mistral': 1829, 'n_tokens_neox': 1782, 'n_words': 268}