Skip to content

Commit 766cdef

Browse files
authored
Merge pull request #49 from CharlotteDunoisLabs/patch-promises
Change promise usage to recommendation
2 parents 1b7bd54 + 3af4c31 commit 766cdef

14 files changed

+33
-97
lines changed

src/AbstractSyncAdapter.php

+5-41
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@
33
namespace React\Filesystem;
44

55
use DateTime;
6+
use LogicException;
67
use React\Filesystem\Node\NodeInterface;
7-
use React\Filesystem\Stream\StreamFactory;
8-
use React\Promise\FulfilledPromise;
98
use React\Promise\PromiseInterface;
10-
use React\Promise\RejectedPromise;
11-
use WyriHaximus\React\ChildProcess\Messenger\Messages\Factory;
12-
use WyriHaximus\React\ChildProcess\Messenger\Messenger;
139

1410
abstract class AbstractSyncAdapter implements AdapterInterface
1511
{
@@ -123,8 +119,6 @@ protected function processLsContents($basePath, $result, ObjectStream $stream)
123119
];
124120
$promises[] = \React\Filesystem\detectType($this->typeDetectors, $node)->then(function (NodeInterface $node) use ($stream) {
125121
$stream->write($node);
126-
127-
return new FulfilledPromise();
128122
});
129123
}
130124

@@ -154,19 +148,7 @@ public function touch($path, $mode = self::CREATION_MODE)
154148
*/
155149
public function open($path, $flags, $mode = self::CREATION_MODE)
156150
{
157-
return new RejectedPromise();
158-
$id = null;
159-
return \WyriHaximus\React\ChildProcess\Messenger\Factory::parentFromClass(self::CHILD_CLASS_NAME, $this->loop)->then(function (Messenger $messenger) use (&$id, $path, $flags, $mode) {
160-
$id = count($this->fileDescriptors);
161-
$this->fileDescriptors[$id] = $messenger;
162-
return $this->fileDescriptors[$id]->rpc(Factory::rpc('open', [
163-
'path' => $path,
164-
'flags' => $flags,
165-
'mode' => $mode,
166-
]));
167-
})->then(function () use ($path, $flags, &$id) {
168-
return \React\Promise\resolve(StreamFactory::create($path, $id, $flags, $this));
169-
});
151+
return \React\Promise\reject(new LogicException('Not implemented'));
170152
}
171153

172154
/**
@@ -177,13 +159,7 @@ public function open($path, $flags, $mode = self::CREATION_MODE)
177159
*/
178160
public function read($fileDescriptor, $length, $offset)
179161
{
180-
return new RejectedPromise();
181-
return $this->fileDescriptors[$fileDescriptor]->rpc(Factory::rpc('read', [
182-
'length' => $length,
183-
'offset' => $offset,
184-
]))->then(function ($payload) {
185-
return \React\Promise\resolve($payload['chunk']);
186-
});
162+
return \React\Promise\reject(new LogicException('Not implemented'));
187163
}
188164

189165
/**
@@ -195,12 +171,7 @@ public function read($fileDescriptor, $length, $offset)
195171
*/
196172
public function write($fileDescriptor, $data, $length, $offset)
197173
{
198-
return new RejectedPromise();
199-
return $this->fileDescriptors[$fileDescriptor]->rpc(Factory::rpc('write', [
200-
'chunk' => $data,
201-
'length' => $length,
202-
'offset' => $offset,
203-
]));
174+
return \React\Promise\reject(new LogicException('Not implemented'));
204175
}
205176

206177
/**
@@ -209,14 +180,7 @@ public function write($fileDescriptor, $data, $length, $offset)
209180
*/
210181
public function close($fd)
211182
{
212-
return new RejectedPromise();
213-
$fileDescriptor = $this->fileDescriptors[$fd];
214-
unset($this->fileDescriptors[$fd]);
215-
return $fileDescriptor->rpc(Factory::rpc('close'))->then(function () use ($fileDescriptor) {
216-
return $fileDescriptor->softTerminate();
217-
}, function () use ($fileDescriptor) {
218-
return $fileDescriptor->softTerminate();
219-
});
183+
return \React\Promise\reject(new LogicException('Not implemented'));
220184
}
221185

