-
-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathFilesystem.php
More file actions
157 lines (135 loc) · 3.72 KB
/
Filesystem.php
File metadata and controls
157 lines (135 loc) · 3.72 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
<?php
namespace React\Filesystem;
use RuntimeException;
use React\EventLoop\LoopInterface;
use React\EventLoop\ExtUvLoop;
use React\Filesystem\Node;
class Filesystem implements FilesystemInterface
{
/**
* @var AdapterInterface
*/
protected $adapter;
/**
* @param LoopInterface $loop
* @param array $options
* @return FilesystemInterface
* @throws RuntimeException
*/
public static function create(LoopInterface $loop, array $options = [])
{
$adapters = static::getSupportedAdapters();
foreach($adapters as $adapter) {
if($adapter === 'Uv' && !$loop instanceof ExtUvLoop) {
continue;
}
$adapter = "\\React\\Filesystem\\".$adapter."\\Adapter";
return static::setFilesystemOnAdapter(static::createFromAdapter(new $adapter($loop, $options)));
}
throw new RuntimeException('No supported adapter found for this installation');
}
/**
* @param AdapterInterface $adapter
* @return FilesystemInterface
*/
public static function createFromAdapter(AdapterInterface $adapter)
{
return static::setFilesystemOnAdapter(new static($adapter));
}
/**
* @param FilesystemInterface $filesystem
* @return FilesystemInterface
*/
protected static function setFilesystemOnAdapter(FilesystemInterface $filesystem)
{
$filesystem->getAdapter()->setFilesystem($filesystem);
return $filesystem;
}
/**
* @return string[]
*/
public static function getSupportedAdapters()
{
$adapters = [];
if (Uv\Adapter::isSupported()) {
$adapters[] = 'Uv';
}
if (Eio\Adapter::isSupported()) {
$adapters[] = 'Eio';
}
if (ChildProcess\Adapter::isSupported()) {
$adapters[] = 'ChildProcess';
}
return $adapters;
}
/**
* Filesystem constructor.
* @param AdapterInterface $adapter
*/
private function __construct(AdapterInterface $adapter)
{
$this->adapter = $adapter;
}
/**
* @return AdapterInterface
*/
public function getAdapter()
{
return $this->adapter;
}
/**
* @param string $filename
* @return Node\FileInterface
*/
public function file($filename)
{
return new Node\File($filename, $this);
}
/**
* @param string $path
* @return Node\DirectoryInterface
*/
public function dir($path)
{
return new Node\Directory($path, $this);
}
/**
* @param string $path
* @param Node\NodeInterface $destination
* @return Node\LinkInterface
*/
public function link($path, Node\NodeInterface $destination)
{
return new Node\Link($path, $destination, $this);
}
/**
* @param string $path
* @return \React\Promise\PromiseInterface
*/
public function constructLink($path)
{
return $this->adapter->readlink($path)->then(function ($linkPath) {
return $this->adapter->detectType($linkPath);
})->then(function (Node\NodeInterface $destination) use ($path) {
return \React\Promise\resolve($this->link($path, $destination));
});
}
/**
* @param string $filename
* @return \React\Promise\PromiseInterface
*/
public function getContents($filename)
{
$file = $this->file($filename);
return $file->exists()->then(function () use ($file) {
return $file->getContents();
});
}
/**
* @param CallInvokerInterface $invoker
*/
public function setInvoker(CallInvokerInterface $invoker)
{
$this->adapter->setInvoker($invoker);
}
}