2 namespace Robo\Task\Base;
4 use Robo\Common\ExecTrait;
5 use Robo\Contract\CommandInterface;
6 use Robo\Contract\PrintedInterface;
7 use Robo\Contract\SimulatedInterface;
8 use Robo\Task\BaseTask;
9 use Symfony\Component\Process\Process;
13 * Executes shell script. Closes it when running in background mode.
17 * $this->taskExec('compass')->arg('watch')->run();
19 * $this->_exec('compass watch');
21 * $this->taskExec('compass watch')->background()->run();
23 * if ($this->taskExec('phpunit .')->run()->wasSuccessful()) {
24 * $this->say('tests passed');
30 class Exec extends BaseTask implements CommandInterface, PrintedInterface, SimulatedInterface
32 use \Robo\Common\CommandReceiver;
33 use \Robo\Common\ExecOneCommand;
38 protected static $instances = [];
41 * @var string|\Robo\Contract\CommandInterface
46 * @param string|\Robo\Contract\CommandInterface $command
48 public function __construct($command)
50 $this->command = $this->receiveCommand($command);
56 public function __destruct()
62 * Executes command in background mode (asynchronously)
66 public function background($arg = true)
68 self::$instances[] = $this;
69 $this->background = $arg;
76 protected function getCommandDescription()
78 return $this->getCommand();
83 public function getCommand()
85 return trim($this->command . $this->arguments);
91 public function simulate($context)
93 $this->printAction($context);
96 public static function stopRunningJobs()
98 foreach (self::$instances as $instance) {
108 public function run()
110 $this->hideProgressIndicator();
111 // TODO: Symfony 4 requires that we supply the working directory.
112 $result_data = $this->execute(new Process($this->getCommand(), getcwd()));
115 $result_data->getExitCode(),
116 $result_data->getMessage(),
117 $result_data->getData()
119 $this->showProgressIndicator();
123 if (function_exists('pcntl_signal')) {
124 pcntl_signal(SIGTERM, ['Robo\Task\Base\Exec', 'stopRunningJobs']);
127 register_shutdown_function(['Robo\Task\Base\Exec', 'stopRunningJobs']);