-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTask.php
191 lines (173 loc) · 3.86 KB
/
Task.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
<?php
/**
* @package axy\cli\bin
* @author Oleg Grigoriev <[email protected]>
*/
namespace axy\cli\bin;
use axy\cli\bin\io\IProcess;
use axy\cli\bin\io\RealProcess;
use axy\cli\bin\opts\Parser;
use axy\evil\Superglobals;
/**
* Interface of the bin task
*/
abstract class Task
{
/**
* The constructor
*
* @param string[] $argv [optional]
* @param IProcess $io [optional]
*/
public function __construct(array $argv = null, IProcess $io = null)
{
if ($argv === null) {
$argv = Superglobals::getSERVER()['argv'];
}
$this->opts = Parser::parse($argv, $this->optionsFormat);
$this->io = $io ?: new RealProcess();
}
/**
* Executes the task
*
* @return \axy\cli\bin\io\IProcess
*/
public function run()
{
if ($this->opts->error !== null) {
if ($this->opts->error !== '') {
$top = '{cmd}: ' . $this->opts->error;
} else {
$top = '';
}
$this->usage($top);
return $this->io;
}
if (!$this->loadOpts()) {
return $this->io;
}
$this->process();
return $this->io;
}
/**
* @param string $top [optional]
* @param int $status [optional]
*/
protected function usage($top = null, $status = null)
{
$rc = new \ReflectionClass($this);
$fn = dirname($rc->getFileName()).'/'.$this->fileUsage;
if (is_file($fn)) {
$content = rtrim(file_get_contents($fn)).PHP_EOL;
} else {
$content = '';
}
if ($top) {
$content = $top.PHP_EOL.$content;
}
$content = str_replace('{cmd}', basename($this->opts->command), $content);
if ($status === null) {
$status = $this->usageStatusExit;
}
$this->io->error($content, $status, false);
}
/**
* For override
*/
abstract protected function process();
/**
* For override
*
* @return bool
*/
protected function loadOpts()
{
return true;
}
/**
* Output
*
* @param string $message
* @param bool $nl [optional]
*/
final protected function out($message, $nl = true)
{
$this->io->write($message, $nl);
}
/**
* Write to STDERR
*
* @param string $message
* @param int $status [optional]
* @param bool $nl [optional]
*/
final protected function error($message, $status = null, $nl = true)
{
$this->io->error($message, $status, $nl);
}
/**
* Silent read from STDIN
*
* @param string $prompt [optional]
* @param bool $nl [optional]
* @return string
*/
final protected function silentRead($prompt = null, $nl = null)
{
if ($prompt !== null) {
$this->io->write($prompt, false);
}
return $this->io->silentReadLine($nl);
}
/**
* Reads entire STDIN
*
* @return string
*/
final protected function readAll()
{
return $this->io->readAll();
}
/**
* Reads the next line from STDIN
*
* @return string
*/
final protected function readLine()
{
return $this->io->readLine();
}
/**
* Sets the exit code of the process
*
* @param int $code
*/
final protected function setStatus($code)
{
$this->io->setStatus($code);
}
/**
* @var \axy\cli\bin\opts\Result
*/
protected $opts;
/**
* @var \axy\cli\bin\io\IProcess
*/
protected $io;
/**
* For override
*
* @var int
*/
protected $usageStatusExit = 1;
/**
* For override
*
* @var array
*/
protected $optionsFormat = [];
/**
* @var string
*/
protected $fileUsage = 'usage.txt';
}