Skip to content

Commit ffb77d0

Browse files
author
Charlotte Dunois
committed
Move stream factory creation to node
1 parent f98d456 commit ffb77d0

File tree

7 files changed

+37
-85
lines changed

7 files changed

+37
-85
lines changed

src/AdapterInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
interface AdapterInterface
99
{
1010
const CREATION_MODE = 'rwxrw----';
11-
11+
1212
/**
1313
* Checks whether the current installation supports the adapter.
1414
*
15-
* @return boolean
15+
* @return bool
1616
*/
1717
public static function isSupported();
1818

@@ -127,7 +127,7 @@ public function touch($path, $mode = self::CREATION_MODE);
127127
* @param string $path
128128
* @param string $flags
129129
* @param $mode
130-
* @return PromiseInterface<file descriptor>
130+
* @return PromiseInterface
131131
*/
132132
public function open($path, $flags, $mode = self::CREATION_MODE);
133133

src/ChildProcess/Adapter.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use React\Filesystem\TypeDetectorInterface;
1616
use React\Filesystem\PermissionFlagResolver;
1717
use React\Filesystem\Node\NodeInterface;
18-
use React\Filesystem\Stream\StreamFactory;
1918
use React\Promise\PromiseInterface;
2019
use WyriHaximus\React\ChildProcess\Messenger\Messages\Factory;
2120
use WyriHaximus\React\ChildProcess\Messenger\Messages\Payload;
@@ -212,8 +211,8 @@ public function open($path, $flags, $mode = self::CREATION_MODE)
212211
'flags' => $flags,
213212
'mode' => $mode,
214213
]));
215-
})->then(function () use ($path, $flags, &$id) {
216-
return \React\Promise\resolve(StreamFactory::create($path, $id, $flags, $this));
214+
})->then(function () use (&$id) {
215+
return $id;
217216
});
218217
}
219218

src/Eio/Adapter.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use React\Filesystem\ObjectStream;
1313
use React\Filesystem\OpenFileLimiter;
1414
use React\Filesystem\PermissionFlagResolver;
15-
use React\Filesystem\Stream\StreamFactory;
1615
use React\Filesystem\TypeDetectorInterface;
1716
use React\Promise\Deferred;
1817

@@ -102,7 +101,7 @@ protected function applyConfiguration(array $options)
102101
}
103102

104103
/**
105-
* @return boolean
104+
* @return bool
106105
*/
107106
public static function isSupported()
108107
{
@@ -267,9 +266,7 @@ public function open($path, $flags, $mode = self::CREATION_MODE)
267266
$eioFlags,
268267
$mode,
269268
]);
270-
})->then(function ($fileDescriptor) use ($path, $flags) {
271-
return StreamFactory::create($path, $fileDescriptor, $flags, $this);
272-
}, function ($error) {
269+
})->otherwise(function ($error) {
273270
$this->openFileLimiter->close();
274271
return \React\Promise\reject($error);
275272
});

src/Node/Directory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public function lsRecursiveStreaming()
240240

241241
/**
242242
* @param $sourceStream
243-
* @return Stream
243+
* @return ObjectStream
244244
*/
245245
protected function processLsRecursiveContents($sourceStream)
246246
{

src/Node/File.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use React\Filesystem\FilesystemInterface;
88
use React\Filesystem\ObjectStream;
99
use React\Filesystem\ObjectStreamSink;
10-
use React\Filesystem\Stream\GenericStreamInterface;
10+
use React\Filesystem\Stream\StreamFactory;
1111
use React\Promise\Stream;
1212
use React\Stream\ReadableStreamInterface;
1313
use React\Stream\WritableStreamInterface;
@@ -118,10 +118,10 @@ public function open($flags, $mode = AdapterInterface::CREATION_MODE)
118118
return \React\Promise\reject(new Exception('File is already open'));
119119
}
120120

121-
return $this->adapter->open($this->path, $flags, $mode)->then(function (GenericStreamInterface $stream) {
121+
return $this->adapter->open($this->path, $flags, $mode)->then(function ($fd) use ($flags) {
122122
$this->open = true;
123-
$this->fileDescriptor = $stream->getFiledescriptor();
124-
return $stream;
123+
$this->fileDescriptor = $fd;
124+
return StreamFactory::create($this->path, $fd, $flags, $this->adapter);
125125
});
126126
}
127127

src/Stream/GenericStreamTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ public function getFiledescriptor()
4848
}
4949

5050
/**
51-
* @return boolean
51+
* @return bool
5252
*/
5353
public function isClosed()
5454
{
5555
return $this->closed;
5656
}
5757

5858
/**
59-
* @param boolean $closed
59+
* @param bool $closed
6060
*/
6161
public function setClosed($closed)
6262
{

tests/Node/FileTest.php

Lines changed: 23 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -235,23 +235,21 @@ public function testOpen()
235235
$path = 'foo.bar';
236236
$filesystem = $this->mockAdapter();
237237

238-
$stream = $this->getMock('React\Filesystem\Stream\GenericStreamInterface', [], ['foo:bar']);
238+
$fd = 'foo:bar';
239239
$flags = 'abc';
240240

241241
$filesystem
242242
->expects($this->once())
243243
->method('open')
244244
->with($path, $flags)
245-
->will($this->returnValue(new FulfilledPromise($stream)))
245+
->will($this->returnValue(new FulfilledPromise($fd)))
246246
;
247247

248-
$callbackFired = false;
249-
(new File($path, Filesystem::createFromAdapter($filesystem)))->open($flags)->then(function ($passStream) use (&$callbackFired, $stream) {
250-
$this->assertSame($stream, $passStream);
251-
$callbackFired = true;
252-
});
248+
$fs = Filesystem::createFromAdapter($filesystem);
249+
$pass = $this->await((new File($path, $fs))->open($flags), $fs->getAdapter()->getLoop());
253250

254-
$this->assertTrue($callbackFired);
251+
$this->assertInstanceOf('\React\Filesystem\Stream\GenericStreamInterface', $pass);
252+
$this->assertSame($fd, $pass->getFiledescriptor());
255253
}
256254

