1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace N98\Magento\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
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 \Symfony\Component\Console\Input\InputInterface $input |
97
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
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
|
|
|
$storeManager = $this->getObjectManager()->get('Magento\Store\Model\StoreManagerInterface'); |
115
|
|
|
/* @var $storeManager \Magento\Store\Model\StoreManagerInterface */ |
116
|
|
|
$store = $storeManager->getStore(\Magento\Store\Model\Store::DEFAULT_STORE_ID); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if ($input->getOption('on')) { |
121
|
|
|
$isFalse = true; |
122
|
|
|
} elseif ($input->getOption('off')) { |
123
|
|
|
$isFalse = false; |
124
|
|
|
} else { |
125
|
|
|
$scopeConfig = $this->getObjectManager()->get('\Magento\Framework\App\Config\ScopeConfigInterface'); |
126
|
|
|
/* @var $scopeConfig \Magento\Framework\App\Config\ScopeConfigInterface */ |
127
|
|
|
$isFalse = !$scopeConfig->isSetFlag( |
128
|
|
|
$this->configPath, |
129
|
|
|
\Magento\Store\Model\ScopeInterface::SCOPE_STORE, |
130
|
|
|
$store->getCode() |
|
|
|
|
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$this->_beforeSave($store, $isFalse); |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
if ($store->getId() == \Magento\Store\Model\Store::DEFAULT_STORE_ID) { |
138
|
|
|
$scope = 'default'; // @TODO Constant was removed in Magento2 ? |
139
|
|
|
} else { |
140
|
|
|
$scope = \Magento\Store\Model\ScopeInterface::SCOPE_STORES; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$configSetCommands = [ |
144
|
|
|
'command' => 'config:set', |
145
|
|
|
'path' => $this->configPath, |
146
|
|
|
'value' => $isFalse ? 1 : 0, |
147
|
|
|
'--scope' => $scope, |
148
|
|
|
'--scope-id' => $store->getId(), |
149
|
|
|
]; |
150
|
|
|
|
151
|
|
|
$input = new ArrayInput($configSetCommands); |
152
|
|
|
$this->getApplication()->setAutoExit(false); |
153
|
|
|
$this->getApplication()->run($input, new NullOutput()); |
154
|
|
|
|
155
|
|
|
$comment = '<comment>' . $this->toggleComment . '</comment> ' |
156
|
|
|
. '<info>' . (!$isFalse ? $this->falseName : $this->trueName) . '</info>' |
157
|
|
|
. ($runOnStoreView ? ' <comment>for store</comment> <info>' . $store->getCode() . '</info>' : ''); |
|
|
|
|
158
|
|
|
$output->writeln($comment); |
159
|
|
|
|
160
|
|
|
$this->_afterSave($store, $isFalse); |
161
|
|
|
|
162
|
|
|
$input = new StringInput('cache:flush'); |
163
|
|
|
$this->getApplication()->run($input, new NullOutput()); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
168
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
169
|
|
|
* @return mixed |
170
|
|
|
* @throws \Exception |
171
|
|
|
*/ |
172
|
|
|
protected function _initStore($input, $output) |
173
|
|
|
{ |
174
|
|
|
return $this->getHelperSet()->get('parameter')->askStore($input, $output, 'store', $this->withAdminStore); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param \Magento\Store\Model\Store $store |
179
|
|
|
* @param bool $disabled |
180
|
|
|
*/ |
181
|
|
|
protected function _beforeSave(\Magento\Store\Model\Store $store, $disabled) |
|
|
|
|
182
|
|
|
{ |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param \Magento\Store\Model\Store $store |
187
|
|
|
* @param bool $disabled |
188
|
|
|
*/ |
189
|
|
|
protected function _afterSave(\Magento\Store\Model\Store $store, $disabled) |
|
|
|
|
190
|
|
|
{ |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
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: