Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
FileExistsUsingExec | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
4.07 | |
0.00% |
0 / 1 |
fileExists | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
4.07 |
1 | <?php |
2 | |
3 | namespace FileUtil; |
4 | |
5 | use ExecWithFallback\ExecWithFallback; |
6 | |
7 | /** |
8 | * A fileExist implementation using exec() |
9 | * |
10 | * @package FileUtil |
11 | * @author Bjørn Rosell <it@rosell.dk> |
12 | */ |
13 | class FileExistsUsingExec |
14 | { |
15 | |
16 | /** |
17 | * A fileExist based on an exec call. |
18 | * |
19 | * @throws \Exception If exec cannot be called |
20 | * @return boolean|null True if file exists. False if it doesn't. |
21 | */ |
22 | public static function fileExists($path) |
23 | { |
24 | if (!ExecWithFallback::anyAvailable()) { |
25 | throw new \Exception( |
26 | 'cannot determine if file exists using exec() or similar - the function is unavailable' |
27 | ); |
28 | } |
29 | |
30 | // Lets try to find out by executing "ls path/to/cwebp" |
31 | ExecWithFallback::exec('ls ' . $path, $output, $returnCode); |
32 | if (($returnCode == 0) && (isset($output[0]))) { |
33 | return true; |
34 | } |
35 | |
36 | // We assume that "ls" command is general available! |
37 | // As that failed, we can conclude the file does not exist. |
38 | return false; |
39 | } |
40 | } |