257255

@@ -260,14 +258,14 @@ public function testOpenTwice()
260258
$path = 'foo.bar';
261259
$filesystem = $this->mockAdapter();
262260

263-
$stream = $this->getMock('React\Filesystem\Stream\GenericStreamInterface', [], ['foo:bar']);
261+
$fd = 'foo:bar';
264262
$flags = 'abc';
265263

266264
$filesystem
267265
->expects($this->once())
268266
->method('open')
269267
->with($path, $flags)
270-
->will($this->returnValue(new FulfilledPromise($stream)))
268+
->will($this->returnValue(new FulfilledPromise($fd)))
271269
;
272270

273271
$file = new File($path, Filesystem::createFromAdapter($filesystem));
@@ -280,14 +278,10 @@ public function testGetContents()
280278
$path = 'foo.bar';
281279
$fd = '0123456789abcdef';
282280

283-
$openPromise = $this->getMock('React\Promise\PromiseInterface', [
284-
'then',
285-
]);
286-
287281
$filesystem = $this->mockAdapter();
288282

289283
$filesystem
290-
->expects($this->once())
284+
->expects($this->any())
291285
->method('stat')
292286
->with($path)
293287
->will($this->returnValue(new FulfilledPromise([
@@ -299,14 +293,14 @@ public function testGetContents()
299293
->expects($this->once())
300294
->method('open')
301295
->with($path, 'r')
302-
->will($this->returnValue($openPromise))
296+
->will($this->returnValue(new FulfilledPromise($fd)))
303297
;
304298

305299
$filesystem
306300
->expects($this->once())
307301
->method('read')
308302
->with($fd, 1, 0)
309-
->will($this->returnValue(new FulfilledPromise(str_repeat('a', 1))))
303+
->will($this->returnValue(new FulfilledPromise('a')))
310304
;
311305

312306
$filesystem
@@ -316,21 +310,6 @@ public function testGetContents()
316310
->will($this->returnValue(new FulfilledPromise()))
317311
;
318312

319-
$stream = new ReadableStream(
320-
$path,
321-
$fd,
322-
$filesystem
323-
);
324-
325-
$openPromise
326-
->expects($this->once())
327-
->method('then')
328-
->with($this->isType('callable'))
329-
->will($this->returnCallback(function ($resolveCb) use ($stream) {
330-
return new FulfilledPromise($resolveCb($stream));
331-
}))
332-
;
333-
334313
$getContentsPromise = (new File($path, Filesystem::createFromAdapter($filesystem)))->getContents();
335314
$this->assertInstanceOf('React\Promise\PromiseInterface', $getContentsPromise);
336315
}
@@ -341,30 +320,20 @@ public function testClose()
341320
$fd = '0123456789abcdef';
342321
$filesystem = $this->mockAdapter();
343322

344-
$stream = $this->getMock('React\Filesystem\Stream\GenericStreamInterface', [
345-
'getFiledescriptor',
346-
], [
347-
'foo:bar',
348-
]);
323+
$openPromise = new FulfilledPromise($fd);
349324

350-
$stream
351-
->expects($this->once())
352-
->method('getFiledescriptor')
353-
->with()
354-
->will($this->returnValue($fd))
325+
$filesystem
326+
->method('stat')
327+
->with($path)
328+
->will($this->returnValue(new FulfilledPromise([
329+
'size' => 1,
330+
])))
355331
;
356332

357-
$openPromise = $this->getMock('React\Promise\PromiseInterface', [
358-
'then',
359-
]);
360-
361-
$openPromise
362-
->expects($this->once())
363-
->method('then')
364-
->with($this->isType('callable'))
365-
->will($this->returnCallback(function ($resolveCb) use ($stream) {
366-
return new FulfilledPromise($resolveCb($stream));
367-
}))
333+
$filesystem
334+
->method('read')
335+
->with($path)
336+
->will($this->returnValue(new FulfilledPromise('a')))
368337
;
369338

370339
$filesystem
@@ -374,24 +343,11 @@ public function testClose()
374343
->will($this->returnValue($openPromise))
375344
;
376345

377-
$closePromise = $this->getMock('React\Promise\PromiseInterface', [
378-
'then',
379-
]);
380-
381-
$closePromise
382-
->expects($this->once())
383-
->method('then')
384-
->with($this->isType('callable'))
385-
->will($this->returnCallback(function ($resolveCb) use ($stream) {
386-
return \React\Promise\resolve($resolveCb($stream));
387-
}))
388-
;
389-
390346
$filesystem
391347
->expects($this->once())
392348
->method('close')
393349
->with($fd)
394-
->will($this->returnValue($closePromise))
350+
->will($this->returnValue(\React\Promise\resolve()))
395351
;
396352

397353
$file = new File($path, Filesystem::createFromAdapter($filesystem));

0 commit comments

Comments
 (0)