M6Web\Bundle\PhpProcessManagerBundle\Bridge\HttpKernel::onRequest PHP Method

onRequest() public method

Handle a request
public onRequest ( React\Http\Request $request, React\Http\Response $response )
$request React\Http\Request
$response React\Http\Response
    public function onRequest(ReactRequest $request, ReactResponse $response)
    {
        $content = '';
        $headers = $request->getHeaders();
        $contentLength = isset($headers['Content-Length']) ? (int) $headers['Content-Length'] : 0;
        $request->on('data', function ($data) use($request, $response, &$content, $contentLength) {
            // Read data (may be empty for GET request)
            $content .= $data;
            // Handle request after receive
            if (strlen($content) >= $contentLength) {
                $symfonyRequest = static::mapRequest($request, $content);
                try {
                    // Execute
                    $symfonyResponse = $this->application->handle($symfonyRequest);
                } catch (\Throwable $t) {
                    // Executed only in PHP 7, will not match in PHP 5.x
                    $this->fatalError($response, $t);
                    return;
                } catch (\Exception $e) {
                    // Executed only in PHP 5.x, will not be reached in PHP 7
                    $this->fatalError($response, $e);
                    return;
                }
                static::mapResponse($response, $symfonyResponse);
                if ($this->application instanceof SymfonyHttpKernel\TerminableInterface) {
                    $this->application->terminate($symfonyRequest, $symfonyResponse);
                }
            }
        });
    }

Usage Example

 public function testOnRequest()
 {
     $this->if($application = $this->getApplicationMock())->and($reactConInterface = new \mock\React\Socket\ConnectionInterface())->and($testedClass = new TestedClass($application))->and($request = new \React\Http\Request('GET', '/', array(), '1.1', ['Expect' => '100-continue']))->and($response = new \React\Http\Response($reactConInterface))->and($testedClass->onRequest($request, $response))->and($request->emit('data', ['Response of the Ultimate Question of Life, the Universe, and Everything']))->then->mock($application)->call('handle')->once()->mock($reactConInterface)->call('write')->withArguments("HTTP/1.1 200 OK\r\nX-Powered-By: React/alpha\r\ncache-control: no-cache\r\nTransfer-Encoding: chunked\r\n\r\n")->once()->call('write')->withArguments("10\r\nthe answer is 42\r\n")->once()->call('write')->withArguments("0\r\n\r\n")->once();
 }