-
Notifications
You must be signed in to change notification settings - Fork 304
/
Exec.php
145 lines (126 loc) · 3.12 KB
/
Exec.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
namespace Robo\Task\Base;
use Closure;
use Robo\Contract\CommandInterface;
use Robo\Contract\PrintedInterface;
use Robo\Contract\SimulatedInterface;
use Robo\Task\BaseTask;
use Symfony\Component\Process\Process;
use Robo\Result;
use Robo\Common\CommandReceiver;
use Robo\Common\ExecOneCommand;
/**
* Executes shell script. Closes it when running in background mode.
*
* ``` php
* <?php
* $this->taskExec('compass')->arg('watch')->run();
* // or use shortcut
* $this->_exec('compass watch');
*
* $this->taskExec('compass watch')->background()->run();
*
* if ($this->taskExec('phpunit .')->run()->wasSuccessful()) {
* $this->say('tests passed');
* }
*
* ?>
* ```
*/
class Exec extends BaseTask implements CommandInterface, PrintedInterface, SimulatedInterface
{
use CommandReceiver;
use ExecOneCommand;
/**
* @var static[]
*/
protected static $instances = [];
/**
* @var string|\Robo\Contract\CommandInterface
*/
protected $command;
private static $isSetupStopRunningJob = false;
/**
* @param string|\Robo\Contract\CommandInterface $command
*/
public function __construct($command)
{
$this->command = $this->receiveCommand($command);
$this->setupStopRunningJobs();
}
private function setupStopRunningJobs()
{
if (self::$isSetupStopRunningJob === true) {
return;
}
$stopRunningJobs = Closure::fromCallable(['self', 'stopRunningJobs']);
if (function_exists('pcntl_signal')) {
pcntl_signal(SIGTERM, $stopRunningJobs);
}
register_shutdown_function($stopRunningJobs);
self::$isSetupStopRunningJob = true;
}
public function __destruct()
{
$this->stop();
}
/**
* Executes command in background mode (asynchronously)
*
* @param bool $arg
*
* @return $this
*/
public function background($arg = true)
{
self::$instances[] = $this;
$this->background = $arg;
return $this;
}
/**
* {@inheritdoc}
*/
protected function getCommandDescription()
{
return $this->getCommand();
}
/**
* {@inheritdoc}
*/
public function getCommand()
{
return trim($this->command . $this->arguments);
}
/**
* {@inheritdoc}
*/
public function simulate($context)
{
$this->printAction($context);
}
public static function stopRunningJobs()
{
foreach (self::$instances as $instance) {
if ($instance) {
unset($instance);
}
}
}
/**
* {@inheritdoc}
*/
public function run()
{
$this->hideProgressIndicator();
// TODO: Symfony 4 requires that we supply the working directory.
$result_data = $this->execute(Process::fromShellCommandline($this->getCommand(), getcwd()));
$result = new Result(
$this,
$result_data->getExitCode(),
$result_data->getMessage(),
$result_data->getData()
);
$this->showProgressIndicator();
return $result;
}
}