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\ProgressBar\ProgressBar; |
20
|
|
|
use ZF\Console\Route; |
21
|
|
|
|
22
|
|
|
abstract class AbstractCommand |
23
|
|
|
{ |
24
|
|
|
const CONFIG_FILE = 'phpoole.yml'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Console |
28
|
|
|
*/ |
29
|
|
|
protected $console; |
30
|
|
|
/** |
31
|
|
|
* @var Route |
32
|
|
|
*/ |
33
|
|
|
protected $route; |
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $path; |
38
|
|
|
/** |
39
|
|
|
* @var PHPoole |
40
|
|
|
*/ |
41
|
|
|
protected $phpoole; |
42
|
|
|
/** |
43
|
|
|
* @var Filesystem |
44
|
|
|
*/ |
45
|
|
|
protected $fs; |
46
|
|
|
/** |
47
|
|
|
* @var ProgressBar |
48
|
|
|
*/ |
49
|
|
|
protected $progressBar = null; |
50
|
|
|
/** |
51
|
|
|
* @var int |
52
|
|
|
*/ |
53
|
|
|
protected $pbMax = 0; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Start command processing. |
57
|
|
|
* |
58
|
|
|
* @param Route $route |
59
|
|
|
* @param Console $console |
60
|
|
|
* |
61
|
|
|
* @return mixed |
62
|
|
|
*/ |
63
|
|
|
public function __invoke(Route $route, Console $console) |
64
|
|
|
{ |
65
|
|
|
$this->route = $route; |
66
|
|
|
$this->console = $console; |
67
|
|
|
|
68
|
|
|
$this->path = realpath($this->route->getMatchedParam('path', getcwd())); |
69
|
|
|
if (!is_dir($this->path)) { |
70
|
|
|
throw new \Exception('Invalid <path> provided!'); |
71
|
|
|
} |
72
|
|
|
$this->path = str_replace(DIRECTORY_SEPARATOR, '/', $this->path); |
73
|
|
|
|
74
|
|
|
$this->fs = new Filesystem(); |
75
|
|
|
|
76
|
|
|
return $this->processCommand(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Process the command. |
81
|
|
|
*/ |
82
|
|
|
abstract public function processCommand(); |
|
|
|
|
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return Console |
86
|
|
|
*/ |
87
|
|
|
public function getConsole() |
88
|
|
|
{ |
89
|
|
|
return $this->console; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return Route |
94
|
|
|
*/ |
95
|
|
|
public function getRoute() |
96
|
|
|
{ |
97
|
|
|
return $this->route; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
public function getPath() |
104
|
|
|
{ |
105
|
|
|
return $this->path; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param int $start |
110
|
|
|
* @param int $max |
111
|
|
|
* |
112
|
|
|
* @return ProgressBar |
113
|
|
|
*/ |
114
|
|
|
protected function newPB($start, $max) |
115
|
|
|
{ |
116
|
|
|
if ($this->progressBar == null || $max != $this->pbMax) { |
117
|
|
|
$this->pbMax = $max; |
118
|
|
|
$adapter = new \Zend\ProgressBar\Adapter\Console([ |
119
|
|
|
'elements' => [ |
120
|
|
|
\Zend\ProgressBar\Adapter\Console::ELEMENT_PERCENT, |
121
|
|
|
\Zend\ProgressBar\Adapter\Console::ELEMENT_BAR, |
122
|
|
|
\Zend\ProgressBar\Adapter\Console::ELEMENT_TEXT |
123
|
|
|
]]); |
124
|
|
|
$this->progressBar = new ProgressBar($adapter, $start, $max); |
125
|
|
|
} |
126
|
|
|
return $this->progressBar; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return ProgressBar |
131
|
|
|
*/ |
132
|
|
|
protected function getPB() |
133
|
|
|
{ |
134
|
|
|
return $this->progressBar; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param array $options |
139
|
|
|
* |
140
|
|
|
* @return PHPoole |
141
|
|
|
*/ |
142
|
|
|
public function getPHPoole(array $options = []) |
143
|
|
|
{ |
144
|
|
|
$messageCallback = function ($code, $message = '', $itemsCount = 0, $itemsMax = 0, $verbose = true) { |
|
|
|
|
145
|
|
|
switch ($code) { |
146
|
|
|
case 'CREATE': |
147
|
|
|
case 'CONVERT': |
148
|
|
|
case 'GENERATE': |
149
|
|
|
case 'COPY': |
150
|
|
|
case 'RENDER': |
151
|
|
|
case 'TIME': |
152
|
|
|
$this->wlAnnonce($message); |
153
|
|
|
break; |
154
|
|
|
case 'CREATE_PROGRESS': |
155
|
|
|
case 'CONVERT_PROGRESS': |
156
|
|
|
case 'GENERATE_PROGRESS': |
157
|
|
|
case 'COPY_PROGRESS': |
158
|
|
|
case 'RENDER_PROGRESS': |
159
|
|
|
if ($itemsMax && $itemsCount) { |
160
|
|
|
$this->newPB(1, $itemsMax); |
161
|
|
|
$this->getPB()->update($itemsCount, "$message"); |
162
|
|
|
} else { |
163
|
|
|
$this->wl($message); |
164
|
|
|
} |
165
|
|
|
break; |
166
|
|
|
} |
167
|
|
|
}; |
168
|
|
|
|
169
|
|
|
if (!$this->phpoole instanceof PHPoole) { |
170
|
|
|
if (!file_exists($this->getPath().'/'.self::CONFIG_FILE)) { |
171
|
|
|
throw new \Exception(sprintf('Config file not found in "%s"!', $this->getPath())); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
try { |
175
|
|
|
$optionsFile = Yaml::parse(file_get_contents($this->getPath().'/'.self::CONFIG_FILE)); |
176
|
|
|
if (is_array($options)) { |
177
|
|
|
$options = array_replace_recursive($optionsFile, $options); |
178
|
|
|
} |
179
|
|
|
$this->phpoole = new PHPoole($options, $messageCallback); |
180
|
|
|
$this->phpoole->setSourceDir($this->getPath()); |
181
|
|
|
$this->phpoole->setDestinationDir($this->getPath()); |
182
|
|
|
} catch (ParseException $e) { |
183
|
|
|
throw new \Exception(sprintf('Config file parse error: %s', $e->getMessage())); |
184
|
|
|
} catch (\Exception $e) { |
185
|
|
|
throw new \Exception(sprintf($e->getMessage())); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return $this->phpoole; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param string $text |
194
|
|
|
*/ |
195
|
|
|
public function wl($text) |
196
|
|
|
{ |
197
|
|
|
$this->console->writeLine($text); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param string $text |
202
|
|
|
*/ |
203
|
|
|
public function wlAnnonce($text) |
204
|
|
|
{ |
205
|
|
|
$this->console->writeLine($text, Color::WHITE); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @param $text |
210
|
|
|
*/ |
211
|
|
|
public function wlDone($text) |
212
|
|
|
{ |
213
|
|
|
$this->console->writeLine($text, Color::GREEN); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @param string $text |
218
|
|
|
*/ |
219
|
|
|
public function wlAlert($text) |
220
|
|
|
{ |
221
|
|
|
$this->console->writeLine($text, Color::YELLOW); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @param $text |
226
|
|
|
*/ |
227
|
|
|
public function wlError($text) |
228
|
|
|
{ |
229
|
|
|
$this->console->writeLine($text, Color::RED); |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
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
@return
doc comment to communicate to implementors of these methods what they are expected to return.