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