class PhpProcess extends Process
PhpProcess runs a PHP script in an independent process.
$p = new PhpProcess('<?php echo "foo"; ?>'); $p->run(); print $p->getOutput()."\n";
Constants
ERR | |
OUT | |
STATUS_READY | |
STATUS_STARTED | |
STATUS_TERMINATED | |
STDIN | |
STDOUT | |
STDERR | |
TIMEOUT_PRECISION | |
ITER_NON_BLOCKING | |
ITER_KEEP_OUTPUT | |
ITER_SKIP_OUT | |
ITER_SKIP_ERR |
Properties
static array | $exitCodes | Exit codes translation table. | from Process |
Methods
__construct(string $script, string|null $cwd = null, array $env = null, int|float|null $timeout = 60, array $options = array()) Constructor. | ||
__destruct() | from Process | |
__clone() | from Process | |
int | run(callable|null $callback = null) Runs the process. | from Process |
Process | mustRun(callable $callback = null) Runs the process. | from Process |
start(callable $callback = null) Starts the process and returns after writing the input to STDIN. | ||
Process | restart(callable $callback = null) Restarts the process. | from Process |
int | wait(callable $callback = null) Waits for the process to terminate. | from Process |
int|null | getPid() Returns the Pid (process identifier), if applicable. | from Process |
Process | signal(int $signal) Sends a POSIX signal to the process. | from Process |
Process | disableOutput() Disables fetching output and error output from the underlying process. | from Process |
Process | enableOutput() Enables fetching output and error output from the underlying process. | from Process |
bool | isOutputDisabled() Returns true in case the output is disabled, false otherwise. | from Process |
string | getOutput() Returns the current output of the process (STDOUT). | from Process |
string | getIncrementalOutput() Returns the output incrementally. | from Process |
Generator | getIterator(int $flags) Returns an iterator to the output of the process, with the output type as keys (Process::OUT/ERR). | from Process |
Process | clearOutput() Clears the process output. | from Process |
string | getErrorOutput() Returns the current error output of the process (STDERR). | from Process |
string | getIncrementalErrorOutput() Returns the errorOutput incrementally. | from Process |
Process | clearErrorOutput() Clears the process output. | from Process |
null|int | getExitCode() Returns the exit code returned by the process. | from Process |
null|string | getExitCodeText() Returns a string representation for the exit code returned by the process. | from Process |
bool | isSuccessful() Checks if the process ended successfully. | from Process |
bool | hasBeenSignaled() Returns true if the child process has been terminated by an uncaught signal. | from Process |
int | getTermSignal() Returns the number of the signal that caused the child process to terminate its execution. | from Process |
bool | hasBeenStopped() Returns true if the child process has been stopped by a signal. | from Process |
int | getStopSignal() Returns the number of the signal that caused the child process to stop its execution. | from Process |
bool | isRunning() Checks if the process is currently running. | from Process |
bool | isStarted() Checks if the process has been started with no regard to the current state. | from Process |
bool | isTerminated() Checks if the process is terminated. | from Process |
string | getStatus() Gets the process status. | from Process |
int | stop(int|float $timeout = 10, int $signal = null) Stops the process. | from Process |
addOutput(string $line) Adds a line to the STDOUT stream. | from Process | |
addErrorOutput(string $line) Adds a line to the STDERR stream. | from Process | |
string | getCommandLine() Gets the command line to be executed. | from Process |
Process | setCommandLine(string $commandline) Sets the command line to be executed. | from Process |
float|null | getTimeout() Gets the process timeout (max. runtime). | from Process |
float|null | getIdleTimeout() Gets the process idle timeout (max. time since last output). | from Process |
Process | setTimeout(int|float|null $timeout) Sets the process timeout (max. runtime). | from Process |
Process | setIdleTimeout(int|float|null $timeout) Sets the process idle timeout (max. time since last output). | from Process |
Process | setTty(bool $tty) Enables or disables the TTY mode. | from Process |
bool | isTty() Checks if the TTY mode is enabled. | from Process |
Process | setPty(bool $bool) Sets PTY mode. | from Process |
bool | isPty() Returns PTY state. | from Process |
string|null | getWorkingDirectory() Gets the working directory. | from Process |
Process | setWorkingDirectory(string $cwd) Sets the current working directory. | from Process |
array | getEnv() Gets the environment variables. | from Process |
Process | setEnv(array $env) Sets the environment variables. | from Process |
resource|string|Iterator|null | getInput() Gets the Process input. | from Process |
Process | setInput(resource|scalar|Traversable|null $input) Sets the input. | from Process |
array | getOptions() Gets the options for proc_open. | from Process |
Process | setOptions(array $options) Sets the options for proc_open. | from Process |
bool | getEnhanceWindowsCompatibility() Gets whether or not Windows compatibility is enabled. | from Process |
Process | setEnhanceWindowsCompatibility(bool $enhance) Sets whether or not Windows compatibility is enabled. | from Process |
bool | getEnhanceSigchildCompatibility() Returns whether sigchild compatibility mode is activated or not. | from Process |
Process | setEnhanceSigchildCompatibility(bool $enhance) Activates sigchild compatibility mode. | from Process |
checkTimeout() Performs a check between the timeout definition and the time the process started. | from Process | |
static bool | isPtySupported() Returns whether PTY is supported on the current operating system. | from Process |
setPhpBinary($php) Sets the path to the PHP binary to use. |
Details
__construct(string $script, string|null $cwd = null, array $env = null, int|float|null $timeout = 60, array $options = array())
Constructor.
__destruct()
__clone()
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.
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.
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.
Process restart(callable $callback = null)
Restarts the process.
Be warned that the process is cloned before being started.
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.
int|null getPid()
Returns the Pid (process identifier), if applicable.
Process signal(int $signal)
Sends a POSIX signal to the process.
Process disableOutput()
Disables fetching output and error output from the underlying process.
Process enableOutput()
Enables fetching output and error output from the underlying process.
bool isOutputDisabled()
Returns true in case the output is disabled, false otherwise.
string getOutput()
Returns the current output of the process (STDOUT).
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.
Generator getIterator(int $flags)
Returns an iterator to the output of the process, with the output type as keys (Process::OUT/ERR).
Process clearOutput()
Clears the process output.
string getErrorOutput()
Returns the current error output of the process (STDERR).
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.
Process clearErrorOutput()
Clears the process output.
null|int getExitCode()
Returns the exit code returned by the process.
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.
bool isSuccessful()
Checks if the process ended successfully.
bool hasBeenSignaled()
Returns true if the child process has been terminated by an uncaught signal.
It always returns false on Windows.
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.
bool hasBeenStopped()
Returns true if the child process has been stopped by a signal.
It always returns false on Windows.
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.
bool isRunning()
Checks if the process is currently running.
bool isStarted()
Checks if the process has been started with no regard to the current state.
bool isTerminated()
Checks if the process is terminated.
string getStatus()
Gets the process status.
The status is one of: ready, started, terminated.
int stop(int|float $timeout = 10, int $signal = null)
Stops the process.
addOutput(string $line)
Adds a line to the STDOUT stream.
addErrorOutput(string $line)
Adds a line to the STDERR stream.
string getCommandLine()
Gets the command line to be executed.
Process setCommandLine(string $commandline)
Sets the command line to be executed.
float|null getTimeout()
Gets the process timeout (max. runtime).
float|null getIdleTimeout()
Gets the process idle timeout (max. time since last output).
Process setTimeout(int|float|null $timeout)
Sets the process timeout (max. runtime).
To disable the timeout, set this value to null.
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.
Process setTty(bool $tty)
Enables or disables the TTY mode.
bool isTty()
Checks if the TTY mode is enabled.
Process setPty(bool $bool)
Sets PTY mode.
bool isPty()
Returns PTY state.
string|null getWorkingDirectory()
Gets the working directory.
Process setWorkingDirectory(string $cwd)
Sets the current working directory.
array getEnv()
Gets the environment variables.
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.
resource|string|Iterator|null getInput()
Gets the Process input.
Process setInput(resource|scalar|Traversable|null $input)
Sets the input.
This content will be passed to the underlying process standard input.
array getOptions()
Gets the options for proc_open.
Process setOptions(array $options)
Sets the options for proc_open.
bool getEnhanceWindowsCompatibility()
Gets whether or not Windows compatibility is enabled.
This is true by default.
Process setEnhanceWindowsCompatibility(bool $enhance)
Sets whether or not Windows compatibility is enabled.
bool getEnhanceSigchildCompatibility()
Returns whether sigchild compatibility mode is activated or not.
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
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
static bool isPtySupported()
Returns whether PTY is supported on the current operating system.
setPhpBinary($php)
Sets the path to the PHP binary to use.
Please login to continue.