1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AppUtils\FileHelper; |
6
|
|
|
|
7
|
|
|
use AppUtils\FileHelper; |
8
|
|
|
use AppUtils\FileHelper_Exception; |
9
|
|
|
use DirectoryIterator; |
10
|
|
|
use SplFileInfo; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @method FolderInfo requireReadable(?int $errorCode = null) |
14
|
|
|
* @method FolderInfo requireExists(?int $errorCode = null) |
15
|
|
|
* @method FolderInfo requireWritable(?int $errorCode = null) |
16
|
|
|
*/ |
17
|
|
|
class FolderInfo extends AbstractPathInfo |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var array<string,FolderInfo> |
21
|
|
|
*/ |
22
|
|
|
private static $infoCache = array(); |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param string|PathInfoInterface|SplFileInfo $path |
26
|
|
|
* @return FolderInfo |
27
|
|
|
* @throws FileHelper_Exception |
28
|
|
|
*/ |
29
|
|
|
public static function factory($path) : FolderInfo |
30
|
|
|
{ |
31
|
|
|
$pathString = AbstractPathInfo::type2string($path); |
32
|
|
|
|
33
|
|
|
if(empty($path)) { |
34
|
|
|
throw new FileHelper_Exception( |
35
|
|
|
'Invalid', |
36
|
|
|
'', |
37
|
|
|
FileHelper::ERROR_PATH_INVALID |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if($path instanceof FileInfo || FileInfo::is_file($pathString)) |
42
|
|
|
{ |
43
|
|
|
throw new FileHelper_Exception( |
44
|
|
|
'Cannot use a file', |
45
|
|
|
sprintf( |
46
|
|
|
'The path [%s] seems to be a file, not a folder.', |
47
|
|
|
$pathString |
48
|
|
|
), |
49
|
|
|
FileHelper::ERROR_PATH_IS_NOT_A_FOLDER |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if(!isset(self::$infoCache[$pathString])) |
54
|
|
|
{ |
55
|
|
|
self::$infoCache[$pathString] = new FolderInfo($pathString); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return self::$infoCache[$pathString]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Clears the folder cache that keeps track of any folders |
63
|
|
|
* created via {@see FolderInfo::factory()} for performance |
64
|
|
|
* reasons. |
65
|
|
|
* |
66
|
|
|
* @return void |
67
|
|
|
*/ |
68
|
|
|
public static function clearCache() : void |
69
|
|
|
{ |
70
|
|
|
self::$infoCache = array(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Detects if the target path is a folder. |
75
|
|
|
* |
76
|
|
|
* NOTE: If the folder does not exist on disk, this will |
77
|
|
|
* return true under the following conditions: |
78
|
|
|
* |
79
|
|
|
* - The path does not contain a file extension |
80
|
|
|
* - The path ends with a slash |
81
|
|
|
* |
82
|
|
|
* @param string $path |
83
|
|
|
* @return bool |
84
|
|
|
*/ |
85
|
|
|
public static function is_dir(string $path) : bool |
86
|
|
|
{ |
87
|
|
|
$path = trim($path); |
88
|
|
|
$test = trim($path, '/\\'); |
89
|
|
|
|
90
|
|
|
if($path === '' || $test === '.' || $test === '..') |
91
|
|
|
{ |
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$endingChar = $path[strlen($path) - 1]; |
96
|
|
|
|
97
|
|
|
return $endingChar === '/' || $endingChar === '\\' || is_dir($path); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return $this |
102
|
|
|
* |
103
|
|
|
* @throws FileHelper_Exception |
104
|
|
|
* @see FileHelper::ERROR_CANNOT_DELETE_FOLDER |
105
|
|
|
*/ |
106
|
|
|
public function delete() : FolderInfo |
107
|
|
|
{ |
108
|
|
|
if(!$this->exists()) |
109
|
|
|
{ |
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if(rmdir($this->path)) |
114
|
|
|
{ |
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
throw new FileHelper_Exception( |
119
|
|
|
sprintf( |
120
|
|
|
'Cannot delete folder [%s].', |
121
|
|
|
$this->getName() |
122
|
|
|
), |
123
|
|
|
sprintf( |
124
|
|
|
'The folder could not be deleted at path: [%s]', |
125
|
|
|
$this->getPath() |
126
|
|
|
), |
127
|
|
|
FileHelper::ERROR_CANNOT_DELETE_FOLDER |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return $this |
133
|
|
|
* |
134
|
|
|
* @throws FileHelper_Exception |
135
|
|
|
* @see FileHelper::ERROR_CANNOT_CREATE_FOLDER |
136
|
|
|
*/ |
137
|
|
|
public function create() : FolderInfo |
138
|
|
|
{ |
139
|
|
|
if(is_dir($this->path) || mkdir($this->path, 0777, true) || is_dir($this->path)) |
140
|
|
|
{ |
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
throw new FileHelper_Exception( |
145
|
|
|
sprintf( |
146
|
|
|
'Could not create target folder [%s].', |
147
|
|
|
$this->getName() |
148
|
|
|
), |
149
|
|
|
sprintf( |
150
|
|
|
'Tried to create the folder in path [%s].', |
151
|
|
|
$this->getPath() |
152
|
|
|
), |
153
|
|
|
FileHelper::ERROR_CANNOT_CREATE_FOLDER |
154
|
|
|
); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function getRelativeTo(FolderInfo $folder) : string |
158
|
|
|
{ |
159
|
|
|
return FileHelper::relativizePath($this->getPath(), $folder->getPath()); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function createFolderFinder() : FolderFinder |
163
|
|
|
{ |
164
|
|
|
return new FolderFinder($this); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function getIterator() : DirectoryIterator |
168
|
|
|
{ |
169
|
|
|
$this->requireExists()->requireIsFolder(); |
170
|
|
|
|
171
|
|
|
return new DirectoryIterator($this->getPath()); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function getExtension(bool $lowercase = true) : string |
175
|
|
|
{ |
176
|
|
|
return ''; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function getFolderPath() : string |
180
|
|
|
{ |
181
|
|
|
return $this->getPath(); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function createSubFolder(string $name) : FolderInfo |
185
|
|
|
{ |
186
|
|
|
return FileHelper::createFolder($this->getPath().'/'.$name); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function saveFile(string $fileName, string $content='') : FileInfo |
190
|
|
|
{ |
191
|
|
|
return FileHelper::saveFile($this.'/'.$fileName, $content); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param array<mixed> $data |
196
|
|
|
* @param string $fileName |
197
|
|
|
* @param bool $pretty |
198
|
|
|
* @return JSONFile |
199
|
|
|
* @throws FileHelper_Exception |
200
|
|
|
*/ |
201
|
|
|
public function saveJSONFile(array $data, string $fileName, bool $pretty=false) : JSONFile |
202
|
|
|
{ |
203
|
|
|
return FileHelper::saveAsJSON($data, $this.'/'.$fileName, $pretty); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|