/** * @param ObjectStream $stream * @return \React\Promise\Promise */ public static function promise(ObjectStream $stream) { $deferred = new Deferred(); $list = new \SplObjectStorage(); $stream->on('data', function ($object) use($list) { $list->attach($object); }); $stream->on('end', function () use($deferred, $list) { $deferred->resolve($list); }); return $deferred->promise(); }
public function testObjectStream() { $node = $this->getMock('React\\Filesystem\\Node\\NodeInterface'); $stream = new ObjectStream(); $this->assertTrue($stream->isWritable()); $this->assertTrue($stream->isReadable()); $stream->on('data', function (NodeInterface $data) use($node) { $this->assertEquals($node, $data); }); $stream->end($node); $this->assertFalse($stream->isWritable()); $this->assertFalse($stream->isReadable()); $stream->close(); $this->assertFalse($stream->isWritable()); $this->assertFalse($stream->isReadable()); }
protected function processLsContents($result, ObjectStream $stream) { $promises = []; foreach ($result as $entry) { $node = ['path' => $entry['path'], 'type' => $entry['type']]; $promises[] = \React\Filesystem\detectType($this->typeDetectors, $node)->then(function (NodeInterface $node) use($stream) { $stream->write($node); return new FulfilledPromise(); }); } \React\Promise\all($promises)->then(function () use($stream) { $stream->close(); }); }
/** * @param array $node * @param ObjectStream $stream * @return PromiseInterface */ protected function processLsDent(array $node, ObjectStream $stream) { $promiseChain = new RejectedPromise(); foreach ($this->typeDetectors as $detector) { $promiseChain = $promiseChain->otherwise(function () use($node, $detector) { return $detector->detect($node); }); } return $promiseChain->then(function ($callable) use($node, $stream) { $stream->emit('data', [$callable($node['path'])]); }); }
/** * @param FileInterface $node * @return ObjectStream */ protected function copyToFile(FileInterface $node) { $stream = new ObjectStream(); $this->open('r')->then(function (ReadableStreamInterface $readStream) use($node, $stream) { $node->open('ctw')->then(function (WritableStreamInterface $writeStream) use($readStream, $node, $stream) { $readStream->pipe($writeStream)->on('end', function () use($stream, $node) { $stream->end($node); }); }); }); return $stream; }
/** * @param $sourceStream * @return Stream */ protected function processLsRecursiveContents($sourceStream) { $stream = new ObjectStream(); $closeCount = 0; $sourceStream->on('data', function (NodeInterface $node) use(&$closeCount, $stream) { if ($node instanceof Directory || $node instanceof File) { $stream->emit('data', [$node]); } if ($node instanceof Directory) { $this->streamLsIntoStream($node, $stream, $closeCount); } }); $sourceStream->on('end', function () use(&$closeCount, $stream) { $this->adapter->getLoop()->addPeriodicTimer(0.01, function (TimerInterface $timer) use(&$closeCount, $stream) { if ($closeCount === 0) { $timer->cancel(); $stream->close(); } }); }); return $stream; }