222186
/**

src/Eio/Adapter.php

-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use React\Filesystem\Stream\StreamFactory;
1616
use React\Filesystem\TypeDetectorInterface;
1717
use React\Promise\Deferred;
18-
use React\Promise\FulfilledPromise;
1918

2019
class Adapter implements AdapterInterface
2120
{
@@ -228,8 +227,6 @@ protected function processLsContents($basePath, $result, ObjectStream $stream)
228227
];
229228
$promises[] = \React\Filesystem\detectType($this->typeDetectors, $node)->then(function (NodeInterface $node) use ($stream) {
230229
$stream->write($node);
231-
232-
return new FulfilledPromise();
233230
});
234231
}
235232

src/Eio/ConstTypeDetector.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace React\Filesystem\Eio;
44

5+
use Exception;
56
use React\Filesystem\FilesystemInterface;
67
use React\Filesystem\TypeDetectorInterface;
7-
use React\Promise\RejectedPromise;
88

99
class ConstTypeDetector implements TypeDetectorInterface
1010
{
@@ -37,7 +37,7 @@ public function __construct(FilesystemInterface $filesystem)
3737
public function detect(array $node)
3838
{
3939
if (!isset($node['type']) || !isset($this->mapping[$node['type']])) {
40-
return new RejectedPromise();
40+
return \React\Promise\reject(new Exception('Unknown node type'));
4141
}
4242

4343
return \React\Promise\resolve([

src/MappedTypeDetector.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace React\Filesystem;
44

5-
use React\Promise\RejectedPromise;
5+
use Exception;
66

77
class MappedTypeDetector implements TypeDetectorInterface
88
{
@@ -52,7 +52,7 @@ public function __construct(FilesystemInterface $filesystem, $options = [])
5252
public function detect(array $node)
5353
{
5454
if (!isset($node['type']) || !isset($this->mapping[$node['type']])) {
55-
return new RejectedPromise();
55+
return \React\Promise\reject(new Exception('Unknown type'));
5656
}
5757

5858
return \React\Promise\resolve([

src/ModeTypeDetector.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace React\Filesystem;
44

5+
use Exception;
56
use React\Filesystem\FilesystemInterface;
67
use React\Filesystem\TypeDetectorInterface;
7-
use React\Promise\RejectedPromise;
88

99
class ModeTypeDetector implements TypeDetectorInterface
1010
{
@@ -43,7 +43,7 @@ public function detect(array $node)
4343

4444
protected function walkMapping($stat)
4545
{
46-
$promiseChain = new RejectedPromise();
46+
$promiseChain = \React\Promise\reject(new Exception('Unknown type'));
4747
foreach ($this->mapping as $mappingMode => $method) {
4848
$promiseChain = $promiseChain->otherwise(function () use ($stat, $mappingMode, $method) {
4949
return $this->matchMapping($stat['mode'], $mappingMode, $method);
@@ -61,6 +61,6 @@ protected function matchMapping($mode, $mappingMode, $method)
6161
]);
6262
}
6363

64-
return new RejectedPromise();
64+
return \React\Promise\reject(new Exception('Unknown filesystem method for type'));
6565
}
6666
}

src/Node/Directory.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use React\Filesystem\ObjectStream;
99
use React\Filesystem\ObjectStreamSink;
1010
use React\Promise\Deferred;
11-
use React\Promise\FulfilledPromise;
1211

1312
class Directory implements DirectoryInterface
1413
{
@@ -114,15 +113,13 @@ protected function processSizeContents($nodes, $recursive)
114113
$numbers['directories'] += $size['directories'];
115114
$numbers['files'] += $size['files'];
116115
$numbers['size'] += $size['size'];
117-
return new FulfilledPromise();
118116
});
119117
}
120118
break;
121119
case $node instanceof File:
122120
$numbers['files']++;
123121
$promises[] = $node->size()->then(function ($size) use (&$numbers) {
124122
$numbers['size'] += $size;
125-
return new FulfilledPromise();
126123
});
127124
break;
128125
}
@@ -189,7 +186,7 @@ public function createRecursive($mode = AdapterInterface::CREATION_MODE)
189186
})->then(function () use ($mode) {
190187
return $this->create($mode);
191188
})->then(function () {
192-
return new FulfilledPromise();
189+
return null;
193190
});
194191
}
195192

src/Node/File.php

+6-8
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
namespace React\Filesystem\Node;
44

5+
use Exception;
56
use React\Filesystem\AdapterInterface;
67
use React\Filesystem\FilesystemInterface;
78
use React\Filesystem\ObjectStream;
89
use React\Filesystem\ObjectStreamSink;
910
use React\Filesystem\Stream\GenericStreamInterface;
10-
use React\Promise\FulfilledPromise;
11-
use React\Promise\RejectedPromise;
1211
use React\Promise\Stream;
1312
use React\Stream\ReadableStreamInterface;
1413
use React\Stream\WritableStreamInterface;
@@ -50,9 +49,9 @@ public function __construct($filename, FilesystemInterface $filesystem)
5049
public function exists()
5150
{
5251
return $this->stat()->then(function () {
53-
return new FulfilledPromise();
52+
return null;
5453
}, function () {
55-
return new RejectedPromise(new \Exception('Not found'));
54+
throw new Exception('Not found');
5655
});
5756
}
5857

@@ -96,7 +95,7 @@ public function rename($toFilename)
9695
public function create($mode = AdapterInterface::CREATION_MODE, $time = null)
9796
{
9897
return $this->stat()->then(function () {
99-
return new RejectedPromise(new \Exception('File exists'));
98+
throw new \Exception('File exists already');
10099
}, function () use ($mode, $time) {
101100
return $this->adapter->touch($this->path, $mode, $time);
102101
});
@@ -116,7 +115,7 @@ public function touch($mode = AdapterInterface::CREATION_MODE, $time = null)
116115
public function open($flags, $mode = AdapterInterface::CREATION_MODE)
117116
{
118117
if ($this->open === true) {
119-
return new RejectedPromise();
118+
return \React\Promise\reject(new Exception('File is already open'));
120119
}
121120

122121
return $this->adapter->open($this->path, $flags, $mode)->then(function (GenericStreamInterface $stream) {
@@ -132,13 +131,12 @@ public function open($flags, $mode = AdapterInterface::CREATION_MODE)
132131
public function close()
133132
{
134133
if ($this->open === false) {
135-
return new RejectedPromise();
134+
return \React\Promise\reject(new Exception('File is already closed'));
136135
}
137136

138137
return $this->adapter->close($this->fileDescriptor)->then(function () {
139138
$this->open = false;
140139
$this->fileDescriptor = null;
141-
return new FulfilledPromise();
142140
});
143141
}
144142

src/OpenFileLimiter.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace React\Filesystem;
44

55
use React\Promise\Deferred;
6-
use React\Promise\FulfilledPromise;
76

87
class OpenFileLimiter
98
{
@@ -43,7 +42,7 @@ public function open()
4342
{
4443
if ($this->current < $this->limit) {
4544
$this->current++;
46-
return new FulfilledPromise();
45+
return \React\Promise\resolve();
4746
}
4847

4948
$deferred = new Deferred();

src/Stream/DuplexStream.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Evenement\EventEmitter;
66
use React\Filesystem\AdapterInterface;
77
use React\Stream\DuplexStreamInterface;
8-
use React\Promise\FulfilledPromise;
98

109
class DuplexStream extends EventEmitter implements DuplexStreamInterface, GenericStreamInterface
1110
{
@@ -39,12 +38,11 @@ protected function readChunk()
3938
protected function resolveSize()
4039
{
4140
if ($this->readCursor < $this->size) {
42-
return new FulfilledPromise();
41+
return \React\Promise\resolve();
4342
}
4443

4544
return $this->getFilesystem()->stat($this->path)->then(function ($stat) {
4645
$this->size = $stat['size'];
47-
return new FulfilledPromise();
4846
});
4947
}
5048
}

src/Stream/ReadableStreamTrait.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
namespace React\Filesystem\Stream;
44

5-
use React\Promise\FulfilledPromise;
6-
use React\Promise\RejectedPromise;
5+
use Exception;
76
use React\Stream\Util;
87
use React\Stream\WritableStreamInterface;
98

@@ -27,11 +26,11 @@ public function resume()
2726
if ($this->size === null && $this->sizeLookupPromise === null) {
2827
$this->sizeLookupPromise = $this->getFilesystem()->stat($this->getPath())->then(function ($info) {
2928
if ($this->size !== null) {
30-
return new RejectedPromise();
29+
throw new Exception('File was already stat-ed');
3130
}
31+
3232
$this->size = $info['size'];
3333
$this->readCursor = 0;
34-
return new FulfilledPromise();
3534
});
3635
}
3736

src/functions.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace React\Filesystem;
44

5-
use React\Promise\RejectedPromise;
5+
use Exception;
66

77
/**
88
* @param AdapterInterface $adapter
@@ -40,7 +40,7 @@ function getOpenFileLimit(array $options)
4040
*/
4141
function detectType(array $typeDetectors, array $node)
4242
{
43-
$promiseChain = new RejectedPromise();
43+
$promiseChain = \React\Promise\reject(new Exception('Unknown type'));
4444
foreach ($typeDetectors as $detector) {
4545
$promiseChain = $promiseChain->otherwise(function () use ($node, $detector) {
4646
return $detector->detect($node);

tests/Eio/ConstTypeDetectorTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace React\Tests\Filesystem\Eio;
44

5+
use Exception;
56
use React\Filesystem\Eio\ConstTypeDetector;
67
use React\Filesystem\Filesystem;
78
use React\Tests\Filesystem\TestCase;
@@ -62,7 +63,7 @@ public function testDetectUnknown()
6263
(new ConstTypeDetector($filesystem))->detect([
6364
'type' => 123,
6465
])->otherwise(function ($result) use (&$callbackFired) {
65-
$this->assertNull($result);
66+
$this->assertInstanceOf('Exception', $result);
6667
$callbackFired = true;
6768
});
6869

tests/ModeTypeDetectorTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace React\Tests\Filesystem;
44

5+
use Exception;
56
use React\Filesystem\Filesystem;
67
use React\Filesystem\ModeTypeDetector;
78
use React\Promise\FulfilledPromise;
@@ -69,7 +70,7 @@ public function testDetectUnknown()
6970
(new ModeTypeDetector($filesystem))->detect([
7071
'path' => 'foo.bar',
7172
])->otherwise(function ($result) use (&$callbackFired) {
72-
$this->assertNull($result);
73+
$this->assertInstanceOf('Exception', $result);
7374
$callbackFired = true;
7475
});
7576

0 commit comments

Comments
 (0)