|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Ntentan Framework |
|
4
|
|
|
* Copyright (c) 2008-2015 James Ekow Abaka Ainooson |
|
5
|
|
|
* |
|
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining |
|
7
|
|
|
* a copy of this software and associated documentation files (the |
|
8
|
|
|
* "Software"), to deal in the Software without restriction, including |
|
9
|
|
|
* without limitation the rights to use, copy, modify, merge, publish, |
|
10
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to |
|
11
|
|
|
* permit persons to whom the Software is furnished to do so, subject to |
|
12
|
|
|
* the following conditions: |
|
13
|
|
|
* |
|
14
|
|
|
* The above copyright notice and this permission notice shall be |
|
15
|
|
|
* included in all copies or substantial portions of the Software. |
|
16
|
|
|
* |
|
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
18
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|
19
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|
20
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
|
21
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
|
22
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
|
23
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
24
|
|
|
* |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
namespace ntentan\utils; |
|
28
|
|
|
|
|
29
|
|
|
use ntentan\utils\exceptions\FileNotFoundException; |
|
30
|
|
|
use ntentan\utils\filesystem\Directory; |
|
31
|
|
|
use ntentan\utils\filesystem\File; |
|
32
|
|
|
use ntentan\utils\filesystem\FileInterface; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* A collection of filesystem utilities. |
|
36
|
|
|
* |
|
37
|
|
|
*/ |
|
38
|
|
|
class Filesystem |
|
39
|
|
|
{ |
|
40
|
|
|
/** |
|
41
|
|
|
* Checks if a file is writeable. |
|
42
|
|
|
* In cases where the file cannot be written to an exception is thrown. |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $path The path to the file to be checked. |
|
45
|
|
|
* @param string|null $message |
|
46
|
|
|
* @throws exceptions\FileNotWriteableException |
|
47
|
|
|
*/ |
|
48
|
13 |
|
public static function checkWritable(string $path, string $message = null): void |
|
49
|
|
|
{ |
|
50
|
13 |
|
if (!is_writable($path)) { |
|
51
|
2 |
|
throw new exceptions\FileNotWriteableException($message ?? "Location [$path] is not writeable"); |
|
52
|
|
|
} |
|
53
|
11 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Check if a file is readable. |
|
57
|
|
|
* In cases where the file cannot be read, an exception is thrown. |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $path The path to the file to be checked. |
|
60
|
|
|
* @param string|null $message |
|
61
|
|
|
* @throws exceptions\FileNotReadableException |
|
62
|
|
|
*/ |
|
63
|
11 |
|
public static function checkReadable(string $path, string $message = null): void |
|
64
|
|
|
{ |
|
65
|
11 |
|
if (!is_readable($path)) { |
|
66
|
2 |
|
throw new exceptions\FileNotReadableException($message ?? "Location $path is not readable"); |
|
67
|
|
|
} |
|
68
|
9 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Checks if a file exists and throws an exception if not. |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $path |
|
74
|
|
|
* @param string|null $message |
|
75
|
|
|
* @throws FileNotFoundException |
|
76
|
|
|
*/ |
|
77
|
16 |
|
public static function checkExists(string $path, string $message = null): void |
|
78
|
|
|
{ |
|
79
|
16 |
|
if (!file_exists($path)) { |
|
80
|
3 |
|
throw new exceptions\FileNotFoundException($message ?? "Location '$path' does not exist"); |
|
81
|
|
|
} |
|
82
|
13 |
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Checks if a file exists and throws an exception if it does. |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $path |
|
88
|
|
|
* @param string|null $message |
|
89
|
|
|
* @throws exceptions\FileAlreadyExistsException |
|
90
|
|
|
*/ |
|
91
|
3 |
|
public static function checkNotExists(string $path, string $message = null): void |
|
92
|
|
|
{ |
|
93
|
|
|
try { |
|
94
|
3 |
|
if (file_exists($path)) { |
|
95
|
3 |
|
throw new exceptions\FileAlreadyExistsException($message ?? "Location '$path' already exists"); |
|
96
|
|
|
} |
|
97
|
1 |
|
} catch (\Throwable $e) { |
|
98
|
1 |
|
throw new exceptions\FileAlreadyExistsException($message ?? "Location '$path' already exists"); |
|
99
|
|
|
} |
|
100
|
2 |
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Checks if a file exists and is writeable and throws a relevant exception if either condition is not met. |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $path |
|
106
|
|
|
* @param string|null $message |
|
107
|
|
|
* @throws FileNotFoundException |
|
108
|
|
|
* @throws exceptions\FileNotWriteableException |
|
109
|
|
|
*/ |
|
110
|
9 |
|
public static function checkWriteSafety(string $path, string $message = null) : void |
|
111
|
|
|
{ |
|
112
|
9 |
|
Filesystem::checkExists($path, $message); |
|
113
|
8 |
|
Filesystem::checkWritable($path, $message); |
|
114
|
7 |
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Checks if a file exists and is readable and throws a relevant excetion if either condition is not met. |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $path |
|
120
|
|
|
* @param string $message |
|
121
|
|
|
* @throws FileNotFoundException |
|
122
|
|
|
* @throws exceptions\FileNotReadableException |
|
123
|
|
|
*/ |
|
124
|
3 |
|
public static function checkReadSafety(string $path, string $message=null) : void |
|
125
|
|
|
{ |
|
126
|
3 |
|
Filesystem::checkExists($path, $message); |
|
127
|
2 |
|
Filesystem::checkReadable($path, $message); |
|
128
|
1 |
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Return an instance of the relevant FileInterface (File or Directory) for a file in a given path. |
|
132
|
|
|
* |
|
133
|
|
|
* @param string $path |
|
134
|
|
|
* @return FileInterface |
|
135
|
|
|
* @throws FileNotFoundException |
|
136
|
|
|
*/ |
|
137
|
1 |
|
public static function get($path) : FileInterface |
|
138
|
|
|
{ |
|
139
|
1 |
|
if (is_dir($path)) { |
|
140
|
1 |
|
return new filesystem\Directory($path); |
|
141
|
1 |
|
} else if (is_file($path)) { |
|
142
|
1 |
|
return new filesystem\File($path); |
|
143
|
|
|
} |
|
144
|
|
|
throw new FileNotFoundException("Could not get location '{$path}'"); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Return an instance of the File object for the given path. |
|
149
|
|
|
* |
|
150
|
|
|
* @param $path |
|
151
|
|
|
* @return File |
|
152
|
|
|
*/ |
|
153
|
5 |
|
public static function file($path) : File |
|
154
|
|
|
{ |
|
155
|
5 |
|
return new filesystem\File($path); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Return an instance of the Directory object for the given path. |
|
160
|
|
|
* |
|
161
|
|
|
* @param $path |
|
162
|
|
|
* @return Directory |
|
163
|
|
|
*/ |
|
164
|
5 |
|
public static function directory($path) : Directory |
|
165
|
|
|
{ |
|
166
|
5 |
|
return new filesystem\Directory($path); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Returns a file collection for all whose name match with the provided pattern. |
|
171
|
|
|
* The format for the pattern is similar to those used by most shells as wildcards for selecting files. |
|
172
|
|
|
* |
|
173
|
|
|
* @param $pattern |
|
174
|
|
|
* @return filesystem\FileCollection |
|
175
|
|
|
*/ |
|
176
|
|
|
public static function glob($pattern) |
|
177
|
|
|
{ |
|
178
|
|
|
return new filesystem\FileCollection(glob($pattern)); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Takes any path (relative or absolute) and returns its absolute form relative to a given path. When a relative |
|
183
|
|
|
* path is not provided, the current working directory is used. |
|
184
|
|
|
* |
|
185
|
|
|
* @param $path |
|
186
|
|
|
* @param null $relativeTo |
|
|
|
|
|
|
187
|
|
|
* @return string |
|
188
|
|
|
*/ |
|
189
|
|
|
public static function getAbsolutePath($path, $relativeTo = null) |
|
190
|
|
|
{ |
|
191
|
|
|
$relativeTo = $relativeTo ?? getcwd(); |
|
192
|
|
|
if (preg_match('/^(\\\\|\/)?\.|\.\.\\\\\//', $path) == 1 || (preg_match('/^[a-zA-Z]:/', $path) == 0 && PHP_OS == "Windows")) { |
|
193
|
|
|
$path = $relativeTo . DIRECTORY_SEPARATOR . $path; |
|
194
|
|
|
} else if (isset($path[0]) && $path[0] !== '\\' && $path[0] !== '/') { |
|
195
|
|
|
$path = $relativeTo . DIRECTORY_SEPARATOR . $path; |
|
196
|
|
|
} |
|
197
|
|
|
return $path; |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|