ProcessExecutor.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Util;
  12. use Symfony\Component\Process\Process;
  13. /**
  14. * @author Robert Schönthal <seroscho@googlemail.com>
  15. */
  16. class ProcessExecutor
  17. {
  18. /**
  19. * runs a process on the commandline
  20. *
  21. * @param $command the command to execute
  22. * @param null $output the output will be written into this var if passed
  23. * @return int statuscode
  24. */
  25. public function execute($command, &$output = null)
  26. {
  27. $captureOutput = count(func_get_args()) > 1;
  28. $process = new Process($command);
  29. $process->run(function($type, $buffer) use ($captureOutput) {
  30. if ($captureOutput) {
  31. return;
  32. }
  33. echo $buffer;
  34. });
  35. if ($captureOutput) {
  36. $output = $process->getOutput();
  37. }
  38. return $process->getExitCode();
  39. }
  40. public function splitLines($output)
  41. {
  42. return ((string) $output === '') ? array() : preg_split('{\r?\n}', $output);
  43. }
  44. }