1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Phootwork package. |
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
5
|
|
|
* file that was distributed with this source code. |
6
|
|
|
* |
7
|
|
|
* @license MIT License |
8
|
|
|
* @copyright Thomas Gossmann |
9
|
|
|
*/ |
10
|
|
|
namespace phootwork\file; |
11
|
|
|
|
12
|
|
|
use DirectoryIterator; |
13
|
|
|
use Iterator; |
14
|
|
|
use phootwork\file\exception\FileException; |
15
|
|
|
use Stringable; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Directory |
19
|
|
|
*/ |
20
|
|
|
class Directory implements Iterator, Stringable { |
21
|
|
|
use FileOperationTrait; |
22
|
|
|
|
23
|
|
|
/** @var ?DirectoryIterator */ |
24
|
|
|
private ?DirectoryIterator $iterator = null; |
25
|
|
|
|
26
|
20 |
|
public function __construct(string|Stringable $filename) { |
27
|
20 |
|
$this->pathname = (string) $filename; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Creates the directory |
32
|
|
|
* |
33
|
|
|
* @param int $mode |
34
|
|
|
* |
35
|
|
|
* @throws FileException when something goes wrong |
36
|
|
|
*/ |
37
|
15 |
|
public function make(int $mode = 0777): void { |
38
|
15 |
|
if (!$this->exists() && !@mkdir($this->pathname, $mode, true)) { |
39
|
1 |
|
throw new FileException(sprintf('Failed to create directory "%s"', $this->pathname)); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Recursively deletes the directory |
45
|
|
|
* |
46
|
|
|
* @throws FileException when something goes wrong |
47
|
|
|
*/ |
48
|
5 |
|
public function delete(): void { |
49
|
5 |
|
foreach ($this as $file) { |
50
|
5 |
|
if (!$file->isDot()) { |
51
|
5 |
|
$file->delete(); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
5 |
|
if (!@rmdir($this->pathname)) { |
56
|
1 |
|
throw new FileException(sprintf('Failed to delete directory "%s"', $this->pathname)); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Returns a directory iterator |
62
|
|
|
* |
63
|
|
|
* @return DirectoryIterator |
64
|
|
|
*/ |
65
|
6 |
|
private function getIterator(): DirectoryIterator { |
66
|
6 |
|
if ($this->iterator === null) { |
67
|
6 |
|
$this->iterator = new DirectoryIterator($this->pathname); |
68
|
|
|
} |
69
|
|
|
|
70
|
6 |
|
return $this->iterator; |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return FileDescriptor |
75
|
|
|
* |
76
|
|
|
* @internal |
77
|
|
|
*/ |
78
|
6 |
|
public function current(): FileDescriptor { |
79
|
6 |
|
return FileDescriptor::fromFileInfo($this->getIterator()->current()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @internal |
84
|
|
|
*/ |
85
|
1 |
|
public function key(): float|bool|int|string|null { |
86
|
1 |
|
return $this->getIterator()->key(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @internal |
91
|
|
|
*/ |
92
|
6 |
|
public function next(): void { |
93
|
6 |
|
$this->getIterator()->next(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @internal |
98
|
|
|
*/ |
99
|
6 |
|
public function rewind(): void { |
100
|
6 |
|
$this->getIterator()->rewind(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @internal |
105
|
|
|
*/ |
106
|
6 |
|
public function valid(): bool { |
107
|
6 |
|
return $this->getIterator()->valid(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* String representation of this directory as pathname |
112
|
|
|
*/ |
113
|
2 |
|
public function __toString(): string { |
114
|
2 |
|
return $this->pathname; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|