Kraken\Stream\StreamWriter::write PHP Method

write() public method

public write ( $text )
    public function write($text)
    {
        if (!$this->writable) {
            return $this->throwAndEmitException(new WriteException('Stream is no longer writable.'));
        }
        $sent = fwrite($this->resource, $text);
        if ($sent === false) {
            return $this->throwAndEmitException(new WriteException('Error occurred while writing to the stream resource.'));
        }
        $this->emit('drain', [$this]);
        return true;
    }

Usage Example

Beispiel #1
0
 public function testStreamReader_StreamWriter_WriteAndReadDataScenario()
 {
     $local = $this->basePath();
     $writer = new StreamWriter(fopen("file://{$local}/temp", 'w+'));
     $reader = new StreamReader(fopen("file://{$local}/temp", 'r+'));
     $expectedData = "qwertyuiop\n";
     $capturedData = null;
     $readData = null;
     $reader->on('data', function ($origin, $data) use(&$capturedData) {
         $capturedData = $data;
     });
     $reader->on('drain', $this->expectCallableNever());
     $reader->on('error', $this->expectCallableNever());
     $reader->on('close', $this->expectCallableOnce());
     $writer->on('data', $this->expectCallableNever());
     $writer->on('drain', $this->expectCallableOnce());
     $writer->on('error', $this->expectCallableNever());
     $writer->on('close', $this->expectCallableOnce());
     $writer->write($expectedData);
     $readData = $reader->read();
     $writer->close();
     $reader->close();
     $this->assertEquals($expectedData, $readData);
     $this->assertEquals($expectedData, $capturedData);
 }