1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace N98\Magento\Command\Config; |
4
|
|
|
|
5
|
|
|
use Magento\Config\Model\ResourceModel\Config\Data\Collection; |
6
|
|
|
use N98\Util\Console\Helper\Table\Renderer\RendererFactory; |
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
|
12
|
|
|
class GetCommand extends AbstractConfigCommand |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var Collection |
16
|
|
|
*/ |
17
|
|
|
private $collection; |
18
|
|
|
|
19
|
|
|
protected function configure() |
20
|
|
|
{ |
21
|
|
|
$this |
22
|
|
|
->setName('config:get') |
23
|
|
|
->setDescription('Get a core config item') |
24
|
|
|
->setHelp( |
25
|
|
|
<<<EOT |
26
|
|
|
If <info>path</info> is not set, all available config items will be listed. |
27
|
|
|
The <info>path</info> may contain wildcards (*). |
28
|
|
|
If <info>path</info> ends with a trailing slash, all child items will be listed. E.g. |
29
|
|
|
|
30
|
|
|
config:get web/ |
31
|
|
|
is the same as |
32
|
|
|
config:get web/* |
33
|
|
|
EOT |
34
|
|
|
) |
35
|
|
|
->addArgument('path', InputArgument::OPTIONAL, 'The config path') |
36
|
|
|
->addOption( |
37
|
|
|
'scope', |
38
|
|
|
null, |
39
|
|
|
InputOption::VALUE_REQUIRED, |
40
|
|
|
'The config value\'s scope (default, websites, stores)' |
41
|
|
|
) |
42
|
|
|
->addOption('scope-id', null, InputOption::VALUE_REQUIRED, 'The config value\'s scope ID') |
43
|
|
|
->addOption( |
44
|
|
|
'decrypt', |
45
|
|
|
null, |
46
|
|
|
InputOption::VALUE_NONE, |
47
|
|
|
'Decrypt the config value using env.php\'s crypt key' |
48
|
|
|
) |
49
|
|
|
->addOption('update-script', null, InputOption::VALUE_NONE, 'Output as update script lines') |
50
|
|
|
->addOption('magerun-script', null, InputOption::VALUE_NONE, 'Output for usage with config:set') |
51
|
|
|
->addOption( |
52
|
|
|
'format', |
53
|
|
|
null, |
54
|
|
|
InputOption::VALUE_OPTIONAL, |
55
|
|
|
'Output Format. One of [' . implode(',', RendererFactory::getFormats()) . ']' |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
$help = <<<HELP |
59
|
|
|
If path is not set, all available config items will be listed. path may contain wildcards (*) |
60
|
|
|
HELP; |
61
|
|
|
$this->setHelp($help); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param Collection $collection |
66
|
|
|
*/ |
67
|
|
|
public function inject(Collection $collection) |
68
|
|
|
{ |
69
|
|
|
$this->collection = $collection; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param InputInterface $input |
74
|
|
|
* @param OutputInterface $output |
75
|
|
|
* @return int|void |
76
|
|
|
*/ |
77
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
78
|
|
|
{ |
79
|
|
|
$collection = $this->collection; |
80
|
|
|
|
81
|
|
|
$searchPath = $input->getArgument('path'); |
82
|
|
|
|
83
|
|
|
if (substr($input->getArgument('path'), -1, 1) === '/') { |
84
|
|
|
$searchPath .= '*'; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$collection->addFieldToFilter('path', array( |
88
|
|
|
'like' => str_replace('*', '%', $searchPath), |
89
|
|
|
)); |
90
|
|
|
|
91
|
|
|
if ($scope = $input->getOption('scope')) { |
92
|
|
|
$collection->addFieldToFilter('scope', array('eq' => $scope)); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if ($scopeId = $input->getOption('scope-id')) { |
96
|
|
|
$collection->addFieldToFilter( |
97
|
|
|
'scope_id', |
98
|
|
|
array('eq' => $scopeId) |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$collection->addOrder('path', 'ASC'); |
103
|
|
|
|
104
|
|
|
// sort according to the config overwrite order |
105
|
|
|
// trick to force order default -> (f)website -> store , because f comes after d and before s |
106
|
|
|
$collection->addOrder('REPLACE(scope, "website", "fwebsite")', 'ASC'); |
107
|
|
|
|
108
|
|
|
$collection->addOrder('scope_id', 'ASC'); |
109
|
|
|
|
110
|
|
|
if ($collection->count() == 0) { |
111
|
|
|
$output->writeln(sprintf("Couldn't find a config value for \"%s\"", $input->getArgument('path'))); |
112
|
|
|
|
113
|
|
|
return; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
foreach ($collection as $item) { |
117
|
|
|
$table[] = array( |
|
|
|
|
118
|
|
|
'path' => $item->getPath(), |
119
|
|
|
'scope' => $item->getScope(), |
120
|
|
|
'scope_id' => $item->getScopeId(), |
121
|
|
|
'value' => $this->_formatValue( |
122
|
|
|
$item->getValue(), |
123
|
|
|
$input->getOption('decrypt') ? 'decrypt' : '' |
124
|
|
|
), |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
ksort($table); |
129
|
|
|
|
130
|
|
|
if ($input->getOption('update-script')) { |
131
|
|
|
$this->renderAsUpdateScript($output, $table); |
132
|
|
|
} elseif ($input->getOption('magerun-script')) { |
133
|
|
|
$this->renderAsMagerunScript($output, $table); |
134
|
|
|
} else { |
135
|
|
|
$this->renderAsTable($output, $table, $input->getOption('format')); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param OutputInterface $output |
141
|
|
|
* @param array $table |
142
|
|
|
* @param string $format |
143
|
|
|
*/ |
144
|
|
|
protected function renderAsTable(OutputInterface $output, $table, $format) |
145
|
|
|
{ |
146
|
|
|
$formattedTable = array(); |
147
|
|
|
foreach ($table as $row) { |
148
|
|
|
$formattedTable[] = array( |
149
|
|
|
$row['path'], |
150
|
|
|
$row['scope'], |
151
|
|
|
$row['scope_id'], |
152
|
|
|
$this->renderTableValue($row['value'], $format), |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/* @var $tableHelper \N98\Util\Console\Helper\TableHelper */ |
157
|
|
|
$tableHelper = $this->getHelper('table'); |
158
|
|
|
$tableHelper |
159
|
|
|
->setHeaders(array('Path', 'Scope', 'Scope-ID', 'Value')) |
160
|
|
|
->setRows($formattedTable) |
161
|
|
|
->renderByFormat($output, $formattedTable, $format); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
private function renderTableValue($value, $format) |
|
|
|
|
165
|
|
|
{ |
166
|
|
|
if ($value === null) { |
167
|
|
|
switch ($format) { |
168
|
|
|
case null: |
169
|
|
|
$value = self::DISPLAY_NULL_UNKOWN_VALUE; |
170
|
|
|
break; |
171
|
|
|
case 'json': |
172
|
|
|
break; |
173
|
|
|
case 'csv': |
174
|
|
|
case 'xml': |
175
|
|
|
$value = 'NULL'; |
176
|
|
|
break; |
177
|
|
|
default: |
178
|
|
|
throw new \UnexpectedValueException( |
179
|
|
|
sprintf("Unhandled format %s", var_export($value, true)) |
180
|
|
|
); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return $value; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param OutputInterface $output |
189
|
|
|
* @param array $table |
190
|
|
|
*/ |
191
|
|
|
protected function renderAsUpdateScript(OutputInterface $output, $table) |
192
|
|
|
{ |
193
|
|
|
$output->writeln('<?php'); |
194
|
|
|
$output->writeln('$installer = $this;'); |
195
|
|
|
$output->writeln('# generated by n98-magerun'); |
196
|
|
|
|
197
|
|
|
foreach ($table as $row) { |
198
|
|
|
if ($row['scope'] == 'default') { |
199
|
|
|
$output->writeln( |
200
|
|
|
sprintf( |
201
|
|
|
'$installer->setConfigData(%s, %s);', |
202
|
|
|
var_export($row['path'], true), |
203
|
|
|
var_export($row['value'], true) |
204
|
|
|
) |
205
|
|
|
); |
206
|
|
|
} else { |
207
|
|
|
$output->writeln( |
208
|
|
|
sprintf( |
209
|
|
|
'$installer->setConfigData(%s, %s, %s, %s);', |
210
|
|
|
var_export($row['path'], true), |
211
|
|
|
var_export($row['value'], true), |
212
|
|
|
var_export($row['scope'], true), |
213
|
|
|
var_export($row['scope_id'], true) |
214
|
|
|
) |
215
|
|
|
); |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @param OutputInterface $output |
222
|
|
|
* @param array $table |
223
|
|
|
*/ |
224
|
|
|
protected function renderAsMagerunScript(OutputInterface $output, $table) |
225
|
|
|
{ |
226
|
|
|
foreach ($table as $row) { |
227
|
|
|
$value = $row['value']; |
228
|
|
|
if ($value !== null) { |
229
|
|
|
$value = str_replace(array("\n", "\r"), array('\n', '\r'), $value); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
$disaplayValue = $value === null ? "NULL" : escapeshellarg($value); |
233
|
|
|
$protectNullString = $value === "NULL" ? '--no-null ' : ''; |
234
|
|
|
|
235
|
|
|
$line = sprintf( |
236
|
|
|
'config:set %s--scope-id=%s --scope=%s -- %s %s', |
237
|
|
|
$protectNullString, |
238
|
|
|
$row['scope_id'], |
239
|
|
|
$row['scope'], |
240
|
|
|
escapeshellarg($row['path']), |
241
|
|
|
$disaplayValue |
242
|
|
|
); |
243
|
|
|
$output->writeln($line); |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.