|
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
|
|
|
* @throws exceptions\FileNotWriteableException |
|
46
|
|
|
*/ |
|
47
|
11 |
|
public static function checkWritable(string $path): void |
|
48
|
|
|
{ |
|
49
|
11 |
|
if (!is_writable($path)) { |
|
50
|
2 |
|
throw new exceptions\FileNotWriteableException("Location [$path] is not writeable"); |
|
51
|
|
|
} |
|
52
|
9 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Check if a file is readable. |
|
56
|
|
|
* In cases where the file cannot be read, an exception is thrown. |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $path The path to the file to be checked. |
|
59
|
|
|
* @throws exceptions\FileNotReadableException |
|
60
|
|
|
*/ |
|
61
|
11 |
|
public static function checkReadable(string $path): void |
|
62
|
|
|
{ |
|
63
|
11 |
|
if (!is_readable($path)) { |
|
64
|
2 |
|
throw new exceptions\FileNotReadableException("Location $path is not readable"); |
|
65
|
|
|
} |
|
66
|
9 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Checks if a file exists and throws an exception if not. |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $path |
|
72
|
|
|
* @throws exceptions\FileNotFoundException |
|
73
|
|
|
*/ |
|
74
|
14 |
|
public static function checkExists(string $path): void |
|
75
|
|
|
{ |
|
76
|
14 |
|
if (!file_exists($path)) { |
|
77
|
5 |
|
throw new exceptions\FileNotFoundException("Location '$path' does not exist"); |
|
78
|
|
|
} |
|
79
|
11 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Checks if a file exists and throws an exception if it does. |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $path |
|
85
|
|
|
* @throws exceptions\FileAlreadyExistsException |
|
86
|
|
|
*/ |
|
87
|
3 |
|
public static function checkNotExists(string $path): void |
|
88
|
|
|
{ |
|
89
|
3 |
|
if (file_exists($path)) { |
|
90
|
1 |
|
throw new exceptions\FileAlreadyExistsException("Location '$path' already exists"); |
|
91
|
|
|
} |
|
92
|
2 |
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Checks if a file exists and is writeable and throws a relevant exception if either condition is not met. |
|
96
|
|
|
* |
|
97
|
|
|
* @param $path |
|
98
|
|
|
* @throws exceptions\FileNotFoundException |
|
99
|
|
|
* @throws exceptions\FileNotWriteableException |
|
100
|
|
|
*/ |
|
101
|
7 |
|
public static function checkWriteSafety($path) |
|
102
|
|
|
{ |
|
103
|
7 |
|
Filesystem::checkExists($path); |
|
104
|
6 |
|
Filesystem::checkWritable($path); |
|
105
|
5 |
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Checks if a file exists and is readable and throws a relevant excetion if either condition is not met. |
|
109
|
|
|
* |
|
110
|
|
|
* @param $path |
|
111
|
|
|
* @throws exceptions\FileNotFoundException |
|
112
|
|
|
* @throws exceptions\FileNotReadableException |
|
113
|
|
|
*/ |
|
114
|
3 |
|
public static function checkReadSafety($path) |
|
115
|
|
|
{ |
|
116
|
3 |
|
Filesystem::checkExists($path); |
|
117
|
2 |
|
Filesystem::checkReadable($path); |
|
118
|
1 |
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Return an instance of the relevant FileInterface (File or Directory) for a file in a given path. |
|
122
|
|
|
* |
|
123
|
|
|
* @param string $path |
|
124
|
|
|
* @return FileInterface |
|
125
|
|
|
* @throws FileNotFoundException |
|
126
|
|
|
*/ |
|
127
|
1 |
|
public static function get($path) |
|
128
|
|
|
{ |
|
129
|
1 |
|
if (is_dir($path)) { |
|
130
|
1 |
|
return new filesystem\Directory($path); |
|
131
|
1 |
|
} else if (is_file($path)) { |
|
132
|
1 |
|
return new filesystem\File($path); |
|
133
|
|
|
} |
|
134
|
|
|
throw new FileNotFoundException("Could not get location '{$path}'"); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
5 |
|
public static function file($path) |
|
138
|
|
|
{ |
|
139
|
5 |
|
return new filesystem\File($path); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
5 |
|
public static function directory($path) |
|
143
|
|
|
{ |
|
144
|
5 |
|
return new filesystem\Directory($path); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
public static function glob($glob) |
|
148
|
|
|
{ |
|
149
|
|
|
return new filesystem\FileCollection(glob($glob)); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
public static function getAbsolutePath($path, $relativeTo = null) |
|
|
|
|
|
|
153
|
|
|
{ |
|
154
|
|
|
$relativeTo = $relativeTo ?? getcwd(); |
|
|
|
|
|
|
155
|
|
|
if (preg_match('/^(\\\\|\/)?\.|\.\.\\\\\//', $path) == 1 || (preg_match('/^[a-zA-Z]:/', $path) == 0 && PHP_OS == "Windows")) { |
|
156
|
|
|
$path = getcwd() . DIRECTORY_SEPARATOR . $path; |
|
157
|
|
|
} else if (isset($path[0]) && $path[0] !== '\\' && $path[0] !== '/') { |
|
158
|
|
|
$path = getcwd() . DIRECTORY_SEPARATOR . $path; |
|
159
|
|
|
} |
|
160
|
|
|
return $path; |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.