1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* this file is part of pipelines */ |
4
|
|
|
|
5
|
|
|
namespace Ktomk\Pipelines\Utility; |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
use Exception; |
9
|
|
|
use Ktomk\Pipelines\File; |
10
|
|
|
use Ktomk\Pipelines\Step; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class FileShower |
14
|
|
|
* |
15
|
|
|
* Shows information about a file |
16
|
|
|
* |
17
|
|
|
* @package Ktomk\Pipelines\Utility |
18
|
|
|
*/ |
19
|
|
|
class FileShower |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var callable |
23
|
|
|
*/ |
24
|
|
|
private $output; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var File |
28
|
|
|
*/ |
29
|
|
|
private $file; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* FileInfo constructor. |
33
|
|
|
* @param callable $output |
34
|
|
|
* @param File $file |
35
|
|
|
*/ |
36
|
5 |
|
public function __construct($output, File $file) |
37
|
|
|
{ |
38
|
5 |
|
$this->output = $output; |
39
|
5 |
|
$this->file = $file; |
40
|
5 |
|
} |
41
|
|
|
|
42
|
1 |
|
public function showImages() |
43
|
|
|
{ |
44
|
1 |
|
$file = $this->file; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* step iterator |
48
|
|
|
* |
49
|
|
|
* @param File $file |
50
|
|
|
* @return array|Step[] |
51
|
|
|
*/ |
52
|
1 |
|
$iter = function (File $file) { |
53
|
1 |
|
$ids = $file->getPipelineIds(); |
54
|
1 |
|
$return = array(); |
55
|
1 |
|
foreach ($ids as $id) { |
56
|
1 |
|
foreach ($file->getById($id)->getSteps() as $index => $step) { |
57
|
1 |
|
$return["$id:/step/$index"] = $step; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
return $return; |
62
|
1 |
|
}; |
63
|
|
|
|
64
|
1 |
|
$images = array(); |
65
|
1 |
|
foreach ($iter($file) as $step) { |
66
|
1 |
|
$image = $step->getImage(); |
67
|
1 |
|
$images[$image] = $image; |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
foreach ($images as $image) { |
71
|
1 |
|
$this->info($image); |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
return 0; |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
public function showPipelineIds() |
78
|
|
|
{ |
79
|
1 |
|
$file = $this->file; |
80
|
|
|
|
81
|
1 |
|
foreach ($file->getPipelineIds() as $id) { |
82
|
1 |
|
$this->info($id); |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
return 0; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return int 0 if there were no errors, 1 if there were errors |
90
|
|
|
*/ |
91
|
2 |
|
public function showPipelines() |
92
|
|
|
{ |
93
|
2 |
|
$pipelines = $this->file; |
94
|
|
|
|
95
|
2 |
|
$errors = 0; |
96
|
2 |
|
$table = array(array('PIPELINE ID', 'IMAGES', 'STEPS')); |
97
|
2 |
|
foreach ($pipelines->getPipelineIds() as $id) { |
98
|
|
|
|
99
|
|
|
try { |
100
|
2 |
|
$pipeline = $pipelines->getById($id); |
101
|
1 |
|
$steps = $pipeline->getSteps(); |
102
|
1 |
|
} catch (Exception $e) { |
103
|
1 |
|
$errors++; |
104
|
1 |
|
$table[] = array($id, 'ERROR', $e->getMessage()); |
105
|
1 |
|
continue; |
106
|
|
|
} |
107
|
|
|
|
108
|
1 |
|
list($images, $names) = $this->getImagesAndNames($steps); |
109
|
|
|
|
110
|
1 |
|
$images = $images ? implode(', ', $images) : ''; |
111
|
1 |
|
$steps = sprintf('%d%s', count($steps), $names ? ' ("' . implode('""; "', $names) . '")' : ''); |
112
|
1 |
|
$table[] = array($id, $images, $steps); |
113
|
|
|
} |
114
|
|
|
|
115
|
2 |
|
$this->textTable($table); |
116
|
|
|
|
117
|
2 |
|
return $errors ? 1 : 0; |
118
|
|
|
} |
119
|
|
|
|
120
|
2 |
|
private function textTable(array $table) |
121
|
|
|
{ |
122
|
2 |
|
$sizes = $this->textTableGetSizes($table); |
123
|
|
|
|
124
|
2 |
|
foreach ($table as $row) { |
125
|
2 |
|
$line = $this->textTableGetRow($row, $sizes); |
126
|
2 |
|
$this->info($line); |
127
|
|
|
} |
128
|
2 |
|
} |
129
|
|
|
|
130
|
4 |
|
private function info($message) |
131
|
|
|
{ |
132
|
4 |
|
call_user_func($this->output, $message); |
133
|
4 |
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param array|Step[] $steps |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
1 |
|
private function getImagesAndNames(array $steps) |
140
|
|
|
{ |
141
|
1 |
|
$images = array(); |
142
|
1 |
|
$names = array(); |
143
|
|
|
|
144
|
1 |
|
foreach ($steps as $step) { |
145
|
1 |
|
$image = $step->getImage(); |
146
|
1 |
|
if ($image !== File::DEFAULT_IMAGE) { |
147
|
1 |
|
$images[] = $image; |
148
|
|
|
} |
149
|
1 |
|
$name = $step->getName(); |
150
|
1 |
|
$name && $names[] = $name; |
151
|
|
|
} |
152
|
|
|
|
153
|
1 |
|
return array($images, $names); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* get max sizes for each column in array table |
158
|
|
|
* |
159
|
|
|
* @param array $table |
160
|
|
|
* @return array|int[] sizes |
161
|
|
|
*/ |
162
|
2 |
|
private function textTableGetSizes(array $table) |
163
|
|
|
{ |
164
|
2 |
|
$sizes = array(); |
165
|
2 |
|
foreach ($table[0] as $index => $cell) { |
166
|
2 |
|
$sizes[$index] = 0; |
167
|
|
|
} |
168
|
|
|
|
169
|
2 |
|
foreach ($table as $row) { |
170
|
2 |
|
foreach ($row as $index => $column) { |
171
|
2 |
|
$sizes[$index] = max($sizes[$index], strlen($column)); |
172
|
|
|
} |
173
|
|
|
} |
174
|
2 |
|
return $sizes; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param array|string[] $row |
179
|
|
|
* @param array|int[] $sizes |
180
|
|
|
* @return string |
181
|
|
|
*/ |
182
|
2 |
|
private function textTableGetRow(array $row, array $sizes) |
183
|
|
|
{ |
184
|
2 |
|
$line = ''; |
185
|
2 |
|
foreach ($row as $index => $column) { |
186
|
2 |
|
$len = strlen($column); |
187
|
2 |
|
$index && $line .= " "; |
188
|
2 |
|
$line .= $column; |
189
|
2 |
|
$line .= str_repeat(' ', $sizes[$index] - $len); |
190
|
|
|
} |
191
|
|
|
|
192
|
2 |
|
return $line; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|