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 ZF\Console\Route; |
20
|
|
|
|
21
|
|
|
abstract class AbstractCommand |
22
|
|
|
{ |
23
|
|
|
const CONFIG_FILE = 'phpoole.yml'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Console |
27
|
|
|
*/ |
28
|
|
|
protected $console; |
29
|
|
|
/** |
30
|
|
|
* @var Route |
31
|
|
|
*/ |
32
|
|
|
protected $route; |
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $path; |
37
|
|
|
/** |
38
|
|
|
* @var PHPoole |
39
|
|
|
*/ |
40
|
|
|
protected $phpoole; |
41
|
|
|
/** |
42
|
|
|
* @var Filesystem |
43
|
|
|
*/ |
44
|
|
|
protected $fs; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Start command processing. |
48
|
|
|
* |
49
|
|
|
* @param Route $route |
50
|
|
|
* @param Console $console |
51
|
|
|
* |
52
|
|
|
* @return mixed |
53
|
|
|
*/ |
54
|
|
|
public function __invoke(Route $route, Console $console) |
55
|
|
|
{ |
56
|
|
|
$this->route = $route; |
57
|
|
|
$this->console = $console; |
58
|
|
|
|
59
|
|
|
$this->path = realpath($this->route->getMatchedParam('path', getcwd())); |
60
|
|
|
if (!is_dir($this->path)) { |
61
|
|
|
throw new \Exception('Invalid <path> provided!'); |
62
|
|
|
exit(2); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
$this->path = str_replace(DIRECTORY_SEPARATOR, '/', $this->path); |
65
|
|
|
|
66
|
|
|
$this->fs = new Filesystem(); |
67
|
|
|
|
68
|
|
|
return $this->processCommand(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Process the command. |
73
|
|
|
*/ |
74
|
|
|
abstract public function processCommand(); |
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return Console |
78
|
|
|
*/ |
79
|
|
|
public function getConsole() |
80
|
|
|
{ |
81
|
|
|
return $this->console; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return Route |
86
|
|
|
*/ |
87
|
|
|
public function getRoute() |
88
|
|
|
{ |
89
|
|
|
return $this->route; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
public function getPath() |
96
|
|
|
{ |
97
|
|
|
return $this->path; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param array $options |
102
|
|
|
* |
103
|
|
|
* @return PHPoole |
|
|
|
|
104
|
|
|
*/ |
105
|
|
|
public function getPHPoole(array $options = []) |
106
|
|
|
{ |
107
|
|
|
$messageCallback = function ($code, $message = '', $itemsCount = 0, $itemsMax = 0, $verbose = true) { |
108
|
|
|
switch (true) { |
109
|
|
|
case $code == 'CREATE' |
110
|
|
|
|| $code == 'CONVERT' |
111
|
|
|
|| $code == 'GENERATE' |
112
|
|
|
|| $code == 'RENDER' |
113
|
|
|
|| $code == 'COPY': |
114
|
|
|
$this->wlAnnonce($message); |
115
|
|
|
break; |
116
|
|
|
case $code == 'CREATE_PROGRESS' |
117
|
|
|
|| $code == 'CONVERT_PROGRESS' |
118
|
|
|
|| $code == 'GENERATE_PROGRESS' |
119
|
|
|
|| $code == 'RENDER_PROGRESS' |
120
|
|
|
|| $code == 'COPY_PROGRESS': |
121
|
|
|
if ($itemsCount > 0 && $verbose !== false) { |
122
|
|
|
$this->wlDone(sprintf("\r (%u/%u) %s", $itemsCount, $itemsMax, $message)); |
123
|
|
|
break; |
124
|
|
|
} |
125
|
|
|
$this->wlDone(" $message"); |
126
|
|
|
break; |
127
|
|
|
} |
128
|
|
|
}; |
129
|
|
|
|
130
|
|
|
if (!$this->phpoole instanceof PHPoole) { |
131
|
|
|
if (!file_exists($this->getPath().'/'.self::CONFIG_FILE)) { |
132
|
|
|
throw new \Exception(sprintf('Config file not found in "%s"!', $this->getPath())); |
133
|
|
|
exit(2); |
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
try { |
137
|
|
|
$optionsFile = Yaml::parse(file_get_contents($this->getPath().'/'.self::CONFIG_FILE)); |
138
|
|
|
if (is_array($options)) { |
139
|
|
|
$options = array_replace_recursive($optionsFile, $options); |
140
|
|
|
} |
141
|
|
|
$this->phpoole = new PHPoole($options, $messageCallback); |
142
|
|
|
$this->phpoole->setSourceDir($this->getPath()); |
143
|
|
|
$this->phpoole->setDestinationDir($this->getPath()); |
144
|
|
|
} catch (ParseException $e) { |
145
|
|
|
throw new \Exception(sprintf('Config file parse error: %s', $e->getMessage())); |
146
|
|
|
exit(1); |
|
|
|
|
147
|
|
|
} catch (\Exception $e) { |
148
|
|
|
throw new \Exception(sprintf($e->getMessage())); |
149
|
|
|
exit(1); |
|
|
|
|
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $this->phpoole; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param $text |
158
|
|
|
*/ |
159
|
|
|
public function wlAnnonce($text) |
160
|
|
|
{ |
161
|
|
|
$this->console->writeLine($text, Color::WHITE); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param $text |
166
|
|
|
*/ |
167
|
|
|
public function wlDone($text) |
168
|
|
|
{ |
169
|
|
|
$this->console->writeLine($text, Color::GREEN); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param $text |
174
|
|
|
*/ |
175
|
|
|
public function wlAlert($text) |
176
|
|
|
{ |
177
|
|
|
$this->console->writeLine($text, Color::YELLOW); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param $text |
182
|
|
|
*/ |
183
|
|
|
public function wlError($text) |
184
|
|
|
{ |
185
|
|
|
$this->console->writeLine($text, Color::RED); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.