Class

Symfony\Component\Process\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.

Methods

__construct(string $script, string $cwd = null, array $env = array(), integer $timeout = 60, array $options = array())

Constructor.

__destruct()

from Process
__clone()

from Process
integer run(callback|null $callback = null)

Runs the process.

from Process
start(callback|null $callback = null)

Starts the process and returns after sending the STDIN.

Process restart(callable $callback = null)

Restarts the process.

from Process
integer wait(callback|null $callback = null)

Waits for the process to terminate.

from Process
string getOutput()

Returns the current output of the process (STDOUT).

from Process
string getIncrementalOutput()

Returns the output incrementally.

from Process
string getErrorOutput()

Returns the current error output of the process (STDERR).

from Process
string getIncrementalErrorOutput()

Returns the errorOutput incrementally.

from Process
integer getExitCode()

Returns the exit code returned by the process.

from Process
string getExitCodeText()

Returns a string representation for the exit code returned by the process.

from Process
Boolean isSuccessful()

Checks if the process ended successfully.

from Process
Boolean hasBeenSignaled()

Returns true if the child process has been terminated by an uncaught signal.

from Process
integer getTermSignal()

Returns the number of the signal that caused the child process to terminate its execution.

from Process
Boolean hasBeenStopped()

Returns true if the child process has been stopped by a signal.

from Process
integer getStopSignal()

Returns the number of the signal that caused the child process to stop its execution.

from Process
Boolean isRunning()

Checks if the process is currently running.

from Process
Boolean isStarted()

Checks if the process has been started with no regard to the current state.

from Process
Boolean isTerminated()

Checks if the process is terminated.

from Process
string getStatus()

Gets the process status.

from Process
integer stop(integer|float $timeout = 10)

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.

from Process
Process setTimeout(integer|float|null $timeout)

Sets the process timeout.

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
string|null getStdin()

Gets the contents of STDIN.

from Process
Process setStdin(string|null $stdin)

Sets the contents of STDIN.

from Process
array getOptions()

Gets the options for proc_open.

from Process
Process setOptions(array $options)

Sets the options for proc_open.

from Process
Boolean getEnhanceWindowsCompatibility()

Gets whether or not Windows compatibility is enabled

from Process
Process setEnhanceWindowsCompatibility(Boolean $enhance)

Sets whether or not Windows compatibility is enabled

from Process
Boolean getEnhanceSigchildCompatibility()

Return whether sigchild compatibility mode is activated or not

from Process
Process setEnhanceSigchildCompatibility(Boolean $enhance)

Activate sigchild compatibility mode

from Process
checkTimeout()

Performs a check between the timeout definition and the time the process started

from Process
setPhpBinary($php)

Sets the path to the PHP binary to use.

Details

at line 42
public __construct(string $script, string $cwd = null, array $env = array(), integer $timeout = 60, array $options = array())

Constructor.

Parameters

string $script The PHP script to run (as a string)
string $cwd The working directory
array $env The environment variables
integer $timeout The timeout in seconds
array $options An array of options for proc_open

in Process at line 161
public __destruct()

in Process at line 167
public __clone()

