|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the PHPoole package. |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace PHPoole\Command; |
|
12
|
|
|
|
|
13
|
|
|
use PHPoole\PHPoole; |
|
14
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
15
|
|
|
use Symfony\Component\Yaml\Exception\ParseException; |
|
16
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
17
|
|
|
use Zend\Console\Adapter\AdapterInterface as Console; |
|
18
|
|
|
use Zend\Console\ColorInterface as Color; |
|
19
|
|
|
use Zend\Console\Prompt\Confirm; |
|
20
|
|
|
use Zend\ProgressBar\ProgressBar; |
|
21
|
|
|
use ZF\Console\Route; |
|
22
|
|
|
|
|
23
|
|
|
abstract class AbstractCommand |
|
24
|
|
|
{ |
|
25
|
|
|
const CONFIG_FILE = 'phpoole.yml'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var Console |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $console; |
|
31
|
|
|
/** |
|
32
|
|
|
* @var Route |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $route; |
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $path; |
|
39
|
|
|
/** |
|
40
|
|
|
* @var PHPoole |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $phpoole; |
|
43
|
|
|
/** |
|
44
|
|
|
* @var Filesystem |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $fs; |
|
47
|
|
|
/** |
|
48
|
|
|
* @var ProgressBar |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $progressBar = null; |
|
51
|
|
|
/** |
|
52
|
|
|
* @var int |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $progressBarMax = 0; |
|
55
|
|
|
/** |
|
56
|
|
|
* @var bool |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $verbose = false; |
|
59
|
|
|
/** |
|
60
|
|
|
* @var bool |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $quiet = false; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Start command processing. |
|
66
|
|
|
* |
|
67
|
|
|
* @param Route $route |
|
68
|
|
|
* @param Console $console |
|
69
|
|
|
* |
|
70
|
|
|
* @return mixed |
|
71
|
|
|
*/ |
|
72
|
|
|
public function __invoke(Route $route, Console $console) |
|
73
|
|
|
{ |
|
74
|
|
|
$this->route = $route; |
|
75
|
|
|
$this->console = $console; |
|
76
|
|
|
$this->fs = new Filesystem(); |
|
77
|
|
|
|
|
78
|
|
|
$this->path = $this->route->getMatchedParam('path', getcwd()); |
|
79
|
|
|
|
|
80
|
|
|
if (realpath($this->path) === false) { |
|
81
|
|
|
if ($this->getRoute()->getName() != 'new') { |
|
82
|
|
|
throw new \Exception('Invalid <path> provided!'); |
|
83
|
|
|
} |
|
84
|
|
|
if (!Confirm::prompt('The provided <path> doesn\'t exist. Do you want to create it? [y/n]', 'y', 'n')) { |
|
85
|
|
|
exit(0); |
|
86
|
|
|
} |
|
87
|
|
|
$this->fs->mkdir($this->path); |
|
88
|
|
|
} |
|
89
|
|
|
$this->path = realpath($this->path); |
|
90
|
|
|
$this->path = str_replace(DIRECTORY_SEPARATOR, '/', $this->path); |
|
91
|
|
|
|
|
92
|
|
|
return $this->processCommand(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Process the command. |
|
97
|
|
|
*/ |
|
98
|
|
|
abstract public function processCommand(); |
|
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return Console |
|
102
|
|
|
*/ |
|
103
|
|
|
public function getConsole() |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->console; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @return Route |
|
110
|
|
|
*/ |
|
111
|
|
|
public function getRoute() |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->route; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return string |
|
118
|
|
|
*/ |
|
119
|
|
|
public function getPath() |
|
120
|
|
|
{ |
|
121
|
|
|
return $this->path; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param int $start |
|
126
|
|
|
* @param int $max |
|
127
|
|
|
* |
|
128
|
|
|
* @return ProgressBar |
|
129
|
|
|
*/ |
|
130
|
|
|
protected function createProgressBar($start, $max) |
|
131
|
|
|
{ |
|
132
|
|
|
if ($this->progressBar === null || $max != $this->progressBarMax) { |
|
133
|
|
|
$this->progressBarMax = $max; |
|
134
|
|
|
$adapter = new \Zend\ProgressBar\Adapter\Console([ |
|
135
|
|
|
'elements' => [ |
|
136
|
|
|
\Zend\ProgressBar\Adapter\Console::ELEMENT_PERCENT, |
|
137
|
|
|
\Zend\ProgressBar\Adapter\Console::ELEMENT_BAR, |
|
138
|
|
|
\Zend\ProgressBar\Adapter\Console::ELEMENT_TEXT, |
|
139
|
|
|
], |
|
140
|
|
|
'textWidth' => 30, |
|
141
|
|
|
]); |
|
142
|
|
|
$this->progressBar = new ProgressBar($adapter, $start, $max); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return $this->progressBar; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @return ProgressBar |
|
150
|
|
|
*/ |
|
151
|
|
|
protected function getProgressBar() |
|
152
|
|
|
{ |
|
153
|
|
|
return $this->progressBar; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Print progress bar. |
|
158
|
|
|
*/ |
|
159
|
|
|
protected function printProgressBar($itemsCount, $itemsMax, $message) |
|
160
|
|
|
{ |
|
161
|
|
|
$this->createProgressBar(0, $itemsMax); |
|
162
|
|
|
$this->getProgressBar()->update($itemsCount, "$message"); |
|
163
|
|
|
if ($itemsCount == $itemsMax) { |
|
164
|
|
|
$this->getProgressBar()->update($itemsCount, "[$itemsCount/$itemsMax]"); |
|
165
|
|
|
$this->getProgressBar()->finish(); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @param array $config |
|
171
|
|
|
* @param array $options |
|
172
|
|
|
* |
|
173
|
|
|
* @return PHPoole |
|
174
|
|
|
*/ |
|
175
|
|
|
public function getPHPoole( |
|
176
|
|
|
array $config = ['debug' => false], |
|
177
|
|
|
array $options = ['verbosity' => PHPoole::VERBOSITY_NORMAL]) |
|
178
|
|
|
{ |
|
179
|
|
|
if (!file_exists($this->getPath().'/'.self::CONFIG_FILE)) { |
|
180
|
|
|
throw new \Exception(sprintf('Config file not found in "%s"!', $this->getPath())); |
|
181
|
|
|
} |
|
182
|
|
|
// verbosity: verbose |
|
183
|
|
|
if ($options['verbosity'] == PHPoole::VERBOSITY_VERBOSE) { |
|
184
|
|
|
$this->verbose = true; |
|
185
|
|
|
} |
|
186
|
|
|
// verbosity: quiet |
|
187
|
|
|
if ($options['verbosity'] == PHPoole::VERBOSITY_QUIET) { |
|
188
|
|
|
$this->quiet = true; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
try { |
|
192
|
|
|
$configFile = Yaml::parse(file_get_contents($this->getPath().'/'.self::CONFIG_FILE)); |
|
193
|
|
|
$config = array_replace_recursive($configFile, $config); |
|
194
|
|
|
$this->phpoole = (new PHPoole($config, $this->messageCallback())) |
|
195
|
|
|
->setSourceDir($this->getPath()) |
|
196
|
|
|
->setDestinationDir($this->getPath()); |
|
197
|
|
|
} catch (ParseException $e) { |
|
198
|
|
|
throw new \Exception(sprintf('Config file parse error: %s', $e->getMessage())); |
|
199
|
|
|
} catch (\Exception $e) { |
|
200
|
|
|
throw new \Exception(sprintf($e->getMessage())); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
return $this->phpoole; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Custom message callback function. |
|
208
|
|
|
*/ |
|
209
|
|
|
public function messageCallback() |
|
210
|
|
|
{ |
|
211
|
|
|
return function ($code, $message = '', $itemsCount = 0, $itemsMax = 0) { |
|
212
|
|
|
if ($this->quiet) { |
|
213
|
|
|
return; |
|
214
|
|
|
} else { |
|
215
|
|
|
if (strpos($code, '_PROGRESS') !== false) { |
|
216
|
|
|
if ($this->verbose) { |
|
217
|
|
|
if ($itemsCount > 0) { |
|
218
|
|
|
$this->wlDone(sprintf('(%u/%u) %s', $itemsCount, $itemsMax, $message)); |
|
219
|
|
|
return; |
|
220
|
|
|
} |
|
221
|
|
|
$this->wlDone("$message"); |
|
222
|
|
|
} else { |
|
223
|
|
|
if (isset($itemsCount) && $itemsMax > 0) { |
|
224
|
|
|
$this->printProgressBar($itemsCount, $itemsMax, $message); |
|
225
|
|
|
} else { |
|
226
|
|
|
$this->wl($message); |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
} elseif (strpos($code, '_ERROR') !== false) { |
|
230
|
|
|
$this->wlError($message); |
|
231
|
|
|
} elseif ($code == 'TIME') { |
|
232
|
|
|
$this->wl($message); |
|
233
|
|
|
} else { |
|
234
|
|
|
$this->wlAnnonce($message); |
|
235
|
|
|
} |
|
236
|
|
|
} |
|
237
|
|
|
}; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* @param string $text |
|
242
|
|
|
*/ |
|
243
|
|
|
public function wl($text) |
|
244
|
|
|
{ |
|
245
|
|
|
$this->console->writeLine($text); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* @param string $text |
|
250
|
|
|
*/ |
|
251
|
|
|
public function wlAnnonce($text) |
|
252
|
|
|
{ |
|
253
|
|
|
$this->console->writeLine($text, Color::WHITE); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* @param string $text |
|
258
|
|
|
*/ |
|
259
|
|
|
public function wlDone($text) |
|
260
|
|
|
{ |
|
261
|
|
|
$this->console->writeLine($text, Color::GREEN); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* @param string $text |
|
266
|
|
|
*/ |
|
267
|
|
|
public function wlAlert($text) |
|
268
|
|
|
{ |
|
269
|
|
|
$this->console->writeLine($text, Color::YELLOW); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* @param string $text |
|
274
|
|
|
*/ |
|
275
|
|
|
public function wlError($text) |
|
276
|
|
|
{ |
|
277
|
|
|
$this->console->writeLine($text, Color::RED); |
|
278
|
|
|
} |
|
279
|
|
|
} |
|
280
|
|
|
|
For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a
@returndoc comment to communicate to implementors of these methods what they are expected to return.