diff --git a/src/AdapterInterface.php b/src/AdapterInterface.php index 9063474f..9d94496d 100644 --- a/src/AdapterInterface.php +++ b/src/AdapterInterface.php @@ -2,6 +2,7 @@ namespace React\Filesystem; +use React\Filesystem\ObjectStream; use React\EventLoop\LoopInterface; use React\Promise\PromiseInterface; @@ -111,6 +112,14 @@ public function stat($filename); */ public function ls($path); + /** + * List contents of the given path. + * + * @param string $path + * @return ObjectStream + */ + public function lsStream($path); + /** * Touch the given path, either creating a file, or updating mtime on the file. * diff --git a/src/ChildProcess/Adapter.php b/src/ChildProcess/Adapter.php index 76f67335..be0ee398 100644 --- a/src/ChildProcess/Adapter.php +++ b/src/ChildProcess/Adapter.php @@ -6,6 +6,7 @@ use Exception; use React\EventLoop\LoopInterface; use React\Filesystem\ObjectStream; +use React\Filesystem\ObjectStreamSink; use React\Filesystem\AdapterInterface; use React\Filesystem\CallInvokerInterface; use React\Filesystem\FilesystemInterface; @@ -322,6 +323,15 @@ public function stat($filename) * @return PromiseInterface */ public function ls($path) + { + return ObjectStreamSink::promise($this->lsStream($path)); + } + + /** + * @param string $path + * @return ObjectStream + */ + public function lsStream($path) { $stream = new ObjectStream(); diff --git a/src/Eio/Adapter.php b/src/Eio/Adapter.php index 1f7b2ca3..361224ec 100644 --- a/src/Eio/Adapter.php +++ b/src/Eio/Adapter.php @@ -10,10 +10,12 @@ use React\Filesystem\ModeTypeDetector; use React\Filesystem\Node\NodeInterface; use React\Filesystem\ObjectStream; +use React\Filesystem\ObjectStreamSink; use React\Filesystem\OpenFileLimiter; use React\Filesystem\PermissionFlagResolver; use React\Filesystem\TypeDetectorInterface; use React\Promise\Deferred; +use React\Promise\PromiseInterface; class Adapter implements AdapterInterface { @@ -191,9 +193,18 @@ public function chown($path, $uid, $gid) } /** - * {@inheritDoc} + * @param string $path + * @return PromiseInterface */ public function ls($path) + { + return ObjectStreamSink::promise($this->lsStream($path)); + } + + /** + * {@inheritDoc} + */ + public function lsStream($path) { $stream = new ObjectStream(); diff --git a/src/Node/Directory.php b/src/Node/Directory.php index 4e7d3b7b..3556254a 100644 --- a/src/Node/Directory.php +++ b/src/Node/Directory.php @@ -69,7 +69,7 @@ public function __construct($path, FilesystemInterface $filesystem, RecursiveInv */ public function ls() { - return ObjectStreamSink::promise($this->lsStreaming()); + return $this->adapter->ls($this->path); } /** @@ -77,7 +77,7 @@ public function ls() */ public function lsStreaming() { - return $this->adapter->ls($this->path); + return $this->adapter->lsStream($this->path); } /** diff --git a/tests/ChildProcess/AdapterTest.php b/tests/ChildProcess/AdapterTest.php index 8fbbf83d..3835a1d5 100644 --- a/tests/ChildProcess/AdapterTest.php +++ b/tests/ChildProcess/AdapterTest.php @@ -5,6 +5,7 @@ use React\EventLoop\Factory; use React\Filesystem\ChildProcess\Adapter; use React\Filesystem\Filesystem; +use React\Filesystem\Node\File; use React\Filesystem\Node\NodeInterface; use React\Promise\Deferred; use React\Promise\FulfilledPromise; @@ -278,6 +279,52 @@ public function testSymlink() } public function testLs() + { + $loop = \React\EventLoop\Factory::create(); + $adapter = new Adapter($loop, [ + 'pool' => [ + 'class' => 'WyriHaximus\React\ChildProcess\Pool\Pool\Dummy', + ], + ]); + $invoker = $this->getMock('React\Filesystem\CallInvokerInterface', [ + '__construct', + 'invokeCall', + 'isEmpty', + ]); + $adapter->setInvoker($invoker); + $fs = Filesystem::createFromAdapter($adapter); + + $deferred = new Deferred(); + + $invoker + ->expects($this->once()) + ->method('invokeCall') + ->with( + 'readdir', + [ + 'path' => 'foo.bar', + 'flags' => 2, + ] + )->will($this->returnValue($deferred->promise())) + ; + + $promise = $adapter->ls('foo.bar'); + $this->assertInstanceOf('React\Promise\PromiseInterface', $promise); + + $deferred->resolve([ + [ + 'type' => 'file', + 'name' => 'bar.foo', + ], + ]); + + $nodes = $this->await($promise, $loop); + $nodes->rewind(); + + $this->assertEquals(new File('foo.bar/bar.foo', $fs), $nodes->current()); + } + + public function testLsStream() { $loop = $this->getMock('React\EventLoop\LoopInterface'); $adapter = new Adapter($loop, [ @@ -307,7 +354,7 @@ public function testLs() )->will($this->returnValue($deferred->promise())) ; - $stream = $adapter->ls('foo.bar'); + $stream = $adapter->lsStream('foo.bar'); $this->assertInstanceOf('React\Filesystem\ObjectStream', $stream); $calledOnData = false; diff --git a/tests/Node/DirectoryTest.php b/tests/Node/DirectoryTest.php index e5634f27..34a8c8a4 100644 --- a/tests/Node/DirectoryTest.php +++ b/tests/Node/DirectoryTest.php @@ -45,13 +45,13 @@ public function testLs() $filesystem = $this->mockAdapter($loop); - $lsStream = $this->getMock('React\Filesystem\ObjectStream'); + $ls = \React\Promise\resolve(); $filesystem ->expects($this->once()) ->method('ls') ->with() - ->will($this->returnValue($lsStream)) + ->will($this->returnValue($ls)) ; $directory = new Directory($path, Filesystem::createFromAdapter($filesystem)); @@ -59,6 +59,27 @@ public function testLs() $this->assertInstanceOf('React\Promise\PromiseInterface', $directory->ls()); } + public function testLsStream() + { + $path = '/home/foo/bar'; + $loop = $this->getMock('React\EventLoop\LoopInterface'); + + $filesystem = $this->mockAdapter($loop); + + $lsStream = $this->getMock('React\Filesystem\ObjectStream'); + + $filesystem + ->expects($this->once()) + ->method('lsStream') + ->with() + ->will($this->returnValue($lsStream)) + ; + + $directory = new Directory($path, Filesystem::createFromAdapter($filesystem)); + + $this->assertInstanceOf('React\Filesystem\ObjectStream', $directory->lsStreaming()); + } + public function testCreate() { $path = 'foo.bar'; diff --git a/tests/TestCase.php b/tests/TestCase.php index 4b62c0b5..5bfb711c 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -35,6 +35,7 @@ protected function mockAdapter(LoopInterface $loop = null) 'chown', 'stat', 'ls', + 'lsStream', 'touch', 'open', 'read', diff --git a/tests/UnknownNodeType.php b/tests/UnknownNodeType.php index 60912256..b62eacce 100644 --- a/tests/UnknownNodeType.php +++ b/tests/UnknownNodeType.php @@ -3,7 +3,7 @@ namespace React\Tests\Filesystem; use React\Filesystem\Node\NodeInterface; -use React\Filesystem\Node\ObjectStream; +use React\Filesystem\ObjectStream; class UnknownNodeType implements NodeInterface {