1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rs\VersionEye\Output; |
4
|
|
|
|
5
|
|
|
use Rs\VersionEye\Http\Pager; |
6
|
|
|
use Symfony\Component\Console\Helper\Table; |
7
|
|
|
use Symfony\Component\Console\Helper\TableHelper; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* BaseOutput. |
12
|
|
|
* |
13
|
|
|
* @author Robert Schönthal <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
abstract class BaseOutput |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* prints a table, values can be modified via $callback. |
19
|
|
|
* |
20
|
|
|
* @param OutputInterface $output |
21
|
|
|
* @param string[] $headings |
22
|
|
|
* @param string[] $keys |
23
|
|
|
* @param array|Pager $data |
24
|
|
|
* @param \Closure $callback |
25
|
|
|
*/ |
26
|
13 |
|
protected function printTable(OutputInterface $output, array $headings, array $keys, $data, \Closure $callback = null) |
27
|
|
|
{ |
28
|
13 |
|
if (!class_exists('Symfony\Component\Console\Helper\Table')) { |
29
|
|
|
$table = new TableHelper(false); |
|
|
|
|
30
|
|
|
} else { |
31
|
13 |
|
$table = new Table($output); |
32
|
|
|
} |
33
|
|
|
|
34
|
13 |
|
$table->setHeaders($headings); |
35
|
|
|
|
36
|
13 |
|
foreach ($data as $row) { |
37
|
13 |
|
$rowData = array_merge(array_flip($keys), array_intersect_key($row, array_flip($keys))); |
38
|
13 |
|
if ($callback) { |
39
|
5 |
|
$rowData = array_map($callback, array_keys($rowData), $rowData); |
40
|
5 |
|
} |
41
|
13 |
|
$table->addRow($rowData); |
42
|
13 |
|
} |
43
|
|
|
|
44
|
13 |
|
$table->render($output); |
|
|
|
|
45
|
13 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* prints a simple boolean. |
49
|
|
|
* |
50
|
|
|
* @param OutputInterface $output |
51
|
|
|
* @param string $success |
52
|
|
|
* @param string $fail |
53
|
|
|
* @param bool $value |
54
|
|
|
* @param bool $line |
55
|
|
|
* |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
18 |
|
protected function printBoolean(OutputInterface $output, $success, $fail, $value, $line = true) |
59
|
|
|
{ |
60
|
18 |
|
if ($value) { |
61
|
14 |
|
$message = '<info>' . $success . '</info>'; |
62
|
14 |
|
} else { |
63
|
8 |
|
$message = '<error>' . $fail . '</error>'; |
64
|
|
|
} |
65
|
|
|
|
66
|
18 |
|
if (false === $line) { |
67
|
4 |
|
return $message; |
68
|
|
|
} |
69
|
|
|
|
70
|
14 |
|
$output->writeln($message); |
71
|
14 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* prints a list combined as <comment>Heading</comment> : <info>Value</info>, values can be modified via $callback. |
75
|
|
|
* |
76
|
|
|
* @param OutputInterface $output |
77
|
|
|
* @param string[] $headings |
78
|
|
|
* @param string[] $keys |
79
|
|
|
* @param array $data |
80
|
|
|
* @param \Closure $callback |
81
|
|
|
*/ |
82
|
10 |
|
protected function printList(OutputInterface $output, array $headings, array $keys, array $data, \Closure $callback = null) |
83
|
|
|
{ |
84
|
10 |
|
$width = $this->getColumnWidth($headings); |
85
|
10 |
|
$data = array_merge(array_flip($keys), array_intersect_key($data, array_flip($keys))); |
86
|
|
|
|
87
|
10 |
|
foreach ($headings as $key => $heading) { |
88
|
10 |
|
$value = array_values($data)[$key]; |
89
|
10 |
|
if ($callback) { |
90
|
5 |
|
$value = $callback($heading, $value); |
91
|
5 |
|
} |
92
|
10 |
|
$value = is_bool($value) ? (true === $value ? 'Yes' : 'No') : $value; |
93
|
|
|
|
94
|
10 |
|
$output->writeln(sprintf('<comment>%s%s</comment> : <info>%s</info>', $heading, str_repeat(' ', $width - strlen($heading)), $value)); |
95
|
10 |
|
} |
96
|
10 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* output for references/search api. |
100
|
|
|
* |
101
|
|
|
* @param OutputInterface $output |
102
|
|
|
* @param array|Pager $products |
103
|
|
|
*/ |
104
|
6 |
|
protected function printProducts(OutputInterface $output, $products) |
105
|
|
|
{ |
106
|
6 |
|
$this->printTable($output, |
107
|
6 |
|
['Name', 'Language', 'Version', 'Type'], |
108
|
6 |
|
['name', 'language', 'version', 'prod_type'], |
109
|
|
|
$products |
110
|
6 |
|
); |
111
|
6 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* prints a simple message. |
115
|
|
|
* |
116
|
|
|
* @param OutputInterface $output |
117
|
|
|
* @param array $response |
118
|
|
|
*/ |
119
|
3 |
|
protected function printMessage(OutputInterface $output, array $response) |
120
|
|
|
{ |
121
|
3 |
|
$this->printBoolean($output, $response['message'], $response['message'], true === $response['success']); |
122
|
3 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* calculates the max width of a given set of string. |
126
|
|
|
* |
127
|
|
|
* @param string[] $headings |
128
|
|
|
* |
129
|
|
|
* @return int |
130
|
|
|
*/ |
131
|
10 |
|
private function getColumnWidth(array $headings) |
132
|
|
|
{ |
133
|
10 |
|
$width = 0; |
134
|
10 |
|
foreach ($headings as $heading) { |
135
|
10 |
|
$width = strlen($heading) > $width ? strlen($heading) : $width; |
136
|
10 |
|
} |
137
|
|
|
|
138
|
10 |
|
return $width + 5; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.