1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* this file is part of pipelines */ |
4
|
|
|
|
5
|
|
|
namespace Ktomk\Pipelines\Utility\Show; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Ktomk\Pipelines\File\Definitions\Service; |
9
|
|
|
use Ktomk\Pipelines\File\File; |
10
|
|
|
use Ktomk\Pipelines\File\ParseException; |
11
|
|
|
use Ktomk\Pipelines\File\Pipeline\Step; |
12
|
|
|
use Ktomk\Pipelines\File\Pipeline\Steps; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class FileShower |
16
|
|
|
* |
17
|
|
|
* Shows information about a file |
18
|
|
|
* |
19
|
|
|
* @package Ktomk\Pipelines\Utility |
20
|
|
|
*/ |
21
|
|
|
class FileShower extends FileShowerAbstract |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @throws InvalidArgumentException |
25
|
|
|
* |
26
|
|
|
* @return int |
27
|
|
|
*/ |
28
|
2 |
|
public function showImages() |
29
|
|
|
{ |
30
|
2 |
|
$images = array(); |
31
|
|
|
|
32
|
2 |
|
foreach ($this->getAllStepsWithServices($this->file) as $step) { |
33
|
2 |
|
$image = $step->getImage(); |
34
|
2 |
|
$images[(string)$image] = $image; |
35
|
|
|
} |
36
|
|
|
|
37
|
2 |
|
foreach ($images as $image) { |
38
|
2 |
|
$this->info($image); |
39
|
|
|
} |
40
|
|
|
|
41
|
2 |
|
return 0; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return int |
46
|
|
|
*/ |
47
|
1 |
|
public function showPipelineIds() |
48
|
|
|
{ |
49
|
1 |
|
$pipelines = $this->file->getPipelines(); |
50
|
|
|
|
51
|
1 |
|
foreach ($pipelines->getPipelineIds() as $id) { |
52
|
1 |
|
$this->info($id); |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
return 0; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* shows pipeline and steps |
60
|
|
|
* |
61
|
|
|
* @return int 0 if there were no errors, 1 if there were errors |
62
|
|
|
*/ |
63
|
2 |
|
public function showFile() |
64
|
|
|
{ |
65
|
2 |
|
$pipelines = $this->file->getPipelines(); |
66
|
|
|
|
67
|
2 |
|
$errors = 0; |
68
|
2 |
|
$table = array(array('PIPELINE ID', 'STEP', 'IMAGE', 'NAME')); |
69
|
2 |
|
foreach ($this->tablePipelineIdsPipelines($pipelines, $table, $errors) as $id => $pipeline) { |
70
|
1 |
|
$steps = (null === $pipeline) ? array() : $pipeline->getSteps(); |
71
|
1 |
|
list($table, $errors) = $this->tableFileSteps($steps, $id, $table, $errors); |
72
|
|
|
} |
73
|
|
|
|
74
|
2 |
|
$this->textTable($table); |
75
|
|
|
|
76
|
2 |
|
return $errors ? 1 : 0; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* shows summary of the file, first pipelines then pipline services |
81
|
|
|
* |
82
|
|
|
* @return int 0 if there were no errors, 1 if there were errors |
83
|
|
|
*/ |
84
|
5 |
|
public function showPipelines() |
85
|
|
|
{ |
86
|
5 |
|
$pipelines = $this->file->getPipelines(); |
87
|
|
|
|
88
|
5 |
|
$errors = 0; |
89
|
5 |
|
$table = array(array('PIPELINE ID', 'IMAGES', 'STEPS')); |
90
|
5 |
|
foreach ($this->tablePipelineIdsPipelines($pipelines, $table, $errors) as $id => $pipeline) { |
91
|
2 |
|
$steps = (null === $pipeline) ? null : $pipeline->getSteps(); |
92
|
2 |
|
list($images, $names) = $this->getImagesAndNames($steps); |
93
|
|
|
|
94
|
2 |
|
$images = $images ? implode(', ', $images) : ''; |
95
|
2 |
|
$steps = sprintf('%d%s', count($steps), $names ? ' ("' . implode('"; "', $names) . '")' : ''); |
96
|
2 |
|
$table[] = array($id, $images, $steps); |
97
|
|
|
} |
98
|
|
|
|
99
|
5 |
|
$this->textTable($table); |
100
|
|
|
|
101
|
5 |
|
return $errors ? 1 : 0; |
102
|
|
|
} |
103
|
|
|
|
104
|
5 |
|
public function showServices() |
105
|
|
|
{ |
106
|
5 |
|
$file = $this->file; |
107
|
5 |
|
$pipelines = $file->getPipelines(); |
108
|
|
|
|
109
|
5 |
|
$errors = 0; |
110
|
5 |
|
$table = array(array('PIPELINE ID', 'STEP', 'SERVICE', 'IMAGE')); |
111
|
|
|
|
112
|
|
|
try { |
113
|
5 |
|
$serviceDefinitions = $file->getDefinitions()->getServices(); |
114
|
2 |
|
} catch (ParseException $e) { |
115
|
2 |
|
$table[] = array('', '', 'ERROR', $e->getParseMessage()); |
116
|
2 |
|
$this->textTable($table); |
117
|
|
|
|
118
|
2 |
|
return 1; |
119
|
|
|
} |
120
|
|
|
|
121
|
3 |
|
foreach ($this->tablePipelineIdsPipelines($pipelines, $table, $errors) as $id => $pipeline) { |
122
|
|
|
list($table, $errors) = |
123
|
2 |
|
$this->tableStepsServices($pipeline->getSteps(), $serviceDefinitions, $id, $table, $errors); |
124
|
|
|
} |
125
|
|
|
|
126
|
3 |
|
$this->textTable($table); |
127
|
|
|
|
128
|
3 |
|
return $errors ? 1 : 0; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param Step[]|Steps $steps |
133
|
|
|
* @param string $id |
134
|
|
|
* @param array $table |
135
|
|
|
* @param int $errors |
136
|
|
|
* |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
1 |
|
private function tableFileSteps($steps, $id, array $table, $errors) |
140
|
|
|
{ |
141
|
1 |
|
foreach ($steps as $index => $step) { |
142
|
|
|
/** @var Step $step */ |
143
|
1 |
|
$name = $step->getName(); |
144
|
1 |
|
null !== $name && $name = sprintf('"%s"', $name); |
145
|
1 |
|
null === $name && $name = 'no-name'; |
146
|
|
|
|
147
|
1 |
|
$table[] = array($id, $index + 1, $step->getImage(), $name); |
148
|
1 |
|
list($table, $errors) = $this->tableFileStepsServices( |
149
|
1 |
|
$step->getServices()->getServiceNames(), |
150
|
|
|
$step, |
151
|
|
|
$id, |
152
|
1 |
|
$index + 1, |
153
|
|
|
$table, |
154
|
|
|
$errors |
155
|
|
|
); |
156
|
|
|
} |
157
|
|
|
|
158
|
1 |
|
return array($table, $errors); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param array|string[] $serviceNames |
163
|
|
|
* @param Step $step |
164
|
|
|
* @param string $id |
165
|
|
|
* @param int $stepNumber 1 based |
166
|
|
|
* @param array $table |
167
|
|
|
* @param int $errors |
168
|
|
|
* |
169
|
|
|
* @return array |
170
|
|
|
*/ |
171
|
1 |
|
private function tableFileStepsServices($serviceNames, Step $step, $id, $stepNumber, array $table, $errors) |
172
|
|
|
{ |
173
|
1 |
|
foreach ($serviceNames as $serviceName) { |
174
|
|
|
/** @var Service $service */ |
175
|
1 |
|
($service = $step->getFile()->getDefinitions()->getServices()->getByName($serviceName)) || $errors++; |
176
|
1 |
|
$table[] = array($id, $stepNumber, $service ? $service->getImage() : 'ERROR', 'service:' . $serviceName); |
177
|
|
|
} |
178
|
|
|
|
179
|
1 |
|
return array($table, $errors); |
180
|
|
|
} |
181
|
|
|
|
182
|
2 |
|
private function tableStepsServices(Steps $steps, $serviceDefinitions, $id, array $table, $errors) |
183
|
|
|
{ |
184
|
2 |
|
foreach ($steps as $step) { |
185
|
|
|
/** @var Step $step */ |
186
|
2 |
|
$serviceNames = $step->getServices()->getServiceNames(); |
187
|
2 |
|
if (!$serviceNames) { |
|
|
|
|
188
|
1 |
|
continue; |
189
|
|
|
} |
190
|
|
|
|
191
|
2 |
|
$stepNo = $step->getIndex() + 1; |
192
|
|
|
|
193
|
2 |
|
foreach ($serviceNames as $name) { |
194
|
2 |
|
if (!$service = $serviceDefinitions->getByName($name)) { |
195
|
1 |
|
$table[] = array($id, $stepNo, 'ERROR', sprintf('Undefined service: "%s"', $name)); |
196
|
1 |
|
$errors++; |
197
|
|
|
} else { |
198
|
2 |
|
$table[] = array($id, $stepNo, $name, $service->getImage()); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
2 |
|
return array($table, $errors); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param File $file |
208
|
|
|
* |
209
|
|
|
* @return Step[] |
210
|
|
|
*/ |
211
|
2 |
|
private function getAllSteps(File $file) |
212
|
|
|
{ |
213
|
2 |
|
$return = array(); |
214
|
2 |
|
foreach ($file->getPipelines()->getPipelines() as $id => $pipeline) { |
215
|
2 |
|
foreach (Steps::fullIter($pipeline->getSteps()) as $index => $step) { |
216
|
2 |
|
$return["${id}:/step/${index}"] = $step; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
220
|
2 |
|
return $return; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* step iterator w/services |
225
|
|
|
* |
226
|
|
|
* @param File $file |
227
|
|
|
* |
228
|
|
|
* @return Service[]|Step[] |
229
|
|
|
*/ |
230
|
|
|
private function getAllStepsWithServices(File $file) |
231
|
|
|
{ |
232
|
2 |
|
$return = array(); |
233
|
|
|
|
234
|
2 |
|
foreach ($this->getAllSteps($file) as $key => $step) { |
235
|
2 |
|
$return[$key] = $step; |
236
|
2 |
|
foreach ($step->getServices()->getDefinitions() as $name => $service) { |
237
|
1 |
|
$return["${key}/service/${name}"] = $service; |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
241
|
2 |
|
return $return; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @param Steps $steps |
246
|
|
|
* |
247
|
|
|
* @return array |
248
|
|
|
*/ |
249
|
|
|
private function getImagesAndNames(Steps $steps = null) |
250
|
|
|
{ |
251
|
2 |
|
$images = array(); |
252
|
2 |
|
$names = array(); |
253
|
|
|
|
254
|
2 |
|
foreach (Steps::fullIter($steps) as $step) { |
255
|
2 |
|
$image = $step->getImage()->getName(); |
256
|
2 |
|
if (File::DEFAULT_IMAGE !== $image) { |
257
|
2 |
|
$images[] = $image; |
258
|
|
|
} |
259
|
2 |
|
$name = $step->getName(); |
260
|
2 |
|
(null !== $name) && $names[] = $name; |
261
|
|
|
} |
262
|
|
|
|
263
|
2 |
|
$images = array_unique($images); |
264
|
|
|
|
265
|
2 |
|
return array($images, $names); |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.