| 1 |
<?php |
| 2 |
|
| 3 |
namespace Safe; |
| 4 |
|
| 5 |
use Safe\Exceptions\ExecException; |
| 6 |
|
| 7 |
/** |
| 8 |
* proc_get_status fetches data about a |
| 9 |
* process opened using proc_open. |
| 10 |
* |
| 11 |
* @param resource $process The proc_open resource that will |
| 12 |
* be evaluated. |
| 13 |
* @return array An array of collected information on success. The returned array contains the following elements: |
| 14 |
* |
| 15 |
* |
| 16 |
* |
| 17 |
* |
| 18 |
* elementtypedescription |
| 19 |
* |
| 20 |
* |
| 21 |
* |
| 22 |
* command |
| 23 |
* string |
| 24 |
* |
| 25 |
* The command string that was passed to proc_open. |
| 26 |
* |
| 27 |
* |
| 28 |
* |
| 29 |
* pid |
| 30 |
* int |
| 31 |
* process id |
| 32 |
* |
| 33 |
* |
| 34 |
* running |
| 35 |
* bool |
| 36 |
* |
| 37 |
* TRUE if the process is still running, FALSE if it has |
| 38 |
* terminated. |
| 39 |
* |
| 40 |
* |
| 41 |
* |
| 42 |
* signaled |
| 43 |
* bool |
| 44 |
* |
| 45 |
* TRUE if the child process has been terminated by |
| 46 |
* an uncaught signal. Always set to FALSE on Windows. |
| 47 |
* |
| 48 |
* |
| 49 |
* |
| 50 |
* stopped |
| 51 |
* bool |
| 52 |
* |
| 53 |
* TRUE if the child process has been stopped by a |
| 54 |
* signal. Always set to FALSE on Windows. |
| 55 |
* |
| 56 |
* |
| 57 |
* |
| 58 |
* exitcode |
| 59 |
* int |
| 60 |
* |
| 61 |
* The exit code returned by the process (which is only |
| 62 |
* meaningful if running is FALSE). |
| 63 |
* Only first call of this function return real value, next calls return |
| 64 |
* -1. |
| 65 |
* |
| 66 |
* |
| 67 |
* |
| 68 |
* termsig |
| 69 |
* int |
| 70 |
* |
| 71 |
* The number of the signal that caused the child process to terminate |
| 72 |
* its execution (only meaningful if signaled is TRUE). |
| 73 |
* |
| 74 |
* |
| 75 |
* |
| 76 |
* stopsig |
| 77 |
* int |
| 78 |
* |
| 79 |
* The number of the signal that caused the child process to stop its |
| 80 |
* execution (only meaningful if stopped is TRUE). |
| 81 |
* |
| 82 |
* |
| 83 |
* |
| 84 |
* |
| 85 |
* |
| 86 |
* @throws ExecException |
| 87 |
* |
| 88 |
*/ |
| 89 |
function proc_get_status($process): array |
| 90 |
{ |
| 91 |
error_clear_last(); |
| 92 |
$result = \proc_get_status($process); |
| 93 |
if ($result === false) { |
| 94 |
throw ExecException::createFromPhpError(); |
| 95 |
} |
| 96 |
return $result; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
/** |
| 101 |
* proc_nice changes the priority of the current |
| 102 |
* process by the amount specified in increment. A |
| 103 |
* positive increment will lower the priority of the |
| 104 |
* current process, whereas a negative increment |
| 105 |
* will raise the priority. |
| 106 |
* |
| 107 |
* proc_nice is not related to |
| 108 |
* proc_open and its associated functions in any way. |
| 109 |
* |
| 110 |
* @param int $increment The new priority value, the value of this may differ on platforms. |
| 111 |
* |
| 112 |
* On Unix, a low value, such as -20 means high priority |
| 113 |
* wheras a positive value have a lower priority. |
| 114 |
* |
| 115 |
* For Windows the increment parameter have the |
| 116 |
* following meanings: |
| 117 |
* @throws ExecException |
| 118 |
* |
| 119 |
*/ |
| 120 |
function proc_nice(int $increment): void |
| 121 |
{ |
| 122 |
error_clear_last(); |
| 123 |
$result = \proc_nice($increment); |
| 124 |
if ($result === false) { |
| 125 |
throw ExecException::createFromPhpError(); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
/** |
| 131 |
* system is just like the C version of the |
| 132 |
* function in that it executes the given |
| 133 |
* command and outputs the result. |
| 134 |
* |
| 135 |
* The system call also tries to automatically |
| 136 |
* flush the web server's output buffer after each line of output if |
| 137 |
* PHP is running as a server module. |
| 138 |
* |
| 139 |
* If you need to execute a command and have all the data from the |
| 140 |
* command passed directly back without any interference, use the |
| 141 |
* passthru function. |
| 142 |
* |
| 143 |
* @param string $command The command that will be executed. |
| 144 |
* @param int|null $return_var If the return_var argument is present, then the |
| 145 |
* return status of the executed command will be written to this |
| 146 |
* variable. |
| 147 |
* |
| 148 |
* @return string Returns the last line of the command output on success. |
| 149 |
* @throws ExecException |
| 150 |
* |
| 151 |
*/ |
| 152 |
function system(string $command, ?int &$return_var = null): string |
| 153 |
{ |
| 154 |
error_clear_last(); |
| 155 |
$result = \system($command, $return_var); |
| 156 |
if ($result === false) { |
| 157 |
throw ExecException::createFromPhpError(); |
| 158 |
} |
| 159 |
return $result; |
| 160 |
} |
| 161 |
|