| 1 |
<?php |
| 2 |
|
| 3 |
namespace Safe; |
| 4 |
|
| 5 |
use Safe\Exceptions\DirException; |
| 6 |
|
| 7 |
/** |
| 8 |
* Changes PHP's current directory to |
| 9 |
* directory. |
| 10 |
* |
| 11 |
* @param string $directory The new current directory |
| 12 |
* @throws DirException |
| 13 |
* |
| 14 |
*/ |
| 15 |
function chdir(string $directory): void |
| 16 |
{ |
| 17 |
error_clear_last(); |
| 18 |
$result = \chdir($directory); |
| 19 |
if ($result === false) { |
| 20 |
throw DirException::createFromPhpError(); |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
/** |
| 26 |
* Changes the root directory of the current process to |
| 27 |
* directory, and changes the current |
| 28 |
* working directory to "/". |
| 29 |
* |
| 30 |
* This function is only available to GNU and BSD systems, and |
| 31 |
* only when using the CLI, CGI or Embed SAPI. Also, this function |
| 32 |
* requires root privileges. |
| 33 |
* |
| 34 |
* @param string $directory The path to change the root directory to. |
| 35 |
* @throws DirException |
| 36 |
* |
| 37 |
*/ |
| 38 |
function chroot(string $directory): void |
| 39 |
{ |
| 40 |
error_clear_last(); |
| 41 |
$result = \chroot($directory); |
| 42 |
if ($result === false) { |
| 43 |
throw DirException::createFromPhpError(); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
/** |
| 49 |
* Gets the current working directory. |
| 50 |
* |
| 51 |
* @return string Returns the current working directory on success. |
| 52 |
* |
| 53 |
* On some Unix variants, getcwd will return |
| 54 |
* FALSE if any one of the parent directories does not have the |
| 55 |
* readable or search mode set, even if the current directory |
| 56 |
* does. See chmod for more information on |
| 57 |
* modes and permissions. |
| 58 |
* @throws DirException |
| 59 |
* |
| 60 |
*/ |
| 61 |
function getcwd(): string |
| 62 |
{ |
| 63 |
error_clear_last(); |
| 64 |
$result = \getcwd(); |
| 65 |
if ($result === false) { |
| 66 |
throw DirException::createFromPhpError(); |
| 67 |
} |
| 68 |
return $result; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
/** |
| 73 |
* Opens up a directory handle to be used in subsequent |
| 74 |
* closedir, readdir, and |
| 75 |
* rewinddir calls. |
| 76 |
* |
| 77 |
* @param string $path The directory path that is to be opened |
| 78 |
* @param resource $context For a description of the context parameter, |
| 79 |
* refer to the streams section of |
| 80 |
* the manual. |
| 81 |
* @return resource Returns a directory handle resource on success |
| 82 |
* @throws DirException |
| 83 |
* |
| 84 |
*/ |
| 85 |
function opendir(string $path, $context = null) |
| 86 |
{ |
| 87 |
error_clear_last(); |
| 88 |
if ($context !== null) { |
| 89 |
$result = \opendir($path, $context); |
| 90 |
} else { |
| 91 |
$result = \opendir($path); |
| 92 |
} |
| 93 |
if ($result === false) { |
| 94 |
throw DirException::createFromPhpError(); |
| 95 |
} |
| 96 |
return $result; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
/** |
| 101 |
* Resets the directory stream indicated by |
| 102 |
* dir_handle to the beginning of the |
| 103 |
* directory. |
| 104 |
* |
| 105 |
* @param resource $dir_handle The directory handle resource previously opened |
| 106 |
* with opendir. If the directory handle is |
| 107 |
* not specified, the last link opened by opendir |
| 108 |
* is assumed. |
| 109 |
* @throws DirException |
| 110 |
* |
| 111 |
*/ |
| 112 |
function rewinddir($dir_handle = null): void |
| 113 |
{ |
| 114 |
error_clear_last(); |
| 115 |
if ($dir_handle !== null) { |
| 116 |
$result = \rewinddir($dir_handle); |
| 117 |
} else { |
| 118 |
$result = \rewinddir(); |
| 119 |
} |
| 120 |
if ($result === false) { |
| 121 |
throw DirException::createFromPhpError(); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
/** |
| 127 |
* Returns an array of files and directories from the |
| 128 |
* directory. |
| 129 |
* |
| 130 |
* @param string $directory The directory that will be scanned. |
| 131 |
* @param int $sorting_order By default, the sorted order is alphabetical in ascending order. If |
| 132 |
* the optional sorting_order is set to |
| 133 |
* SCANDIR_SORT_DESCENDING, then the sort order is |
| 134 |
* alphabetical in descending order. If it is set to |
| 135 |
* SCANDIR_SORT_NONE then the result is unsorted. |
| 136 |
* @param resource $context For a description of the context parameter, |
| 137 |
* refer to the streams section of |
| 138 |
* the manual. |
| 139 |
* @return array Returns an array of filenames on success. If directory is not a directory, then |
| 140 |
* boolean FALSE is returned, and an error of level |
| 141 |
* E_WARNING is generated. |
| 142 |
* @throws DirException |
| 143 |
* |
| 144 |
*/ |
| 145 |
function scandir(string $directory, int $sorting_order = SCANDIR_SORT_ASCENDING, $context = null): array |
| 146 |
{ |
| 147 |
error_clear_last(); |
| 148 |
if ($context !== null) { |
| 149 |
$result = \scandir($directory, $sorting_order, $context); |
| 150 |
} else { |
| 151 |
$result = \scandir($directory, $sorting_order); |
| 152 |
} |
| 153 |
if ($result === false) { |
| 154 |
throw DirException::createFromPhpError(); |
| 155 |
} |
| 156 |
return $result; |
| 157 |
} |
| 158 |
|