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\File; |
9
|
|
|
use Ktomk\Pipelines\File\ParseException; |
10
|
|
|
use Ktomk\Pipelines\File\Pipelines; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class FileShowerAbstract |
14
|
|
|
* |
15
|
|
|
* Show details about the pipelines file on text output |
16
|
|
|
* |
17
|
|
|
* @package Ktomk\Pipelines\Utility\Show |
18
|
|
|
*/ |
19
|
|
|
class FileShowerAbstract |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var callable |
23
|
|
|
*/ |
24
|
|
|
protected $output; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var File |
28
|
|
|
*/ |
29
|
|
|
protected $file; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* FileInfo constructor. |
33
|
|
|
* |
34
|
|
|
* @param callable $output |
35
|
|
|
* @param File $file |
36
|
|
|
*/ |
37
|
18 |
|
public function __construct($output, File $file) |
38
|
|
|
{ |
39
|
18 |
|
$this->output = $output; |
40
|
18 |
|
$this->file = $file; |
41
|
18 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $message |
45
|
|
|
* |
46
|
|
|
* @return void |
47
|
|
|
*/ |
48
|
16 |
|
protected function info($message) |
49
|
|
|
{ |
50
|
16 |
|
call_user_func($this->output, $message); |
51
|
16 |
|
} |
52
|
|
|
|
53
|
|
|
/* table show routines */ |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param Pipelines $pipelines |
57
|
|
|
* @param FileTable $table |
58
|
|
|
* |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
12 |
|
protected function tablePipelineIdsPipelines(Pipelines $pipelines, FileTable $table) |
62
|
|
|
{ |
63
|
12 |
|
$return = array(); |
64
|
|
|
|
65
|
12 |
|
foreach ($pipelines->getPipelineIds() as $id) { |
66
|
12 |
|
list($pipeline, $message) = $this->getShowPipeline($pipelines, $id); |
67
|
12 |
|
$message ? $table->addErrorRow(array($id, 'ERROR', $message)) : $return[$id] = $pipeline; |
68
|
|
|
} |
69
|
|
|
|
70
|
12 |
|
return $return; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* output text table and return status |
75
|
|
|
* |
76
|
|
|
* @param FileTable $table |
77
|
|
|
* |
78
|
|
|
* @return int 1 if with errors, 0 if none |
79
|
|
|
*/ |
80
|
13 |
|
protected function outputTableAndReturn(FileTable $table) |
81
|
|
|
{ |
82
|
13 |
|
$this->textTable($table->toArray()); |
83
|
|
|
|
84
|
13 |
|
return $table->getErrors() ? 1 : 0; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/* text table implementation */ |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param array $table |
91
|
|
|
* |
92
|
|
|
* @return void |
93
|
|
|
*/ |
94
|
13 |
|
private function textTable(array $table) |
95
|
|
|
{ |
96
|
13 |
|
$sizes = $this->textTableGetSizes($table); |
97
|
|
|
|
98
|
13 |
|
foreach ($table as $row) { |
99
|
13 |
|
$line = $this->textTableGetRow($row, $sizes); |
100
|
13 |
|
$this->info($line); |
101
|
|
|
} |
102
|
13 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param Pipelines $pipelines |
106
|
|
|
* @param string $id |
107
|
|
|
* |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
12 |
|
private function getShowPipeline(Pipelines $pipelines, $id) |
111
|
|
|
{ |
112
|
12 |
|
$pipeline = null; |
|
|
|
|
113
|
12 |
|
$message = null; |
114
|
|
|
|
115
|
|
|
try { |
116
|
12 |
|
$pipeline = $pipelines->getById($id); |
117
|
5 |
|
} catch (ParseException $exception) { |
118
|
4 |
|
$message = $exception->getParseMessage(); |
119
|
1 |
|
} catch (InvalidArgumentException $exception) { |
120
|
1 |
|
$message = $exception->getMessage(); |
121
|
|
|
} |
122
|
|
|
|
123
|
12 |
|
return array($pipeline, $message); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* get max sizes for each column in array table |
128
|
|
|
* |
129
|
|
|
* @param array $table |
130
|
|
|
* |
131
|
|
|
* @return array|int[] sizes |
132
|
|
|
*/ |
133
|
13 |
|
private function textTableGetSizes(array $table) |
134
|
|
|
{ |
135
|
13 |
|
$sizes = array(); |
136
|
13 |
|
foreach ($table[0] as $index => $cell) { |
137
|
13 |
|
$sizes[$index] = 0; |
138
|
|
|
} |
139
|
|
|
|
140
|
13 |
|
foreach ($table as $row) { |
141
|
13 |
|
foreach ($row as $index => $column) { |
142
|
13 |
|
$sizes[$index] = max($sizes[$index], strlen($column)); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
13 |
|
return $sizes; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param array|string[] $row |
151
|
|
|
* @param array|int[] $sizes |
152
|
|
|
* |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
13 |
|
private function textTableGetRow(array $row, array $sizes) |
156
|
|
|
{ |
157
|
13 |
|
$line = ''; |
158
|
13 |
|
foreach ($row as $index => $column) { |
159
|
13 |
|
$len = strlen($column); |
160
|
13 |
|
$index && $line .= ' '; |
161
|
13 |
|
$line .= $column; |
162
|
13 |
|
$line .= str_repeat(' ', $sizes[$index] - $len); |
163
|
|
|
} |
164
|
|
|
|
165
|
13 |
|
return rtrim($line); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|