in Process at line 191
public integer run(callback|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.

Parameters

callback|null $callback A PHP callback to run whenever there is some output available on STDOUT or STDERR

Return Value

integer The exit status code

Exceptions

RuntimeException When process can't be launch or is stopped

at line 62
public start(callback|null $callback = null)

Starts the process and returns after sending the 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.

Parameters

callback|null $callback A PHP callback to run whenever there is some output available on STDOUT or STDERR

Exceptions

RuntimeException When process can't be launch or is stopped
RuntimeException When process is already running

in Process at line 276
public Process restart(callable $callback = null)

Restarts the process.

Be warned that the process is cloned before being started.

Parameters

callable $callback A PHP callback to run whenever there is some output available on STDOUT or STDERR

Return Value

Process The new process

Exceptions

RuntimeException When process can't be launch or is stopped
RuntimeException When process is already running

See also

start()

in Process at line 302
public integer wait(callback|null $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.

Parameters

callback|null $callback A valid PHP callback

Return Value

integer The exitcode of the process

Exceptions

RuntimeException When process timed out
RuntimeException When process stopped after receiving signal

in Process at line 347
public string getOutput()

Returns the current output of the process (STDOUT).

Return Value

string The process output

in Process at line 362
public 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.

Return Value

string The process output since the last call

in Process at line 379
public string getErrorOutput()

Returns the current error output of the process (STDERR).

Return Value

string The process error output

in Process at line 395
public 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.

Return Value

string The process error output since the last call

in Process at line 414
public integer getExitCode()

Returns the exit code returned by the process.

Return Value

integer The exit status code

Exceptions

RuntimeException In case --enable-sigchild is activated and the sigchild compatibility mode is disabled

in Process at line 436
public 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.

Return Value

string A string representation for the exit status code

See also

http://tldp.org/LDP/abs/html/exitcodes.html
http://en.wikipedia.org/wiki/Unix_signal

in Process at line 450
public Boolean isSuccessful()

Checks if the process ended successfully.

Return Value

Boolean true if the process ended successfully, false otherwise

in Process at line 466
public Boolean hasBeenSignaled()

Returns true if the child process has been terminated by an uncaught signal.

It always returns false on Windows.

Return Value

Boolean

Exceptions

RuntimeException In case --enable-sigchild is activated

in Process at line 488
public integer getTermSignal()

Returns the number of the signal that caused the child process to terminate its execution.

It is only meaningful if hasBeenSignaled() returns true.

Return Value

integer

Exceptions

RuntimeException In case --enable-sigchild is activated

in Process at line 508
public Boolean hasBeenStopped()

Returns true if the child process has been stopped by a signal.

It always returns false on Windows.

Return Value

Boolean

in Process at line 524
public integer getStopSignal()

Returns the number of the signal that caused the child process to stop its execution.

It is only meaningful if hasBeenStopped() returns true.

Return Value

integer

in Process at line 536
public Boolean isRunning()

Checks if the process is currently running.

Return Value

Boolean true if the process is currently running, false otherwise

in Process at line 552
public Boolean isStarted()

Checks if the process has been started with no regard to the current state.

Return Value

Boolean true if status is ready, false otherwise

in Process at line 562
public Boolean isTerminated()

Checks if the process is terminated.

Return Value

Boolean true if process is terminated, false otherwise

in Process at line 576
public string getStatus()

Gets the process status.

The status is one of: ready, started, terminated.

Return Value

string The current process status

in Process at line 592
public integer stop(integer|float $timeout = 10)

Stops the process.

Parameters

integer|float $timeout The timeout in seconds

Return Value

integer The exit-code of the process

Exceptions

RuntimeException if the process got signaled

in Process at line 619
public addOutput(string $line)

Adds a line to the STDOUT stream.

Parameters

string $line The line to append

in Process at line 629
public addErrorOutput(string $line)

Adds a line to the STDERR stream.

Parameters

string $line The line to append

in Process at line 639
public string getCommandLine()

Gets the command line to be executed.

Return Value

string The command to execute

in Process at line 651
public Process setCommandLine(string $commandline)

Sets the command line to be executed.

Parameters

string $commandline The command to execute

Return Value

Process The current Process instance

in Process at line 663
public float|null getTimeout()

Gets the process timeout.

Return Value

float|null The timeout in seconds or null if it's disabled

in Process at line 679
public Process setTimeout(integer|float|null $timeout)

Sets the process timeout.

To disable the timeout, set this value to null.

Parameters

integer|float|null $timeout The timeout in seconds

Return Value

Process The current Process instance

Exceptions

InvalidArgumentException if the timeout is negative

in Process at line 699
public string|null getWorkingDirectory()

Gets the working directory.

Return Value

string|null The current working directory or null on failure

in Process at line 717
public Process setWorkingDirectory(string $cwd)

Sets the current working directory.

Parameters

string $cwd The new working directory

Return Value

Process The current Process instance

in Process at line 729
public array getEnv()

Gets the environment variables.

Return Value

array The current environment variables

in Process at line 747
public 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.

Parameters

array $env The new environment variables

Return Value

Process The current Process instance

in Process at line 765
public string|null getStdin()

Gets the contents of STDIN.

Return Value

string|null The current contents

in Process at line 777
public Process setStdin(string|null $stdin)

Sets the contents of STDIN.

Parameters

string|null $stdin The new contents

Return Value

Process The current Process instance

in Process at line 789
public array getOptions()

Gets the options for proc_open.

Return Value

array The current options

in Process at line 801
public Process setOptions(array $options)

Sets the options for proc_open.

Parameters

array $options The new options

Return Value

Process The current Process instance

in Process at line 815
public Boolean getEnhanceWindowsCompatibility()

Gets whether or not Windows compatibility is enabled

This is true by default.

Return Value

Boolean

in Process at line 827
public Process setEnhanceWindowsCompatibility(Boolean $enhance)

Sets whether or not Windows compatibility is enabled

Parameters

Boolean $enhance

Return Value

Process The current Process instance

in Process at line 839
public Boolean getEnhanceSigchildCompatibility()

Return whether sigchild compatibility mode is activated or not

Return Value

Boolean

in Process at line 855
public Process setEnhanceSigchildCompatibility(Boolean $enhance)

Activate 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

Parameters

Boolean $enhance

Return Value

Process The current Process instance

in Process at line 871
public 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

Exceptions

RuntimeException In case the timeout was reached

at line 54
public setPhpBinary($php)

Sets the path to the PHP binary to use.

Parameters

$php