ProcessExecutor.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. $process = new Process($command);
  28. $process->run(function($type, $buffer) use ($output) {
  29. if (null === $output) {
  30. return;
  31. }
  32. echo $buffer;
  33. });
  34. if (null !== $output) {
  35. $output = $process->getOutput();
  36. }
  37. return $process->getExitCode();
  38. }
  39. }