1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package CLI |
5
|
|
|
* @author Iurii Makukh <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2018, Iurii Makukh <[email protected]> |
7
|
|
|
* @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+ |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace gplcart\modules\cli\controllers; |
11
|
|
|
|
12
|
|
|
use gplcart\core\CliController; |
13
|
|
|
use gplcart\core\traits\Listing as ListingTrait; |
14
|
|
|
use InvalidArgumentException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Parent controller |
18
|
|
|
*/ |
19
|
|
|
class Command extends CliController |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
use ListingTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Output formatted data |
26
|
|
|
* @param mixed $data |
27
|
|
|
* @param null|string $default |
28
|
|
|
* @return null |
29
|
|
|
*/ |
30
|
|
|
protected function outputFormat($data, $default = null) |
31
|
|
|
{ |
32
|
|
|
switch ($this->getParam(array('f'), $default)) { |
33
|
|
|
case 'print-r': |
34
|
|
|
$this->line(print_r($data, true)); |
35
|
|
|
break; |
36
|
|
|
case 'var-export': |
37
|
|
|
$this->line(var_export($data, true)); |
38
|
|
|
break; |
39
|
|
|
case 'json': |
40
|
|
|
$this->line(json_encode($data, JSON_PRETTY_PRINT)); |
41
|
|
|
break; |
42
|
|
|
case 'var-dump': |
43
|
|
|
ob_start(); |
44
|
|
|
var_dump($data); |
|
|
|
|
45
|
|
|
$this->line(ob_get_clean()); |
46
|
|
|
break; |
47
|
|
|
default: |
48
|
|
|
return null; // Pass to table formatter |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$this->output(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Output simple table |
56
|
|
|
* @param mixed $data |
57
|
|
|
* @param array $header |
58
|
|
|
*/ |
59
|
|
|
protected function outputFormatTable($data, array $header) |
60
|
|
|
{ |
61
|
|
|
if (!is_array($data)) { |
62
|
|
|
$this->line(print_r($data, true)); |
63
|
|
|
$this->output(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
foreach ($data as &$row) { |
|
|
|
|
67
|
|
|
|
68
|
|
|
if (!is_array($row)) { |
69
|
|
|
$this->errorAndExit($this->text('Unexpected table row format')); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
ksort($row); |
73
|
|
|
|
74
|
|
|
foreach ($row as &$item) { |
75
|
|
|
if (is_array($item)) { |
76
|
|
|
$item = '-- Array ' . count($item) . ' items --'; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if (empty($data)) { |
82
|
|
|
$this->line($this->text('No results')); |
83
|
|
|
} else { |
84
|
|
|
array_unshift($data, $header); |
85
|
|
|
$this->table($data); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$this->output(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Returns an array of limits form the limit option or a default value |
93
|
|
|
* @param array $default_limit |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
|
|
protected function getLimit(array $default_limit = array()) |
97
|
|
|
{ |
98
|
|
|
if (empty($default_limit)) { |
99
|
|
|
$default_limit = $this->config->get('module_cli_limit', array(0, 100)); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$limit = $this->getParam('l'); |
103
|
|
|
|
104
|
|
|
if (!isset($limit) || !is_numeric($limit)) { |
105
|
|
|
return $default_limit; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$exploded = explode(',', $limit, 2); |
109
|
|
|
|
110
|
|
|
if (count($exploded) == 1) { |
111
|
|
|
array_unshift($exploded, 0); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $exploded; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Limits an array of items |
119
|
|
|
* @param array $array |
120
|
|
|
* @param array $default_limit |
121
|
|
|
*/ |
122
|
|
|
protected function limitArray(&$array, array $default_limit = array()) |
123
|
|
|
{ |
124
|
|
|
if (is_array($array)) { |
125
|
|
|
$this->limitList($array, $this->getLimit($default_limit)); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Explode a string using a separator character |
131
|
|
|
* @param string $value |
132
|
|
|
* @param string|null $separator |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
|
|
protected function explodeList($value, $separator = null) |
136
|
|
|
{ |
137
|
|
|
if (is_array($value)) { |
138
|
|
|
return $value; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
if ($value === '' || !is_string($value)) { |
142
|
|
|
return array(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
if (!isset($separator)) { |
146
|
|
|
$separator = $this->config->get('module_cli_list_separator', '|'); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return array_map('trim', explode($separator, $value)); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Convert a submitted JSON string into an array or FALSE on error |
154
|
|
|
* @param string $field |
155
|
|
|
*/ |
156
|
|
|
protected function setSubmittedJson($field) |
157
|
|
|
{ |
158
|
|
|
$json = $this->getSubmitted($field); |
159
|
|
|
|
160
|
|
|
if (isset($json)) { |
161
|
|
|
$decoded = json_decode($json, true); |
162
|
|
|
if (json_last_error() === JSON_ERROR_NONE) { |
163
|
|
|
$this->setSubmitted($field, $decoded); |
164
|
|
|
} else { |
165
|
|
|
$decoded = json_decode(base64_decode($json), true); |
166
|
|
|
if (json_last_error() !== JSON_ERROR_NONE) { |
167
|
|
|
throw new InvalidArgumentException('Failed to decode the submitted JSON string'); |
168
|
|
|
} |
169
|
|
|
$this->setSubmitted($field, $decoded); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Converts a submitted string into an array using a character as an separator |
176
|
|
|
* @param $field |
177
|
|
|
*/ |
178
|
|
|
protected function setSubmittedList($field) |
179
|
|
|
{ |
180
|
|
|
$value = $this->getSubmitted($field); |
181
|
|
|
|
182
|
|
|
if (isset($value)) { |
183
|
|
|
$this->setSubmitted($field, $this->explodeList($value)); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Validate prompt input when dealing with multiple values separated by a list character |
189
|
|
|
* @param string $field |
190
|
|
|
* @param string $label |
191
|
|
|
* @param string $validator |
192
|
|
|
* @param null|string $default |
193
|
|
|
*/ |
194
|
|
|
protected function validatePromptList($field, $label, $validator, $default = null) |
195
|
|
|
{ |
196
|
|
|
$input = $this->prompt($label, $default); |
197
|
|
|
|
198
|
|
|
if ($this->isValidInput($this->explodeList($input), $field, $validator)) { |
199
|
|
|
$this->setSubmitted($field, $input); |
200
|
|
|
} else { |
201
|
|
|
$this->errors(); |
202
|
|
|
$this->validatePromptList($field, $label, $validator, $default); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
} |
207
|
|
|
|