forked from consolidation/robo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Runner.php
265 lines (234 loc) · 8.34 KB
/
Runner.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php
namespace Robo;
use Robo\Common\IO;
use League\Container\Container;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\StringInput;
use Consolidation\AnnotatedCommand\PassThroughArgsInput;
class Runner
{
use IO;
const ROBOCLASS = 'RoboFile';
const ROBOFILE = 'RoboFile.php';
/**
* @var string RoboClass
*/
protected $roboClass;
/**
* @var string RoboFile
*/
protected $roboFile;
/**
* @var string working dir of Robo
*/
protected $dir;
/**
* Class Constructor
* @param null $roboClass
* @param null $roboFile
*/
public function __construct($roboClass = null, $roboFile = null, $container = null)
{
// set the const as class properties to allow overwriting in child classes
$this->roboClass = $roboClass ? $roboClass : self::ROBOCLASS ;
$this->roboFile = $roboFile ? $roboFile : self::ROBOFILE;
$this->dir = getcwd();
// Store the container in our config object if it was provided.
if ($container != null) {
Robo::setContainer($container);
}
}
protected function loadRoboFile()
{
if (class_exists($this->roboClass)) {
return true;
}
if (!file_exists($this->dir)) {
$this->yell("Path in `{$this->dir}` is invalid, please provide valid absolute path to load Robofile", 40, 'red');
return false;
}
$this->dir = realpath($this->dir);
chdir($this->dir);
if (!file_exists($this->dir . DIRECTORY_SEPARATOR . $this->roboFile)) {
return false;
}
require_once $this->dir . DIRECTORY_SEPARATOR .$this->roboFile;
if (!class_exists($this->roboClass)) {
$this->writeln("<error>Class ".$this->roboClass." was not loaded</error>");
return false;
}
return true;
}
public function execute($argv, $output = null)
{
$argv = $this->shebang($argv);
$input = $this->prepareInput($argv);
return $this->run($input, $output);
}
public function run($input = null, $output = null)
{
// If we were not provided a container, then create one
if (!Robo::hasContainer()) {
Robo::createDefaultContainer($input, $output);
// Automatically register a shutdown function and
// an error handler when we provide the container.
$this->installRoboHandlers();
}
$container = Robo::getContainer();
$output = $container->get('output');
$app = $container->get('application');
if (!$this->loadRoboFile()) {
$this->yell("Robo is not initialized here. Please run `robo init` to create a new RoboFile", 40, 'yellow');
$app->addInitRoboFileCommand($this->roboFile, $this->roboClass);
$app->run(Robo::input(), Robo::output());
return;
}
// Register the RoboFile with the container and then immediately
// fetch it; this ensures that all of the inflectors will run.
$commandFileName = "{$this->roboClass}Commands";
$container->share($commandFileName, $this->roboClass);
$roboCommandFileInstance = $container->get($commandFileName);
// RoboFiles must always extend `Tasks`.
Robo::addServiceProviders($container, $roboCommandFileInstance->getServiceProviders());
// Register commands for all of the public methods in the RoboFile.
$commandFactory = $container->get('commandFactory');
$commandList = $commandFactory->createCommandsFromClass($roboCommandFileInstance);
foreach ($commandList as $command) {
$app->add($command);
}
$statusCode = $app->run($input, $output);
return $statusCode;
}
public function installRoboHandlers()
{
register_shutdown_function(array($this, 'shutdown'));
set_error_handler(array($this, 'handleError'));
}
/**
* Process a shebang script, if one was used to launch this Runner.
*
* @param array $args
* @return $args with shebang script removed
*/
protected function shebang($args)
{
// Option 1: Shebang line names Robo, but includes no parameters.
// #!/bin/env robo
// The robo class may contain multiple commands; the user may
// select which one to run, or even get a list of commands or
// run 'help' on any of the available commands as usual.
if ((count($args) > 1) && $this->isShebangFile($args[1])) {
return array_merge([$args[0]], array_slice($args, 2));
}
// Option 2: Shebang line stipulates which command to run.
// #!/bin/env robo mycommand
// The robo class must contain a public method named 'mycommand'.
// This command will be executed every time. Arguments and options
// may be provided on the commandline as usual.
if ((count($args) > 2) && $this->isShebangFile($args[2])) {
return array_merge([$args[0]], explode(' ', $args[1]), array_slice($args, 3));
}
return $args;
}
/**
* Determine if the specified argument is a path to a shebang script.
* If so, load it.
*
* @param $filepath file to check
* @return true if shebang script was processed
*/
protected function isShebangFile($filepath)
{
if (!file_exists($filepath)) {
return false;
}
$fp = fopen($filepath, "r");
if ($fp === false) {
return false;
}
$line = fgets($fp);
$result = $this->isShebangLine($line);
if ($result) {
while ($line = fgets($fp)) {
$line = trim($line);
if ($line == '<?php') {
$script = stream_get_contents($fp);
if (preg_match('#^class *([^ ]+)#m', $script, $matches)) {
$this->roboClass = $matches[1];
eval($script);
$result = true;
}
}
}
}
fclose($fp);
return $result;
}
/**
* Test to see if the provided line is a robo 'shebang' line.
*/
protected function isShebangLine($line)
{
return ((substr($line, 0, 2) == '#!') && (strstr($line, 'robo') !== false));
}
/**
* @param $argv
* @return InputInterface
*/
protected function prepareInput($argv)
{
$passThroughArgs = [];
$pos = array_search('--', $argv);
// cutting pass-through arguments
if ($pos !== false) {
$passThroughArgs = array_slice($argv, $pos+1);
$argv = array_slice($argv, 0, $pos);
}
// loading from other directory
$pos = array_search('--load-from', $argv) ?: array_search('-f', $argv);
if ($pos !== false) {
if (isset($argv[$pos +1])) {
$this->dir = $argv[$pos +1];
unset($argv[$pos +1]);
}
unset($argv[$pos]);
// Make adjustments if '--load-from' points at a file.
if (is_file($this->dir)) {
$this->roboFile = basename($this->dir);
$this->dir = dirname($this->dir);
$className = basename($this->roboFile, '.php');
if ($className != $this->roboFile) {
$this->roboClass = $className;
}
}
}
$input = new ArgvInput($argv);
if (!empty($passThroughArgs)) {
$input = new PassThroughArgsInput($passThroughArgs, $input);
}
return $input;
}
public function shutdown()
{
$error = error_get_last();
if (!is_array($error)) {
return;
}
$this->writeln(sprintf("<error>ERROR: %s \nin %s:%d\n</error>", $error['message'], $error['file'], $error['line']));
}
/**
* This is just a proxy error handler that checks the current error_reporting level.
* In case error_reporting is disabled the error is marked as handled, otherwise
* the normal internal error handling resumes.
*
* @return bool
*/
public function handleError()
{
if (error_reporting() === 0) {
return true;
}
return false;
}
}