Completed
Push — master ( 6b6f0c...23d46a )
by Pol
02:54
created

PhpVfs::dir_closedir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace drupol\phpvfs;
6
7
use drupol\phpvfs\Filesystem\Filesystem;
8
use drupol\phpvfs\Filesystem\FilesystemInterface;
9
use drupol\phpvfs\Node\File;
10
use drupol\phpvfs\Node\FileInterface;
11
use drupol\phpvfs\Utils\Path;
12
13
/**
14
 * Class PhpVfs.
15
 */
16
class PhpVfs implements StreamWrapperInterface
17
{
18
    /**
19
     * The scheme.
20
     */
21
    protected const SCHEME = 'phpvfs';
22
23
    /**
24
     * The stream context.
25
     *
26
     * @var array
27
     */
28
    public $context;
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function dir_closedir(): bool // phpcs:ignore
34
    {
35
        throw new \Exception('Not implemented yet.');
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function dir_opendir(string $path, int $options): bool // phpcs:ignore
42
    {
43
        throw new \Exception('Not implemented yet.');
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function dir_readdir(): string // phpcs:ignore
50
    {
51
        throw new \Exception('Not implemented yet.');
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function dir_rewinddir(): bool // phpcs:ignore
58
    {
59
        throw new \Exception('Not implemented yet.');
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 6
    public static function fs(): FilesystemInterface
66
    {
67 6
        $options = \stream_context_get_options(
68 6
            \stream_context_get_default()
69
        );
70
71 6
        return $options[static::SCHEME]['filesystem'];
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function mkdir(string $path, int $mode, int $options): bool
78
    {
79
        throw new \Exception('Not implemented yet.');
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 6
    public static function register(Filesystem $filesystem, array $options = [])
86
    {
87
        $options = [
88 6
            static::SCHEME => [
89 6
                'filesystem' => $filesystem,
90
                'currentFile' => null,
91 6
            ] + $options,
92
        ];
93
94 6
        \stream_context_set_default($options);
95 6
        \stream_wrapper_register(self::SCHEME, __CLASS__);
96 6
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 1
    public function rename(string $from, string $to): bool
102
    {
103 1
        if (!$this::fs()->getCwd()->exist($from)) {
104
            throw new \Exception('Source resource does not exist.');
105
        }
106
107 1
        $from = $this::fs()->getCwd()->get($from);
108
109 1
        if ($this::fs()->getCwd()->exist($to)) {
110 1
            throw new \Exception('Destination already exist.');
111
        }
112
113 1
        $toPath = Path::fromString($to);
114
115 1
        $this::fs()
116 1
            ->getCwd()
117 1
            ->mkdir($toPath->dirname());
118
119 1
        if (null !== $parent = $from->getParent()) {
120 1
            $parent->delete($from);
121
        }
122
123 1
        $directory = $this::fs()->getCwd()->cd($toPath->dirname());
124
125 1
        $from->setAttribute('id', $toPath->basename());
126
127 1
        if ($from instanceof FileInterface) {
128 1
            $from->setPosition(0);
129
        }
130
131
        $directory
132 1
            ->add($from);
133
134 1
        return true;
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140 1
    public function rmdir(string $path, int $options): bool
141
    {
142 1
        $cwd = $this::fs()
143 1
            ->getCwd()
144 1
            ->rmdir($path);
145
146 1
        $this::fs()
147 1
            ->setCwd($cwd);
148
149 1
        return true;
150
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155
    public function stream_cast(int $cast_as) // phpcs:ignore
156
    {
157
        throw new \Exception('Not implemented yet.');
158
    }
159
160
    /**
161
     * {@inheritdoc}
162
     */
163 5
    public function stream_close(): void // phpcs:ignore
164
    {
165 5
        if (null !== $file = $this->getCurrentFile()) {
166 5
            $file->setPosition(0);
167
        }
168
169 5
        $this->setCurrentFile(null);
170 5
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175 1
    public function stream_eof(): bool // phpcs:ignore
176
    {
177 1
        return true;
178
    }
179
180
    /**
181
     * {@inheritdoc}
182
     */
183 5
    public function stream_flush(): bool // phpcs:ignore
184
    {
185 5
        \clearstatcache();
186
187 5
        return true;
188
    }
189
190
    /**
191
     * {@inheritdoc}
192
     */
193
    public function stream_lock($operation): bool // phpcs:ignore
194
    {
195
        throw new \Exception('Not implemented yet.');
196
    }
197
198
    /**
199
     * {@inheritdoc}
200
     */
201 5
    public function stream_open(string $resource, string $mode, int $options, ?string &$openedPath): bool // phpcs:ignore
202
    {
203 5
        $modeSplit = \str_split(\str_replace('b', '', $mode));
204
205 5
        $appendMode = \in_array('a', $modeSplit, true);
206 5
        $readMode = \in_array('r', $modeSplit, true);
207 5
        $writeMode = \in_array('w', $modeSplit, true);
208 5
        $extended = \in_array('+', $modeSplit, true);
0 ignored issues
show
Unused Code introduced by
The assignment to $extended is dead and can be removed.
Loading history...
209
210 5
        $resourcePath = Path::fromString($resource);
211
212 5
        $resourceExist = $this::fs()->getCwd()->exist($resource);
213 5
        $resourceDirnameExist = $this::fs()->getCwd()->exist($resourcePath->dirname());
0 ignored issues
show
Unused Code introduced by
The assignment to $resourceDirnameExist is dead and can be removed.
Loading history...
214
215 5
        if (false === $resourceExist) {
216
            if (true === $readMode) {
217
                if ($options & STREAM_REPORT_ERRORS) {
218
                    \trigger_error(\sprintf('%s: failed to open stream.', $resourcePath), E_USER_WARNING);
219
                }
220
221
                return false;
222
            }
223
224
            $this::fs()
225
                ->getCwd()
226
                ->add(File::create($resource)->root());
227
        }
228
229 5
        $file = $this::fs()->getCwd()->get($resource);
230
231 5
        if (!($file instanceof FileInterface)) {
232
            if ($options & STREAM_REPORT_ERRORS) {
233
                \trigger_error(\sprintf('fopen(%s): failed to open stream: Not a file.', $resource), E_USER_WARNING);
234
            }
235
236
            return false;
237
        }
238
239 5
        if (true === $appendMode) {
240
            $file->seekToEnd();
241 5
        } elseif (true === $writeMode) {
242 5
            $file->truncate();
243 5
            \clearstatcache();
244
        }
245
246 5
        $file->setPosition(0);
247 5
        $this->setCurrentFile($file);
248
249 5
        $openedPath = $file->getPath()->__toString();
250
251 5
        return true;
252
    }
253
254
    /**
255
     * {@inheritdoc}
256
     */
257
    public function stream_read(int $bytes) // phpcs:ignore
258
    {
259
        if (null !== $file = $this->getCurrentFile()) {
260
            return $file->read($bytes);
261
        }
262
263
        return false;
264
    }
265
266
    /**
267
     * {@inheritdoc}
268
     */
269
    public function stream_seek(int $offset, int $whence = SEEK_SET): bool // phpcs:ignore
270
    {
271
        throw new \Exception('Not implemented yet.');
272
    }
273
274
    /**
275
     * {@inheritdoc}
276
     */
277
    public function stream_set_option(int $option, int $arg1, int $arg2): bool // phpcs:ignore
278
    {
279
        throw new \Exception('Not implemented yet.');
280
    }
281
282
    /**
283
     * {@inheritdoc}
284
     */
285 1
    public function stream_stat(): array // phpcs:ignore
286
    {
287 1
        if (null === $file = $this->getCurrentFile()) {
288 1
            return [];
289
        }
290
291 1
        return (array) $file->getAttributes();
292
    }
293
294
    /**
295
     * {@inheritdoc}
296
     */
297
    public function stream_tell(): int // phpcs:ignore
298
    {
299
        throw new \Exception('Not implemented yet.');
300
    }
301
302
    /**
303
     * {@inheritdoc}
304
     */
305 5
    public function stream_write(string $data): int // phpcs:ignore
306
    {
307 5
        if (null !== $file = $this->getCurrentFile()) {
308 5
            return $file->write($data);
309
        }
310
311
        return 0;
312
    }
313
314
    /**
315
     * {@inheritdoc}
316
     */
317 1
    public function unlink(string $path): bool
318
    {
319 1
        if (true === $this::fs()->getCwd()->exist($path)) {
320 1
            $file = $this::fs()->getCwd()->get($path);
321
322 1
            if (null !== $parent = $file->getParent()) {
323 1
                $parent->delete($file);
324
            }
325
        }
326
327 1
        return true;
328
    }
329
330
    /**
331
     * {@inheritdoc}
332
     */
333 6
    public static function unregister()
334
    {
335 6
        \stream_wrapper_unregister(self::SCHEME);
336 6
    }
337
338
    /**
339
     * {@inheritdoc}
340
     */
341
    public function url_stat(string $path, int $flags): array // phpcs:ignore
342
    {
343
        throw new \Exception('Not implemented yet.');
344
    }
345
346
    /**
347
     * @return null|FileInterface
348
     */
349 5
    protected function getCurrentFile(): ?FileInterface
350
    {
351 5
        $options = \stream_context_get_options(
352 5
            \stream_context_get_default()
353
        );
354
355 5
        return $options[static::SCHEME]['currentFile'];
356
    }
357
358
    /**
359
     * @param null|FileInterface $file
360
     */
361 5
    protected function setCurrentFile(?FileInterface $file)
362
    {
363 5
        $options = \stream_context_get_options(
364 5
            \stream_context_get_default()
365
        );
366
367 5
        $options[static::SCHEME]['currentFile'] = $file;
368
369 5
        \stream_context_set_default($options);
370 5
    }
371
}
372