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