Skip to content

Latest commit

 

History

History
86 lines (62 loc) · 2.6 KB

process.rst

File metadata and controls

86 lines (62 loc) · 2.6 KB
.. index::
   single: Process
   single: Components; Process

The Process Component

The Process Component executes commands in sub-processes.

Installation

You can install the component in many different ways:

Usage

The :class:`Symfony\\Component\\Process\\Process` class allows you to execute a command in a sub-process:

use Symfony\Component\Process\Process;

$process = new Process('ls -lsa');
$process->setTimeout(3600);
$process->run();
if (!$process->isSuccessful()) {
    throw new \RuntimeException($process->getErrorOutput());
}

print $process->getOutput();

The :method:`Symfony\\Component\\Process\\Process::run` method takes care of the subtle differences between the different platforms when executing the command.

.. versionadded:: 2.2
    The ``getIncrementalOutput()`` and ``getIncrementalErrorOutput()`` methods were added in Symfony 2.2.

The getOutput() method always return the whole content of the standard output of the command and getErrorOutput() the content of the error output. Alternatively, the :method:`Symfony\\Component\\Process\\Process::getIncrementalOutput` and :method:`Symfony\\Component\\Process\\Process::getIncrementalErrorOutput` methods returns the new outputs since the last call.

When executing a long running command (like rsync-ing files to a remote server), you can give feedback to the end user in real-time by passing an anonymous function to the :method:`Symfony\\Component\\Process\\Process::run` method:

use Symfony\Component\Process\Process;

$process = new Process('ls -lsa');
$process->run(function ($type, $buffer) {
    if ('err' === $type) {
        echo 'ERR > '.$buffer;
    } else {
        echo 'OUT > '.$buffer;
    }
});

If you want to execute some PHP code in isolation, use the PhpProcess instead:

use Symfony\Component\Process\PhpProcess;

$process = new PhpProcess(<<<EOF
    <?php echo 'Hello World'; ?>
EOF
);
$process->run();
.. versionadded:: 2.1
    The ``ProcessBuilder`` class has been as of 2.1.

To make your code work better on all platforms, you might want to use the :class:`Symfony\\Component\\Process\\ProcessBuilder` class instead:

use Symfony\Component\Process\ProcessBuilder;

$builder = new ProcessBuilder(array('ls', '-lsa'));
$builder->getProcess()->run();