PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.5.2
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.5.2
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / vendor / rosell-dk / file-util / src / FileExists.php

FileExists.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 4.5.2, at vendor/rosell-dk/file-util/src/FileExists.php

97 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FileUtil;
4
5 use FileUtil\FileExistsUsingExec;
6
7 /**
8 * A fileExist function free of deception
9 *
10 * @package FileUtil
11 * @author Bjørn Rosell <[email protected]>
12 */
13 class FileExists
14 {
15
16 private static $lastWarning;
17
18 /**
19 * A warning handler that registers that a warning has occured and suppresses it.
20 *
21 * The function is a callback used with "set_error_handler".
22 * It is declared public because it needs to be accessible from the point where the warning is triggered.
23 *
24 * @param integer $errno
25 * @param string $errstr
26 * @param string $errfile
27 * @param integer $errline
28 *
29 * @return void
30 */
31 public static function warningHandler($errno, $errstr, $errfile, $errline)
32 {
33 self::$lastWarning = [$errstr, $errno];
34
35 // Suppress the warning by returning void
36 return;
37 }
38
39 /**
40 * A well behaved replacement for file_exist that throws upon failure rather than emitting a warning.
41 *
42 * @throws \Exception Throws an exception in case file_exists emits a warning
43 * @return boolean True if file exists. False if it doesn't.
44 */
45 public static function fileExists($path)
46 {
47 // There is a challenges here:
48 // We want to suppress warnings, but at the same time we want to know that it happened.
49 // We achieve this by registering an error handler
50 set_error_handler(
51 array('FileUtil\FileExists', "warningHandler"),
52 E_WARNING | E_USER_WARNING | E_NOTICE | E_USER_NOTICE
53 );
54 self::$lastWarning = null;
55 $found = @file_exists($path);
56
57 // restore previous error handler immediately
58 restore_error_handler();
59
60 // If file_exists returns true, we can rely on there being a file there
61 if ($found) {
62 return true;
63 }
64
65 // file_exists returned false.
66 // this result is only trustworthy if no warning was emitted.
67 if (is_null(self::$lastWarning)) {
68 return false;
69 }
70
71 list($errstr, $errno) = self::$lastWarning;
72 throw new \Exception($errstr, $errno);
73 }
74
75 /**
76 * A fileExist doing the best it can.
77 *
78 * @throws \Exception If it cannot be determined if the file exists
79 * @return boolean|null True if file exists. False if it doesn't.
80 */
81 public static function fileExistsTryHarder($path)
82 {
83 try {
84 $result = self::fileExists($path);
85 } catch (\Exception $e) {
86 try {
87 $result = FileExistsUsingExec::fileExists($path);
88 } catch (\Exception $e) {
89 throw new \Exception('Cannot determine if file exists or not');
90 } catch (\Throwable $e) {
91 throw new \Exception('Cannot determine if file exists or not');
92 }
93 }
94 return $result;
95 }
96 }
97