1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace N98\Magento\Command; |
4
|
|
|
|
5
|
|
|
use N98\Util\Console\Helper\ParameterHelper; |
6
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
use Symfony\Component\Console\Input\StringInput; |
11
|
|
|
use Symfony\Component\Console\Output\NullOutput; |
12
|
|
|
|
13
|
|
|
abstract class AbstractMagentoStoreConfigCommand extends AbstractMagentoCommand |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
const SCOPE_STORE_VIEW = 'store'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
const SCOPE_WEBSITE = 'website'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
const SCOPE_GLOBAL = 'global'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Store view or global by additional option |
32
|
|
|
*/ |
33
|
|
|
const SCOPE_STORE_VIEW_GLOBAL = 'store_view_global'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $commandName = ''; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $commandDescription = ''; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
protected $configPath = ''; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
protected $toggleComment = ''; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
protected $falseName = 'disabled'; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
protected $trueName = 'enabled'; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Add admin store to interactive prompt |
67
|
|
|
* |
68
|
|
|
* @var bool |
69
|
|
|
*/ |
70
|
|
|
protected $withAdminStore = false; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var string |
74
|
|
|
*/ |
75
|
|
|
protected $scope = self::SCOPE_STORE_VIEW; |
76
|
|
|
|
77
|
|
|
protected function configure() |
78
|
|
|
{ |
79
|
|
|
$this |
80
|
|
|
->setName($this->commandName) |
81
|
|
|
->addOption('on', null, InputOption::VALUE_NONE, 'Switch on') |
82
|
|
|
->addOption('off', null, InputOption::VALUE_NONE, 'Switch off') |
83
|
|
|
->setDescription($this->commandDescription) |
84
|
|
|
; |
85
|
|
|
|
86
|
|
|
if ($this->scope == self::SCOPE_STORE_VIEW_GLOBAL) { |
87
|
|
|
$this->addOption('global', null, InputOption::VALUE_NONE, 'Set value on default scope'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if ($this->scope == self::SCOPE_STORE_VIEW || $this->scope == self::SCOPE_STORE_VIEW_GLOBAL) { |
91
|
|
|
$this->addArgument('store', InputArgument::OPTIONAL, 'Store code or ID'); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param InputInterface $input |
97
|
|
|
* @param OutputInterface $output |
98
|
|
|
* |
99
|
|
|
* @return int|void |
100
|
|
|
*/ |
101
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
102
|
|
|
{ |
103
|
|
|
$this->detectMagento($output); |
104
|
|
|
if ($this->initMagento()) { |
105
|
|
|
$runOnStoreView = false; |
106
|
|
|
if ($this->scope == self::SCOPE_STORE_VIEW |
107
|
|
|
|| ($this->scope == self::SCOPE_STORE_VIEW_GLOBAL && !$input->getOption('global')) |
108
|
|
|
) { |
109
|
|
|
$runOnStoreView = true; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if ($runOnStoreView) { |
113
|
|
|
$store = $this->_initStore($input, $output); |
114
|
|
|
} else { |
115
|
|
|
$store = \Mage::app()->getStore(\Mage_Core_Model_App::ADMIN_STORE_ID); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if ($input->getOption('on')) { |
120
|
|
|
$isFalse = true; |
121
|
|
|
} elseif ($input->getOption('off')) { |
122
|
|
|
$isFalse = false; |
123
|
|
|
} else { |
124
|
|
|
$isFalse = !\Mage::getStoreConfigFlag($this->configPath, $store->getId()); |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$this->_beforeSave($store, $isFalse); |
128
|
|
|
|
129
|
|
|
\Mage::app()->getConfig()->saveConfig( |
130
|
|
|
$this->configPath, |
131
|
|
|
$isFalse ? 1 : 0, |
132
|
|
|
$store->getId() == \Mage_Core_Model_App::ADMIN_STORE_ID ? 'default' : 'stores', |
133
|
|
|
$store->getId() |
134
|
|
|
); |
135
|
|
|
|
136
|
|
|
$comment = |
137
|
|
|
'<comment>' . $this->toggleComment . '</comment> ' |
138
|
|
|
. '<info>' . (!$isFalse ? $this->falseName : $this->trueName) . '</info>' |
139
|
|
|
. ($runOnStoreView ? ' <comment>for store</comment> <info>' . $store->getCode() . '</info>' : ''); |
|
|
|
|
140
|
|
|
|
141
|
|
|
$output->writeln($comment); |
142
|
|
|
|
143
|
|
|
$this->_afterSave($store, $isFalse); |
144
|
|
|
|
145
|
|
|
$input = new StringInput('cache:flush'); |
146
|
|
|
$this->getApplication()->run($input, new NullOutput()); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Determine if a developer restriction is in place, and if we're enabling something that will use it |
151
|
|
|
* then notify and ask if it needs to be changed from its current value. |
152
|
|
|
* |
153
|
|
|
* @param \Mage_Core_Model_Store $store |
154
|
|
|
* @param bool $enabled |
155
|
|
|
* @return void |
156
|
|
|
*/ |
157
|
|
|
protected function detectAskAndSetDeveloperIp(\Mage_Core_Model_Store $store, $enabled) |
158
|
|
|
{ |
159
|
|
|
if (!$enabled) { |
160
|
|
|
// No need to notify about developer IP restrictions if we're disabling template hints etc |
161
|
|
|
return; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** @var OutputInterface $output */ |
165
|
|
|
$output = $this->getHelper('io')->getOutput(); |
166
|
|
|
|
167
|
|
|
if (!$devRestriction = $store->getConfig('dev/restrict/allow_ips')) { |
168
|
|
|
return; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$this->askAndSetDeveloperIp($output, $store, $devRestriction); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Ask if the developer IP should be changed, and change it if required |
176
|
|
|
* |
177
|
|
|
* @param OutputInterface $output |
178
|
|
|
* @param \Mage_Core_Model_Store $store |
179
|
|
|
* @param string|null $devRestriction |
180
|
|
|
* @return void |
181
|
|
|
*/ |
182
|
|
|
protected function askAndSetDeveloperIp(OutputInterface $output, \Mage_Core_Model_Store $store, $devRestriction) |
183
|
|
|
{ |
184
|
|
|
$output->writeln( |
185
|
|
|
sprintf( |
186
|
|
|
'<comment><info>Please note:</info> developer IP restriction is enabled for <info>%s</info>.', |
187
|
|
|
$devRestriction |
188
|
|
|
) |
189
|
|
|
); |
190
|
|
|
|
191
|
|
|
/** @var $dialog \Symfony\Component\Console\Helper\DialogHelper */ |
192
|
|
|
$dialog = $this->getHelperSet()->get('dialog'); |
193
|
|
|
$newDeveloperIp = $dialog->ask( |
194
|
|
|
$output, |
195
|
|
|
'<question>Change developer IP? Enter a new IP to change or leave blank</question>: ' |
196
|
|
|
); |
197
|
|
|
|
198
|
|
|
if (empty($newDeveloperIp)) { |
199
|
|
|
return; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
$this->setDeveloperIp($store, $newDeveloperIp); |
203
|
|
|
$output->writeln(sprintf('<comment><info>New developer IP restriction set to %s', $newDeveloperIp)); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Set the restricted IP for developer access |
208
|
|
|
* |
209
|
|
|
* @param \Mage_Core_Model_Store $store |
210
|
|
|
* @param string $newDeveloperIp |
211
|
|
|
*/ |
212
|
|
|
protected function setDeveloperIp(\Mage_Core_Model_Store $store, $newDeveloperIp) |
213
|
|
|
{ |
214
|
|
|
\Mage::getModel('core/config') |
215
|
|
|
->saveConfig('dev/restrict/allow_ips', $newDeveloperIp, 'stores', $store->getId()); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @param InputInterface $input |
220
|
|
|
* @param OutputInterface $output |
221
|
|
|
* |
222
|
|
|
* @return mixed |
223
|
|
|
*/ |
224
|
|
|
protected function _initStore(InputInterface $input, OutputInterface $output) |
225
|
|
|
{ |
226
|
|
|
/** @var ParameterHelper $parameterHelper */ |
227
|
|
|
$parameterHelper = $this->getHelper('parameter'); |
228
|
|
|
|
229
|
|
|
return $parameterHelper->askStore($input, $output, 'store', $this->withAdminStore); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @param \Mage_Core_Model_Store $store |
234
|
|
|
* @param bool $disabled |
235
|
|
|
*/ |
236
|
|
|
protected function _beforeSave(\Mage_Core_Model_Store $store, $disabled) |
|
|
|
|
237
|
|
|
{ |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @param \Mage_Core_Model_Store $store |
242
|
|
|
* @param bool $disabled |
243
|
|
|
*/ |
244
|
|
|
protected function _afterSave(\Mage_Core_Model_Store $store, $disabled) |
245
|
|
|
{ |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: