Completed
Push — master ( 5b4366...10bec3 )
by Greg
61:34 queued 55:09
created

ChrootAdapter::copy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * @author    Greg Roach <[email protected]>
4
 * @copyright Copyright (c) 2020
5
 * @licence   MIT
6
 */
7
8
namespace Fisharebest\Flysystem\Adapter;
9
10
use League\Flysystem\Config;
11
use League\Flysystem\DirectoryAttributes;
12
use League\Flysystem\FileAttributes;
13
use League\Flysystem\FilesystemAdapter;
14
use League\Flysystem\FilesystemException;
15
use League\Flysystem\FilesystemOperator;
16
use League\Flysystem\StorageAttributes;
17
use ReflectionException;
18
use ReflectionProperty;
19
20
use function strlen;
21
use function substr;
22
23
/**
24
 * Create a subtree from an existing filesystem.
25
 */
26
class ChrootAdapter implements FilesystemAdapter
27
{
28
    /** @var FilesystemAdapter */
29
    public $adapter;
30
31
    /** @var string */
32
    private $prefix;
33
34
    /**
35
     * ChrootAdapter constructor.
36
     *
37
     * @param FilesystemOperator $filesystem
38
     * @param string             $prefix e.g. 'some/prefix'
39
     *
40
     * @throws ReflectionException
41
     */
42
    public function __construct(FilesystemOperator $filesystem, string $prefix = '')
43
    {
44
        // Since Flysystem 2.0, the adapter is private.  Use reflection to get it.
45
        $property = new ReflectionProperty($filesystem, 'adapter');
46
        $property->setAccessible(true);
47
        $this->adapter = $property->getValue($filesystem);
48
49
        $this->prefix = trim($prefix, '/') . '/';
50
    }
51
52
    /**
53
     * @param string $path
54
     *
55
     * @return bool
56
     * @throws FilesystemException
57
     */
58
    public function fileExists(string $path): bool
59
    {
60
        return $this->adapter->fileExists($this->prefix . $path);
61
    }
62
63
    /**
64
     * @param string $path
65
     * @param string $contents
66
     * @param Config $config
67
     *
68
     * @throws FilesystemException
69
     */
70
    public function write(string $path, string $contents, Config $config): void
71
    {
72
        $this->adapter->write($this->prefix . $path, $contents, $config);
73
    }
74
75
    /**
76
     * @param string   $path
77
     * @param resource $contents
78
     * @param Config   $config
79
     *
80
     * @throws FilesystemException
81
     */
82
    public function writeStream(string $path, $contents, Config $config): void
83
    {
84
        $this->adapter->writeStream($this->prefix . $path, $contents, $config);
85
    }
86
87
    /**
88
     * @param string $path
89
     *
90
     * @return string
91
     * @throws FilesystemException
92
     */
93
    public function read(string $path): string
94
    {
95
        return $this->adapter->read($this->prefix . $path);
96
    }
97
98
    /**
99
     * @param string $path
100
     *
101
     * @return resource
102
     * @throws FilesystemException
103
     */
104
    public function readStream(string $path)
105
    {
106
        return $this->adapter->readStream($this->prefix . $path);
107
    }
108
109
    /**
110
     * @param string $path
111
     *
112
     * @throws FilesystemException
113
     */
114
    public function delete(string $path): void
115
    {
116
        $this->adapter->delete($this->prefix . $path);
117
    }
118
119
    /**
120
     * @param string $path
121
     *
122
     * @throws FilesystemException
123
     */
124
    public function deleteDirectory(string $path): void
125
    {
126
        $this->adapter->deleteDirectory($this->prefix . $path);
127
    }
128
129
    /**
130
     * @param string $path
131
     * @param Config $config
132
     *
133
     * @throws FilesystemException
134
     */
135
    public function createDirectory(string $path, Config $config): void
136
    {
137
        $this->adapter->createDirectory($this->prefix . $path, $config);
138
    }
139
140
    /**
141
     * @param string $path
142
     * @param bool   $deep
143
     *
144
     * @return iterable
145
     * @throws FilesystemException
146
     */
147
    public function listContents(string $path, bool $deep): iterable
148
    {
149
        $storage_attributes = $this->adapter->listContents($this->prefix . $path, $deep);
150
151
        foreach ($storage_attributes as $storage_attribute) {
152
            $attributes = $storage_attribute->jsonSerialize();
153
154
            $attributes[StorageAttributes::ATTRIBUTE_PATH] = substr($attributes[StorageAttributes::ATTRIBUTE_PATH], strlen($this->prefix));
155
156
            if ($storage_attribute instanceof DirectoryAttributes) {
157
                yield DirectoryAttributes::fromArray($attributes);
158
            }
159
160
            if ($storage_attribute instanceof FileAttributes) {
161
                yield FileAttributes::fromArray($attributes);
162
            }
163
        }
164
    }
165
166
    /**
167
     * @param string $source
168
     * @param string $destination
169
     * @param Config $config
170
     *
171
     * @throws FilesystemException
172
     */
173
    public function move(string $source, string $destination, Config $config): void
174
    {
175
        $this->adapter->move($this->prefix . $source, $this->prefix . $destination, $config);
176
    }
177
178
    /**
179
     * @param string $source
180
     * @param string $destination
181
     * @param Config $config
182
     *
183
     * @throws FilesystemException
184
     */
185
    public function copy(string $source, string $destination, Config $config): void
186
    {
187
        $this->adapter->copy($this->prefix . $source, $this->prefix . $destination, $config);
188
    }
189
190
    /**
191
     * @param string $path
192
     *
193
     * @return FileAttributes
194
     * @throws FilesystemException
195
     */
196
    public function lastModified(string $path): FileAttributes
197
    {
198
        return $this->adapter->lastModified($this->prefix . $path);
199
    }
200
201
    /**
202
     * @param string $path
203
     *
204
     * @return FileAttributes
205
     * @throws FilesystemException
206
     */
207
    public function fileSize(string $path): FileAttributes
208
    {
209
        return $this->adapter->fileSize($this->prefix . $path);
210
    }
211
212
    /**
213
     * @param string $path
214
     *
215
     * @return FileAttributes
216
     * @throws FilesystemException
217
     */
218
    public function mimeType(string $path): FileAttributes
219
    {
220
        return $this->adapter->mimeType($this->prefix . $path);
221
    }
222
223
    /**
224
     * @param string $path
225
     * @param string $visibility
226
     *
227
     * @throws FilesystemException
228
     */
229
    public function setVisibility(string $path, string $visibility): void
230
    {
231
        $this->adapter->setVisibility($this->prefix . $path, $visibility);
232
    }
233
234
    /**
235
     * @param string $path
236
     *
237
     * @return FileAttributes
238
     * @throws FilesystemException
239
     */
240
    public function visibility(string $path): FileAttributes
241
    {
242
        return $this->adapter->visibility($this->prefix . $path);
243
    }
244
}
245