|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace IQParts\Content; |
|
4
|
|
|
|
|
5
|
|
|
use IQParts\Content\Object\File; |
|
6
|
|
|
use IQParts\Content\Object\Folder; |
|
7
|
|
|
use League\Flysystem\AdapterInterface; |
|
8
|
|
|
use League\Flysystem\Config; |
|
9
|
|
|
use League\Flysystem\Directory; |
|
10
|
|
|
use League\Flysystem\FileNotFoundException; |
|
11
|
|
|
use League\Flysystem\Filesystem as FlySystem; |
|
12
|
|
|
use League\Flysystem\Plugin\ListPaths; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class Filesystem |
|
16
|
|
|
* @package IQParts\Content |
|
17
|
|
|
*/ |
|
18
|
|
|
final class Filesystem |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var FlySystem |
|
22
|
|
|
*/ |
|
23
|
|
|
private $flySystem; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Filesystem constructor. |
|
27
|
|
|
* @param AdapterInterface $adapter |
|
28
|
|
|
* @param Config $config |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(AdapterInterface $adapter, Config $config = null) |
|
31
|
|
|
{ |
|
32
|
|
|
if (!$config) $config = []; |
|
33
|
|
|
$flySystem = new FlySystem($adapter, $config); |
|
34
|
|
|
$flySystem->addPlugin(new ListPaths()); |
|
35
|
|
|
$this->flySystem = $flySystem; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param string $path |
|
40
|
|
|
* @return bool |
|
41
|
|
|
*/ |
|
42
|
|
|
public function exists(string $path): bool |
|
43
|
|
|
{ |
|
44
|
|
|
if ($path === '/') $path = ''; |
|
45
|
|
|
return $this->flySystem->has($path); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param string $path |
|
50
|
|
|
* @return File|Folder|false |
|
51
|
|
|
* @throws FileNotFoundException |
|
52
|
|
|
*/ |
|
53
|
|
|
public function get(string $path) |
|
54
|
|
|
{ |
|
55
|
|
|
/** |
|
56
|
|
|
* If path = '/' -> create a folder object with all dirs and folders as children |
|
57
|
|
|
*/ |
|
58
|
|
|
if ($path === '/' || $path === '') { |
|
59
|
|
|
$dirs = []; |
|
60
|
|
|
$files = []; |
|
61
|
|
|
$paths = $this->flySystem->listPaths('', false); |
|
62
|
|
|
foreach ($paths as $item) { |
|
63
|
|
|
/** @var File|Directory $i */ |
|
64
|
|
|
$i = $this->flySystem->get($item); |
|
65
|
|
|
if ($i->getType() === 'dir') { |
|
|
|
|
|
|
66
|
|
|
$dirs[] = base64_encode($i->getPath()); |
|
67
|
|
|
} else { |
|
68
|
|
|
$files[] = base64_encode($i->getPath()); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return Folder::fromVariables(base64_encode('/'), '/', '', null, $dirs, $files); |
|
73
|
|
|
} else { |
|
74
|
|
|
$item = $this->flySystem->get($path); |
|
75
|
|
|
if ($item instanceof Directory) { |
|
76
|
|
|
return new Folder($item); |
|
77
|
|
|
} else { |
|
78
|
|
|
return new File($item); |
|
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param string $path |
|
85
|
|
|
* @return bool|false|resource |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getStream(string $path) |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->flySystem->readStream($path); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param string $path |
|
94
|
|
|
* @param bool $recursive |
|
95
|
|
|
* @return \Iterator|Folder[]|File[] |
|
96
|
|
|
*/ |
|
97
|
|
|
public function listDirContents(string $path, bool $recursive = false): \Iterator |
|
98
|
|
|
{ |
|
99
|
|
|
if ($path === '/') $path = ''; |
|
100
|
|
|
foreach ($this->flySystem->listPaths($path, $recursive) as $path) { |
|
101
|
|
|
$item = $this->flySystem->get($path); |
|
102
|
|
|
if ($item instanceof Directory) { |
|
103
|
|
|
yield new Folder($item); |
|
104
|
|
|
} else { |
|
105
|
|
|
yield new File($item); |
|
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param string $path |
|
112
|
|
|
* @param bool $recursive |
|
113
|
|
|
* @return \Iterator|Folder[] |
|
114
|
|
|
*/ |
|
115
|
|
|
public function listDirs(string $path, bool $recursive = false): \Iterator |
|
116
|
|
|
{ |
|
117
|
|
|
if ($path === '/') $path = ''; |
|
118
|
|
|
foreach ($this->flySystem->listPaths($path, $recursive) as $path) { |
|
119
|
|
|
$item = $this->flySystem->get($path); |
|
120
|
|
|
if ($item instanceof Directory) { |
|
121
|
|
|
yield new Folder($item); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param string $path |
|
128
|
|
|
* @param bool $recursive |
|
129
|
|
|
* @return \Iterator|File[] |
|
130
|
|
|
*/ |
|
131
|
|
|
public function listFiles(string $path, bool $recursive = false): \Iterator |
|
132
|
|
|
{ |
|
133
|
|
|
if ($path === '/') $path = ''; |
|
134
|
|
|
foreach ($this->flySystem->listContents($path, $recursive) as $i) { |
|
135
|
|
|
$item = $this->flySystem->get($i['path']); |
|
136
|
|
|
if ($item instanceof \League\Flysystem\File) yield new File($item); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param string $path |
|
142
|
|
|
* @return bool |
|
143
|
|
|
*/ |
|
144
|
|
|
public function delete(string $path) |
|
145
|
|
|
{ |
|
146
|
|
|
return $this->flySystem->delete($path); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @param string $path |
|
151
|
|
|
* @return bool |
|
152
|
|
|
*/ |
|
153
|
|
|
public function deleteDir(string $path) |
|
154
|
|
|
{ |
|
155
|
|
|
return $this->flySystem->deleteDir($path); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @param string $path |
|
160
|
|
|
* @param array $config |
|
161
|
|
|
* @return bool |
|
162
|
|
|
*/ |
|
163
|
|
|
public function createDir(string $path, array $config = []) |
|
164
|
|
|
{ |
|
165
|
|
|
return $this->flySystem->createDir($path, $config); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param string $path |
|
170
|
|
|
* @param $contents |
|
171
|
|
|
* @param array $config |
|
172
|
|
|
* @return bool |
|
173
|
|
|
*/ |
|
174
|
|
|
public function put(string $path, $contents, array $config = []): bool |
|
175
|
|
|
{ |
|
176
|
|
|
return $this->flySystem->put($path, $contents, $config); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @param string $path |
|
181
|
|
|
* @param resource $contents |
|
182
|
|
|
* @param array $config |
|
183
|
|
|
* @return bool |
|
184
|
|
|
*/ |
|
185
|
|
|
public function putStream(string $path, $contents, array $config = []): bool |
|
186
|
|
|
{ |
|
187
|
|
|
return $this->flySystem->putStream($path, $contents, $config); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* @return FlySystem |
|
192
|
|
|
*/ |
|
193
|
|
|
public function getFlySystem(): FlySystem |
|
194
|
|
|
{ |
|
195
|
|
|
return $this->flySystem; |
|
196
|
|
|
} |
|
197
|
|
|
} |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: