<commit_before><commit_msg>add the reactive silex app<commit_after><?php

namespace Kpacha\ReactiveSilex;

use React\Http\Request;
use React\Http\Response;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
use Symfony\Component\HttpKernel\HttpKernelInterface;

class ReactiveApplication extends Application
{

    public function __invoke(Request $request, Response $response)
    {
        $sfRequest = $this->buildSymfonyRequest($request, $response);
        $sfResponse = $this->getHandledResponse($sfRequest);
        $this->parseSymfonyResponse($response, $sfResponse);
    }

    private function buildSymfonyRequest(Request $request)
    {
        return SymfonyRequest::create($request->getPath(), $request->getMethod());
    }

    private function getHandledResponse(SymfonyRequest $sfRequest)
    {
        $sfResponse = null;
        try {
            $sfResponse = $this->handle($sfRequest, HttpKernelInterface::MASTER_REQUEST, false);
            $this->terminate($sfRequest, $sfResponse);
        } catch (\Exception $e) {
            $sfResponse = new SymfonyResponse('We are sorry, but something went terribly wrong.',
                    404 /* ignored */, array('X-Status-Code' => 200));
        }
        return $sfResponse;
    }

    private function parseSymfonyResponse(Response $response, SymfonyResponse $sfResponse)
    {
        $response->writeHead($sfResponse->getStatusCode(), $this->extractResponseHeaders($sfResponse));
        $response->end($sfResponse->getContent());
    }

    private function extractResponseHeaders(SymfonyResponse $sfResponse)
    {
        $headers = array();
        foreach ($sfResponse->headers->allPreserveCase() as $name => $values) {
            foreach ($values as $value) {
                $headers[$name] = $value;
            }
        }
        return $headers;
    }

}

{'fraction_non_alphanumeric': 0.11734431291816778, 'fraction_numerical': 0.003088008234688626, 'mean_word_length': 3.235294117647059, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 19, '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': '7458222', 'n_tokens_mistral': 526, 'n_tokens_neox': 512, 'n_words': 107}