This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Process
Fabien Potencier 29e2c0d355 Merge branch '2.5' into 2.6
* 2.5:
  [2.3] [HttpFoundation] [MimeTypeGuesser]
  Removed dead code and various cleaning
  [Console] Make it clear that the second argument is not about command options.
  Added the '-' character for spaceless on tag start and end to be consistent for block, if, set and for nodes
  [Yaml] fixed parse shortcut Key after unindented collection.
  [Console] fixed #10531
  Make the container considered non-fresh if the environment parameters are changed

Conflicts:
	src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig
2015-01-25 05:39:26 +01:00
..
Exception When a process fails, check if the output is enabled 2014-03-25 23:03:32 +01:00
Pipes Removed dead code and various cleaning 2015-01-21 21:57:55 +01:00
Tests Merge branch '2.5' into 2.6 2015-01-03 09:01:59 +01:00
.gitignore Added missing files .gitignore 2013-07-21 14:12:18 +02:00
CHANGELOG.md [Process] Deprecate using values that are not string for Process::setStdin and ProcessBuilder::setInput 2014-05-23 11:02:52 +02:00
composer.json updated version to 2.6 2014-05-23 16:36:49 +02:00
ExecutableFinder.php use value of DIRECTORY_SEPARATOR to detect Windows 2014-12-30 12:17:23 +01:00
LICENSE Updated copyright to 2015 2015-01-01 13:56:52 +01:00
PhpExecutableFinder.php use value of DIRECTORY_SEPARATOR to detect Windows 2014-12-30 12:17:23 +01:00
PhpProcess.php Docblock fixes 2014-11-30 13:33:44 +00:00
phpunit.xml.dist [Tests] Silenced all deprecations in tests for 2.3 2014-12-18 20:00:19 +01:00
Process.php Merge branch '2.5' into 2.6 2015-01-25 05:39:26 +01:00
ProcessBuilder.php Merge branch '2.3' into 2.5 2015-01-05 21:58:03 +01:00
ProcessUtils.php Merge branch '2.5' into 2.6 2014-12-30 14:34:16 +01:00
README.md [Doc] Use Markdown syntax highlighting 2014-10-01 07:38:33 +02:00

Process Component

Process executes commands in sub-processes.

In this example, we run a simple directory listing and get the result back:

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();

You can think that this is easy to achieve with plain PHP but it's not especially if you want to take care of the subtle differences between the different platforms.

And if you want to be able to get some feedback in real-time, just pass an anonymous function to the run() method and you will get the output buffer as it becomes available:

use Symfony\Component\Process\Process;

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

That's great if you want to execute a long running command (like rsync-ing files to a remote server) and give feedback to the user in real-time.

Resources

You can run the unit tests with the following command:

$ cd path/to/Symfony/Component/Process/
$ composer.phar install
$ phpunit