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\Output; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
|
13
|
|
|
class GetCommand extends AbstractConfigCommand |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var Collection |
17
|
|
|
*/ |
18
|
|
|
private $collection; |
19
|
|
|
|
20
|
|
|
protected function configure() |
21
|
|
|
{ |
22
|
|
|
$this |
23
|
|
|
->setName('config:get') |
24
|
|
|
->setDescription('Get a core config item') |
25
|
|
|
->setHelp( |
26
|
|
|
<<<EOT |
27
|
|
|
If <info>path</info> is not set, all available config items will be listed. |
28
|
|
|
The <info>path</info> may contain wildcards (*). |
29
|
|
|
If <info>path</info> ends with a trailing slash, all child items will be listed. E.g. |
30
|
|
|
|
31
|
|
|
config:get web/ |
32
|
|
|
is the same as |
33
|
|
|
config:get web/* |
34
|
|
|
EOT |
35
|
|
|
) |
36
|
|
|
->addArgument('path', InputArgument::OPTIONAL, 'The config path') |
37
|
|
|
->addOption('scope', null, InputOption::VALUE_REQUIRED, 'The config value\'s scope') |
38
|
|
|
->addOption('scope-id', null, InputOption::VALUE_REQUIRED, 'The config value\'s scope ID') |
39
|
|
|
->addOption( |
40
|
|
|
'decrypt', |
41
|
|
|
null, |
42
|
|
|
InputOption::VALUE_NONE, |
43
|
|
|
'Decrypt the config value using local.xml\'s crypt key' |
44
|
|
|
) |
45
|
|
|
->addOption('update-script', null, InputOption::VALUE_NONE, 'Output as update script lines') |
46
|
|
|
->addOption('magerun-script', null, InputOption::VALUE_NONE, 'Output for usage with config:set') |
47
|
|
|
->addOption( |
48
|
|
|
'format', |
49
|
|
|
null, |
50
|
|
|
InputOption::VALUE_OPTIONAL, |
51
|
|
|
'Output Format. One of [' . implode(',', RendererFactory::getFormats()) . ']' |
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
$help = <<<HELP |
55
|
|
|
If path is not set, all available config items will be listed. path may contain wildcards (*) |
56
|
|
|
HELP; |
57
|
|
|
$this->setHelp($help); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param Collection $collection |
62
|
|
|
*/ |
63
|
|
|
public function inject(Collection $collection) |
64
|
|
|
{ |
65
|
|
|
$this->collection = $collection; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param InputInterface $input |
70
|
|
|
* @param OutputInterface $output |
71
|
|
|
* @return int|void |
72
|
|
|
*/ |
73
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
74
|
|
|
{ |
75
|
|
|
$collection = $this->collection; |
76
|
|
|
|
77
|
|
|
$searchPath = $input->getArgument('path'); |
78
|
|
|
|
79
|
|
|
if (substr($input->getArgument('path'), -1, 1) === '/') { |
80
|
|
|
$searchPath .= '*'; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$collection->addFieldToFilter('path', array( |
84
|
|
|
'like' => str_replace('*', '%', $searchPath) |
85
|
|
|
)); |
86
|
|
|
|
87
|
|
|
if ($scopeId = $input->getOption('scope')) { |
88
|
|
|
$collection->addFieldToFilter('scope', array('eq' => $scopeId)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if ($scopeId = $input->getOption('scope-id')) { |
92
|
|
|
$collection->addFieldToFilter( |
93
|
|
|
'scope_id', |
94
|
|
|
array('eq' => $scopeId) |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$collection->addOrder('path', 'ASC'); |
99
|
|
|
|
100
|
|
|
// sort according to the config overwrite order |
101
|
|
|
// trick to force order default -> (f)website -> store , because f comes after d and before s |
102
|
|
|
$collection->addOrder('REPLACE(scope, "website", "fwebsite")', 'ASC'); |
103
|
|
|
|
104
|
|
|
$collection->addOrder('scope_id', 'ASC'); |
105
|
|
|
|
106
|
|
|
if ($collection->count() == 0) { |
107
|
|
|
$output->writeln(sprintf("Couldn't find a config value for \"%s\"", $input->getArgument('path'))); |
108
|
|
|
|
109
|
|
|
return; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
foreach ($collection as $item) { |
113
|
|
|
$table[] = array( |
|
|
|
|
114
|
|
|
'path' => $item->getPath(), |
115
|
|
|
'scope' => $item->getScope(), |
116
|
|
|
'scope_id' => $item->getScopeId(), |
117
|
|
|
'value' => $this->_formatValue( |
118
|
|
|
$item->getValue(), |
119
|
|
|
$input->getOption('decrypt') ? 'decrypt' : false |
|
|
|
|
120
|
|
|
), |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
ksort($table); |
125
|
|
|
|
126
|
|
|
if ($input->getOption('update-script')) { |
127
|
|
|
$this->renderAsUpdateScript($output, $table); |
128
|
|
|
} elseif ($input->getOption('magerun-script')) { |
129
|
|
|
$this->renderAsMagerunScript($output, $table); |
130
|
|
|
} else { |
131
|
|
|
$this->renderAsTable($output, $table, $input->getOption('format')); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param OutputInterface $output |
137
|
|
|
* @param array $table |
138
|
|
|
* @param string $format |
139
|
|
|
*/ |
140
|
|
|
protected function renderAsTable(OutputInterface $output, $table, $format) |
141
|
|
|
{ |
142
|
|
|
$formattedTable = array(); |
143
|
|
|
foreach ($table as $row) { |
144
|
|
|
$formattedTable[] = array( |
145
|
|
|
$row['path'], |
146
|
|
|
$row['scope'], |
147
|
|
|
$row['scope_id'], |
148
|
|
|
$row['value'], |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
$this->getHelper('table') |
152
|
|
|
->setHeaders(array('Path', 'Scope', 'Scope-ID', 'Value')) |
153
|
|
|
->setRows($formattedTable) |
154
|
|
|
->renderByFormat($output, $formattedTable, $format); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param OutputInterface $output |
159
|
|
|
* @param array $table |
160
|
|
|
*/ |
161
|
|
|
protected function renderAsUpdateScript(OutputInterface $output, $table) |
162
|
|
|
{ |
163
|
|
|
$output->writeln('<?php'); |
164
|
|
|
$output->writeln('$installer = $this;'); |
165
|
|
|
$output->writeln('# generated by n98-magerun'); |
166
|
|
|
|
167
|
|
|
foreach ($table as $row) { |
168
|
|
|
if ($row['scope'] == 'default') { |
169
|
|
|
$output->writeln( |
170
|
|
|
sprintf( |
171
|
|
|
'$installer->setConfigData(%s, %s);', |
172
|
|
|
var_export($row['path'], true), |
173
|
|
|
var_export($row['value'], true) |
174
|
|
|
) |
175
|
|
|
); |
176
|
|
|
} else { |
177
|
|
|
$output->writeln( |
178
|
|
|
sprintf( |
179
|
|
|
'$installer->setConfigData(%s, %s, %s, %s);', |
180
|
|
|
var_export($row['path'], true), |
181
|
|
|
var_export($row['value'], true), |
182
|
|
|
var_export($row['scope'], true), |
183
|
|
|
var_export($row['scope_id'], true) |
184
|
|
|
) |
185
|
|
|
); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param OutputInterface $output |
192
|
|
|
* @param array $table |
193
|
|
|
*/ |
194
|
|
|
protected function renderAsMagerunScript(OutputInterface $output, $table) |
195
|
|
|
{ |
196
|
|
|
foreach ($table as $row) { |
197
|
|
|
$value = str_replace(array("\n", "\r"), array('\n', '\r'), $row['value']); |
198
|
|
|
$line = sprintf( |
199
|
|
|
'config:set --scope-id=%s --scope=%s -- %s %s', |
200
|
|
|
$row['scope_id'], |
201
|
|
|
$row['scope'], |
202
|
|
|
escapeshellarg($row['path']), |
203
|
|
|
escapeshellarg($value) |
204
|
|
|
); |
205
|
|
|
$output->writeln($line); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
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.