-
-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathAdapter.php
More file actions
420 lines (374 loc) · 11 KB
/
Adapter.php
File metadata and controls
420 lines (374 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
<?php
namespace React\Filesystem\ChildProcess;
use DateTime;
use Exception;
use React\EventLoop\LoopInterface;
use React\Filesystem\ObjectStream;
use React\Filesystem\AdapterInterface;
use React\Filesystem\CallInvokerInterface;
use React\Filesystem\FilesystemInterface;
use React\Filesystem\MappedTypeDetector;
use React\Filesystem\ModeTypeDetector;
use React\Filesystem\OpenFileLimiter;
use React\Filesystem\TypeDetectorInterface;
use React\Filesystem\PermissionFlagResolver;
use React\Filesystem\Node\NodeInterface;
use React\Promise\PromiseInterface;
use WyriHaximus\React\ChildProcess\Messenger\Messages\Factory;
use WyriHaximus\React\ChildProcess\Messenger\Messages\Payload;
use WyriHaximus\React\ChildProcess\Messenger\Messenger;
use WyriHaximus\React\ChildProcess\Pool\Options;
use WyriHaximus\React\ChildProcess\Pool\PoolInterface;
class Adapter implements AdapterInterface
{
const DEFAULT_POOL = 'WyriHaximus\React\ChildProcess\Pool\Factory\Flexible';
const POOL_INTERFACE = 'WyriHaximus\React\ChildProcess\Pool\PoolFactoryInterface';
const CHILD_CLASS_NAME = 'React\Filesystem\ChildProcess\Process';
/**
* @var LoopInterface
*/
protected $loop;
/**
* @var FilesystemInterface
*/
protected $filesystem;
/**
* @var PoolInterface
*/
protected $pool;
/**
* @var OpenFileLimiter
*/
protected $openFileLimiter;
/**
* @var array
*/
protected $fileDescriptors = [];
/**
* @var TypeDetectorInterface[]
*/
protected $typeDetectors = [];
/**
* @var PermissionFlagResolver
*/
protected $permissionFlagResolver;
/**
* @var CallInvokerInterface
*/
protected $invoker;
/**
* @var array
*/
protected $options = [
'lsFlags' => SCANDIR_SORT_NONE,
];
/**
* Adapter constructor.
* @param LoopInterface $loop
* @param array $options
*/
public function __construct(LoopInterface $loop, array $options = [])
{
$this->loop = $loop;
$this->invoker = \React\Filesystem\getInvoker($this, $options, 'invoker', 'React\Filesystem\InstantInvoker');
$this->openFileLimiter = new OpenFileLimiter(\React\Filesystem\getOpenFileLimit($options));
$this->permissionFlagResolver = new PermissionFlagResolver();
$this->setUpPool($options);
$this->options = array_merge_recursive($this->options, $options);
}
protected function setUpPool($options)
{
$poolOptions = [
Options::MIN_SIZE => 0,
Options::MAX_SIZE => 50,
Options::TTL => 3,
];
$poolClass = static::DEFAULT_POOL;
if (isset($options['pool']['class']) && is_subclass_of($options['pool']['class'], static::POOL_INTERFACE)) {
$poolClass = $options['pool']['class'];
}
call_user_func_array($poolClass . '::createFromClass', [
self::CHILD_CLASS_NAME,
$this->loop,
$poolOptions,
])->then(function (PoolInterface $pool) {
$this->pool = $pool;
});
}
/**
* @return bool
*/
public static function isSupported()
{
return substr(strtolower(PHP_OS), 0, 3) !== 'win' && function_exists('proc_open');
}
/**
* @return LoopInterface
*/
public function getLoop()
{
return $this->loop;
}
/**
* {@inheritDoc}
*/
public function setFilesystem(FilesystemInterface $filesystem)
{
$this->filesystem = $filesystem;
$this->typeDetectors = [
MappedTypeDetector::createDefault($this->filesystem),
new ModeTypeDetector($this->filesystem),
];
}
/**
* @param CallInvokerInterface $invoker
* @return void
*/
public function setInvoker(CallInvokerInterface $invoker)
{
$this->invoker = $invoker;
}
/**
* @param string $function
* @param array $args
* @param int $errorResultCode
* @return PromiseInterface
*/
public function callFilesystem($function, $args, $errorResultCode = -1)
{
return $this->pool->rpc(Factory::rpc($function, $args))->then(function (Payload $payload) {
return \React\Promise\resolve($payload->getPayload());
}, function ($payload) {
return \React\Promise\reject(new Exception($payload['error']['message']));
});
}
/**
* @param string $path
* @param int $mode
* @return PromiseInterface
*/
public function chmod($path, $mode)
{
return $this->invoker->invokeCall('chmod', [
'path' => $path,
'mode' => decoct($mode),
]);
}
/**
* @param string $path
* @param $mode
* @return PromiseInterface
*/
public function mkdir($path, $mode = self::CREATION_MODE)
{
return $this->invoker->invokeCall('mkdir', [
'path' => $path,
'mode' => decoct($this->permissionFlagResolver->resolve($mode)),
]);
}
/**
* @param string $path
* @param string $flags
* @param $mode
* @return PromiseInterface
*/
public function open($path, $flags, $mode = self::CREATION_MODE)
{
$id = null;
return \WyriHaximus\React\ChildProcess\Messenger\Factory::parentFromClass(self::CHILD_CLASS_NAME, $this->loop)->then(function (Messenger $messenger) use (&$id, $path, $flags, $mode) {
$id = count($this->fileDescriptors);
$this->fileDescriptors[$id] = $messenger;
return $this->fileDescriptors[$id]->rpc(Factory::rpc('open', [
'path' => $path,
'flags' => $flags,
'mode' => $mode,
]));
})->then(function () use (&$id) {
return $id;
});
}
/**
* @param string $fileDescriptor
* @param int $length
* @param int $offset
* @return PromiseInterface
*/
public function read($fileDescriptor, $length, $offset)
{
return $this->fileDescriptors[$fileDescriptor]->rpc(Factory::rpc('read', [
'length' => $length,
'offset' => $offset,
]))->then(function ($payload) {
return \React\Promise\resolve(base64_decode($payload['chunk']));
});
}
/**
* @param string $fileDescriptor
* @param string $data
* @param int $length
* @param int $offset
* @return PromiseInterface
*/
public function write($fileDescriptor, $data, $length, $offset)
{
return $this->fileDescriptors[$fileDescriptor]->rpc(Factory::rpc('write', [
'chunk' => base64_encode($data),
'length' => $length,
'offset' => $offset,
]));
}
/**
* @param string $fd
* @return PromiseInterface
*/
public function close($fd)
{
$fileDescriptor = $this->fileDescriptors[$fd];
unset($this->fileDescriptors[$fd]);
return $fileDescriptor->rpc(Factory::rpc('close'))->then(function () use ($fileDescriptor) {
return $fileDescriptor->softTerminate();
}, function () use ($fileDescriptor) {
return $fileDescriptor->softTerminate();
});
}
/**
* @param string $path
* @return PromiseInterface
*/
public function rmdir($path)
{
return $this->invoker->invokeCall('rmdir', [
'path' => $path,
]);
}
/**
* @param string $path
* @return PromiseInterface
*/
public function unlink($path)
{
return $this->invoker->invokeCall('unlink', [
'path' => $path,
]);
}
/**
* @param string $path
* @param int $uid
* @param int $gid
* @return PromiseInterface
*/
public function chown($path, $uid, $gid)
{
return $this->invoker->invokeCall('chown', [
'path' => $path,
'uid' => $uid,
'gid' => $gid,
]);
}
/**
* @param string $filename
* @return PromiseInterface
*/
public function stat($filename)
{
return $this->invoker->invokeCall('stat', [
'path' => $filename,
])->then(function ($stat) {
$stat['atime'] = new DateTime('@' . $stat['atime']);
$stat['mtime'] = new DateTime('@' . $stat['mtime']);
$stat['ctime'] = new DateTime('@' . $stat['ctime']);
return \React\Promise\resolve($stat);
});
}
/**
* @param string $path
* @return PromiseInterface
*/
public function ls($path)
{
$stream = new ObjectStream();
$this->invoker->invokeCall('readdir', [
'path' => $path,
'flags' => $this->options['lsFlags'],
])->then(function ($result) use ($path, $stream) {
$this->processLsContents($path, $result, $stream);
});
return $stream;
}
protected function processLsContents($basePath, $result, ObjectStream $stream)
{
$promises = [];
foreach ($result as $entry) {
$path = $basePath . DIRECTORY_SEPARATOR . $entry['name'];
$node = [
'path' => $path,
'type' => $entry['type'],
];
$promises[] = \React\Filesystem\detectType($this->typeDetectors, $node)->then(function (NodeInterface $node) use ($stream) {
$stream->write($node);
});
}
\React\Promise\all($promises)->then(function () use ($stream) {
$stream->close();
});
}
/**
* @param string $path
* @param $mode
* @return PromiseInterface
*/
public function touch($path, $mode = self::CREATION_MODE)
{
return $this->invoker->invokeCall('touch', [
'path' => $path,
'mode' => decoct($this->permissionFlagResolver->resolve($mode)),
]);
}
/**
* @param string $fromPath
* @param string $toPath
* @return PromiseInterface
*/
public function rename($fromPath, $toPath)
{
return $this->invoker->invokeCall('rename', [
'from' => $fromPath,
'to' => $toPath,
]);
}
/**
* @param string $path
* @return PromiseInterface
*/
public function readlink($path)
{
return $this->invoker->invokeCall('readlink', [
'path' => $path,
])->then(function ($result) {
return \React\Promise\resolve($result['path']);
});
}
/**
* @param string $fromPath
* @param string $toPath
* @return PromiseInterface
*/
public function symlink($fromPath, $toPath)
{
return $this->invoker->invokeCall('symlink', [
'from' => $fromPath,
'to' => $toPath,
])->then(function ($result) {
return \React\Promise\resolve($result['result']);
});
}
/**
* @inheritDoc
*/
public function detectType($path)
{
return \React\Filesystem\detectType($this->typeDetectors, [
'path' => $path,
]);
}
}