1 | <?php |
||
21 | class Filesystem extends \Symfony\Component\Filesystem\Filesystem |
||
22 | { |
||
23 | /** |
||
24 | * Build an absolute file or directory path separated by the OS specific directory separator. |
||
25 | * |
||
26 | * @param string ...$pathFragments |
||
27 | * |
||
28 | * @return string |
||
29 | */ |
||
30 | 88 | public function absolutePath($pathFragments) |
|
31 | { |
||
32 | 88 | if ($this->isAbsolutePath($pathFragments)) |
|
33 | { |
||
34 | 47 | return $pathFragments; |
|
35 | } |
||
36 | |||
37 | 69 | $args = func_get_args(); |
|
38 | 69 | array_unshift($args, getcwd()); |
|
39 | |||
40 | 69 | return implode(DIRECTORY_SEPARATOR, $args); |
|
41 | } |
||
42 | |||
43 | /** |
||
44 | * Build a file or directory path separated by the OS specific directory separator. |
||
45 | * |
||
46 | * @param string ...$pathFragments |
||
47 | * |
||
48 | * @return string |
||
49 | */ |
||
50 | 58 | public function appendPath($pathFragments) |
|
54 | |||
55 | /** |
||
56 | * Copy a file or folder recursively. |
||
57 | * |
||
58 | * @param string $originFile The original filename |
||
59 | * @param string $targetFile The target filename |
||
60 | * @param bool $overwriteNewerFiles If true, target files newer than origin files are overwritten |
||
61 | * |
||
62 | * @throws FileNotFoundException When originFile doesn't exist |
||
63 | * @throws IOException When copy fails |
||
64 | */ |
||
65 | public function copy($originFile, $targetFile, $overwriteNewerFiles = false) |
||
94 | |||
95 | /** |
||
96 | * Create an instance of stakx's File object with relative path information. |
||
97 | * |
||
98 | * @param string $filePath |
||
99 | * |
||
100 | * @return File |
||
101 | */ |
||
102 | public function createFileObject($filePath) |
||
106 | |||
107 | /** |
||
108 | * Strip the current working directory from an absolute path. |
||
109 | * |
||
110 | * @param string $path An absolute path |
||
111 | * |
||
112 | * @return string |
||
113 | */ |
||
114 | 89 | public function getRelativePath($path) |
|
118 | |||
119 | /** |
||
120 | * Get the name of a given file without the extension. |
||
121 | * |
||
122 | * @param string $filePath A file path |
||
123 | * |
||
124 | * @return string |
||
125 | */ |
||
126 | 4 | public function getBaseName($filePath) |
|
130 | |||
131 | /** |
||
132 | * Get the name of a given file. |
||
133 | * |
||
134 | * @param string $filePath A file path |
||
135 | * |
||
136 | * @return string |
||
137 | */ |
||
138 | public function getFileName($filePath) |
||
142 | |||
143 | /** |
||
144 | * Get the parent directory of a given file. |
||
145 | * |
||
146 | * @param string $filePath A file path |
||
147 | * |
||
148 | * @return string |
||
149 | */ |
||
150 | 40 | public function getFolderPath($filePath) |
|
154 | |||
155 | /** |
||
156 | * Get the extension of a given file. |
||
157 | * |
||
158 | * @param string $filename A file path |
||
159 | * |
||
160 | * @return string The extension of the file |
||
161 | */ |
||
162 | 48 | public function getExtension($filename) |
|
166 | |||
167 | /** |
||
168 | * Check whether or not if a given path is a directory. |
||
169 | * |
||
170 | * @param string $folderPath |
||
171 | * |
||
172 | * @return bool |
||
173 | */ |
||
174 | 12 | public function isDir($folderPath) |
|
178 | |||
179 | /** |
||
180 | * Check whether or not a given path is a file. |
||
181 | * |
||
182 | * @param string $filePath |
||
183 | * |
||
184 | * @return bool |
||
185 | */ |
||
186 | public function isFile($filePath) |
||
190 | |||
191 | /** |
||
192 | * Check whether a given file path is a symlink |
||
193 | * |
||
194 | * @param string $filePath |
||
195 | * |
||
196 | * @return bool |
||
197 | */ |
||
198 | 11 | public function isSymlink($filePath) |
|
202 | |||
203 | /** |
||
204 | * Only read a file's contents if it's within the current working directory |
||
205 | * |
||
206 | * @param string $filePath |
||
207 | * |
||
208 | * @return bool|string |
||
209 | */ |
||
210 | 36 | public function safeReadFile($filePath) |
|
211 | { |
||
212 | 36 | $absPath = realpath($this->absolutePath($filePath)); |
|
213 | |||
214 | 36 | if (!$this->exists($absPath)) |
|
215 | { |
||
216 | 1 | throw new FileNotFoundException(sprintf( |
|
217 | 1 | "The '%s' file could not be found or is outside the website working directory", |
|
218 | 1 | $filePath |
|
219 | )); |
||
220 | } |
||
221 | |||
222 | 36 | if (strpos($absPath, getcwd()) !== 0) |
|
223 | { |
||
224 | throw new FileAccessDeniedException(sprintf( |
||
225 | "The '%s' file is outside the website working directory", |
||
226 | $filePath |
||
227 | )); |
||
228 | } |
||
229 | |||
230 | 36 | return file_get_contents($absPath); |
|
231 | } |
||
232 | |||
233 | /** |
||
234 | * Get the full path to the file without the extension. |
||
235 | * |
||
236 | * @param string $filename A file path |
||
237 | * |
||
238 | * @return string |
||
239 | */ |
||
240 | 4 | public function removeExtension($filename) |
|
241 | { |
||
242 | 4 | return $this->appendPath( |
|
243 | 4 | $this->getFolderPath($filename), |
|
244 | 4 | $this->getBaseName($filename) |
|
245 | ); |
||
246 | } |
||
247 | |||
248 | 2 | public function path($path) |
|
252 | } |
||
253 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.