React\Stream\Stream::handleData PHP Method

handleData() public method

public handleData ( $stream )
    public function handleData($stream)
    {
        $error = null;
        set_error_handler(function ($errno, $errstr, $errfile, $errline) use(&$error) {
            $error = new \ErrorException($errstr, 0, $errno, $errfile, $errline);
        });
        $data = stream_get_contents($stream, $this->bufferSize === null ? -1 : $this->bufferSize);
        restore_error_handler();
        if ($error !== null) {
            $this->emit('error', array(new \RuntimeException('Unable to read from stream: ' . $error->getMessage(), 0, $error), $this));
            $this->close();
            return;
        }
        if ($data !== '') {
            $this->emit('data', array($data, $this));
        }
        if (!is_resource($stream) || feof($stream)) {
            $this->end();
        }
    }

Usage Example

Example #1
0
 /**
  * @covers React\Stream\Stream::handleData
  */
 public function testDataErrorShouldEmitErrorAndClose()
 {
     $stream = fopen('php://temp', 'r+');
     // add a filter which returns an error when encountering an 'a' when reading
     Filter\append($stream, function ($chunk) {
         if (strpos($chunk, 'a') !== false) {
             throw new \Exception('Invalid');
         }
         return $chunk;
     }, STREAM_FILTER_READ);
     $loop = $this->createLoopMock();
     $conn = new Stream($stream, $loop);
     $conn->on('data', $this->expectCallableNever());
     $conn->on('error', $this->expectCallableOnce());
     $conn->on('close', $this->expectCallableOnce());
     fwrite($stream, "foobar\n");
     rewind($stream);
     $conn->handleData($stream);
 }
All Usage Examples Of React\Stream\Stream::handleData