-
-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathModeTypeDetector.php
More file actions
70 lines (60 loc) · 1.74 KB
/
ModeTypeDetector.php
File metadata and controls
70 lines (60 loc) · 1.74 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
<?php
namespace React\Filesystem;
use Exception;
use React\Filesystem\FilesystemInterface;
use React\Filesystem\TypeDetectorInterface;
class ModeTypeDetector implements TypeDetectorInterface
{
/**
* @var array
*/
protected $mapping = [
0xa000 => 'constructLink',
0x4000 => 'dir',
0x8000 => 'file',
];
/**
* @var FilesystemInterface
*/
protected $filesystem;
/**
* @param FilesystemInterface $filesystem
*/
public function __construct(FilesystemInterface $filesystem)
{
$this->filesystem = $filesystem;
}
/**
* @param array $node
* @return \React\Promise\PromiseInterface
*/
public function detect(array $node)
{
if (isset($node['mode'])) {
return $this->walkMapping($node);
}
return $this->filesystem->getAdapter()->stat($node['path'])->then(function ($stat) {
return $this->walkMapping($stat);
});
}
protected function walkMapping($stat)
{
$promiseChain = \React\Promise\reject(new Exception('Unknown type'));
foreach ($this->mapping as $mappingMode => $method) {
$promiseChain = $promiseChain->otherwise(function () use ($stat, $mappingMode, $method) {
return $this->matchMapping($stat['mode'], $mappingMode, $method);
});
}
return $promiseChain;
}
protected function matchMapping($mode, $mappingMode, $method)
{
if (($mode & $mappingMode) == $mappingMode) {
return \React\Promise\resolve([
$this->filesystem,
$method,
]);
}
return \React\Promise\reject(new Exception('Unknown filesystem method for type'));
}
}