LimeShellCommand.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. class LimeShellCommand
  3. {
  4. protected
  5. $command = null,
  6. $status = null,
  7. $output = '',
  8. $errors = '',
  9. $errorFile = '';
  10. public function __construct($file, array $arguments = array())
  11. {
  12. foreach ($arguments as $argument => $value)
  13. {
  14. $arguments[$argument] = '--'.$argument;
  15. if ($value !== true)
  16. {
  17. if (!is_string($value))
  18. {
  19. $value = var_export($value, true);
  20. }
  21. $arguments[$argument] .= '='.escapeshellarg($value);
  22. }
  23. }
  24. $this->errorFile = tempnam(sys_get_temp_dir(), 'lime');
  25. // see http://trac.symfony-project.org/ticket/5437 for the explanation on the weird "cd" thing
  26. $this->command = sprintf(
  27. 'cd & %s %s %s 2>%s',
  28. escapeshellarg(LimeShell::getExecutable()),
  29. escapeshellarg($file),
  30. implode(' ', $arguments),
  31. $this->errorFile
  32. );
  33. }
  34. public function execute()
  35. {
  36. // clear old errors
  37. $this->errors = '';
  38. file_put_contents($this->errorFile, '');
  39. ob_start();
  40. passthru($this->command, $this->status);
  41. $this->output = ob_get_clean();
  42. $this->errors = file_get_contents($this->errorFile);
  43. }
  44. public function getStatus()
  45. {
  46. return $this->status;
  47. }
  48. public function getOutput()
  49. {
  50. return $this->output;
  51. }
  52. public function getErrors()
  53. {
  54. return $this->errors;
  55. }
  56. }