PhpProcess
class PhpProcess extends Process
PhpProcess runs a PHP script in an independent process.
$p = new PhpProcess(''); $p->run(); print $p->getOutput()."\n";
Constants
ERR |
|
OUT |
|
STATUS_READY |
|
STATUS_STARTED |
|
STATUS_TERMINATED |
|
STDIN |
|
STDOUT |
|
STDERR |
|
TIMEOUT_PRECISION |
|
Properties
static array | $exitCodes | Exit codes translation table. | from Process |
Methods
Constructor.
Starts the process and returns after writing the input to STDIN.
Disables fetching output and error output from the underlying process.
Enables fetching output and error output from the underlying process.
Returns a string representation for the exit code returned by the process.
Returns true if the child process has been terminated by an uncaught signal.
Returns the number of the signal that caused the child process to terminate its execution.
Returns the number of the signal that caused the child process to stop its execution.
Checks if the process has been started with no regard to the current state.
Gets the process idle timeout (max. time since last output).
Sets the process idle timeout (max. time since last output).
Gets whether or not Windows compatibility is enabled.
Sets whether or not Windows compatibility is enabled.
Returns whether sigchild compatibility mode is activated or not.
Activates sigchild compatibility mode.
Performs a check between the timeout definition and the time the process started.
Returns whether PTY is supported on the current operating system.
Sets the path to the PHP binary to use.
Details
at line line 36
__construct(string $script, string|null $cwd = null, array $env = null, int|float|null $timeout = 60, array $options = array())
Constructor.
in Process at line line 178
__destruct()
in Process at line line 183
__clone()
in Process at line line 207
int
run(callable|null $callback = null)
Runs the process.
The callback receives the type of output (out or err) and some bytes from the output in real-time. It allows to have feedback from the independent process during execution.
The STDOUT and STDERR are also available after the process is finished via the getOutput() and getErrorOutput() methods.
in Process at line line 227
Process
mustRun(callable $callback = null)
Runs the process.
This is identical to run() except that an exception is thrown if the process exits with a non-zero exit code.
at line line 64
start(callable $callback = null)
Starts the process and returns after writing the input to STDIN.
This method blocks until all STDIN data is sent to the process then it returns while the process runs in the background.
The termination of the process can be awaited with wait().
The callback receives the type of output (out or err) and some bytes from the output in real-time while writing the standard input to the process. It allows to have feedback from the independent process during execution. If there is no callback passed, the wait() method can be called with true as a second parameter then the callback will get all data occurred in (and since) the start call.
in Process at line line 332
Process
restart(callable $callback = null)
Restarts the process.
Be warned that the process is cloned before being started.
in Process at line line 359
int
wait(callable $callback = null)
Waits for the process to terminate.
The callback receives the type of output (out or err) and some bytes from the output in real-time while writing the standard input to the process. It allows to have feedback from the independent process during execution.
in Process at line line 391
int|null
getPid()
Returns the Pid (process identifier), if applicable.
in Process at line line 422
Process
disableOutput()
Disables fetching output and error output from the underlying process.
in Process at line line 443
Process
enableOutput()
Enables fetching output and error output from the underlying process.
in Process at line line 459
bool
isOutputDisabled()
Returns true in case the output is disabled, false otherwise.
in Process at line line 472
string
getOutput()
Returns the current output of the process (STDOUT).
in Process at line line 496
string
getIncrementalOutput()
Returns the output incrementally.
In comparison with the getOutput method which always return the whole output, this one returns the new output since the last call.
in Process at line line 534
string
getErrorOutput()
Returns the current error output of the process (STDERR).
in Process at line line 559
string
getIncrementalErrorOutput()
Returns the errorOutput incrementally.
In comparison with the getErrorOutput method which always return the whole error output, this one returns the new error output since the last call.
in Process at line line 596
null|int
getExitCode()
Returns the exit code returned by the process.
in Process at line line 620
null|string
getExitCodeText()
Returns a string representation for the exit code returned by the process.
This method relies on the Unix exit code status standardization and might not be relevant for other operating systems.
in Process at line line 634
bool
isSuccessful()
Checks if the process ended successfully.
in Process at line line 649
bool
hasBeenSignaled()
Returns true if the child process has been terminated by an uncaught signal.
It always returns false on Windows.
in Process at line line 672
int
getTermSignal()
Returns the number of the signal that caused the child process to terminate its execution.
It is only meaningful if hasBeenSignaled() returns true.
in Process at line line 694
bool
hasBeenStopped()
Returns true if the child process has been stopped by a signal.
It always returns false on Windows.
in Process at line line 710
int
getStopSignal()
Returns the number of the signal that caused the child process to stop its execution.
It is only meaningful if hasBeenStopped() returns true.
in Process at line line 722
bool
isRunning()
Checks if the process is currently running.
in Process at line line 738
bool
isStarted()
Checks if the process has been started with no regard to the current state.
in Process at line line 748
bool
isTerminated()
Checks if the process is terminated.
in Process at line line 762
string
getStatus()
Gets the process status.
The status is one of: ready, started, terminated.
in Process at line line 777
int
stop(int|float $timeout = 10, int $signal = null)
Stops the process.
in Process at line line 807
addOutput(string $line)
Adds a line to the STDOUT stream.
in Process at line line 818
addErrorOutput(string $line)
Adds a line to the STDERR stream.
in Process at line line 829
string
getCommandLine()
Gets the command line to be executed.
in Process at line line 841
Process
setCommandLine(string $commandline)
Sets the command line to be executed.
in Process at line line 853
float|null
getTimeout()
Gets the process timeout (max. runtime).
in Process at line line 863
float|null
getIdleTimeout()
Gets the process idle timeout (max. time since last output).
in Process at line line 879
Process
setTimeout(int|float|null $timeout)
Sets the process timeout (max. runtime).
To disable the timeout, set this value to null.
in Process at line line 898
Process
setIdleTimeout(int|float|null $timeout)
Sets the process idle timeout (max. time since last output).
To disable the timeout, set this value to null.
in Process at line line 937
bool
isTty()
Checks if the TTY mode is enabled.
in Process at line line 961
bool
isPty()
Returns PTY state.
in Process at line line 971
string|null
getWorkingDirectory()
Gets the working directory.
in Process at line line 989
Process
setWorkingDirectory(string $cwd)
Sets the current working directory.
in Process at line line 1001
array
getEnv()
Gets the environment variables.
in Process at line line 1019
Process
setEnv(array $env)
Sets the environment variables.
An environment variable value should be a string. If it is an array, the variable is ignored.
That happens in PHP when 'argv' is registered into the $_ENV array for instance.
in Process at line line 1039
null|string
getInput()
Gets the Process input.
in Process at line line 1055
Process
setInput(mixed $input)
Sets the input.
This content will be passed to the underlying process standard input.
in Process at line line 1071
array
getOptions()
Gets the options for proc_open.
in Process at line line 1097
bool
getEnhanceWindowsCompatibility()
Gets whether or not Windows compatibility is enabled.
This is true by default.
in Process at line line 1109
Process
setEnhanceWindowsCompatibility(bool $enhance)
Sets whether or not Windows compatibility is enabled.
in Process at line line 1121
bool
getEnhanceSigchildCompatibility()
Returns whether sigchild compatibility mode is activated or not.
in Process at line line 1137
Process
setEnhanceSigchildCompatibility(bool $enhance)
Activates sigchild compatibility mode.
Sigchild compatibility mode is required to get the exit code and determine the success of a process when PHP has been compiled with the --enable-sigchild option
in Process at line line 1152
checkTimeout()
Performs a check between the timeout definition and the time the process started.
In case you run a background process (with the start method), you should trigger this method regularly to ensure the process timeout
in Process at line line 1176
static bool
isPtySupported()
Returns whether PTY is supported on the current operating system.
at line line 56
setPhpBinary($php)
Sets the path to the PHP binary to use.