|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace N98\Magento\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use N98\Util\Console\Helper\ParameterHelper; |
|
7
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
12
|
|
|
use Symfony\Component\Console\Input\StringInput; |
|
13
|
|
|
use Symfony\Component\Console\Output\NullOutput; |
|
14
|
|
|
|
|
15
|
|
|
abstract class AbstractMagentoStoreConfigCommand extends AbstractMagentoCommand |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
const SCOPE_STORE_VIEW = 'store'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
const SCOPE_WEBSITE = 'website'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
const SCOPE_GLOBAL = 'global'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Store view or global by additional option |
|
34
|
|
|
*/ |
|
35
|
|
|
const SCOPE_STORE_VIEW_GLOBAL = 'store_view_global'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $commandName = ''; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $commandDescription = ''; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var string |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $configPath = ''; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var string |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $toggleComment = ''; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var string |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $falseName = 'disabled'; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var string |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $trueName = 'enabled'; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Add admin store to interactive prompt |
|
69
|
|
|
* |
|
70
|
|
|
* @var bool |
|
71
|
|
|
*/ |
|
72
|
|
|
protected $withAdminStore = false; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @var string |
|
76
|
|
|
*/ |
|
77
|
|
|
protected $scope = self::SCOPE_STORE_VIEW; |
|
78
|
|
|
|
|
79
|
|
|
protected function configure() |
|
80
|
|
|
{ |
|
81
|
|
|
$this |
|
82
|
|
|
->setName($this->commandName) |
|
83
|
|
|
->addOption('on', null, InputOption::VALUE_NONE, 'Switch on') |
|
84
|
|
|
->addOption('off', null, InputOption::VALUE_NONE, 'Switch off') |
|
85
|
|
|
->setDescription($this->commandDescription) |
|
86
|
|
|
; |
|
87
|
|
|
|
|
88
|
|
|
if ($this->scope == self::SCOPE_STORE_VIEW_GLOBAL) { |
|
89
|
|
|
$this->addOption('global', null, InputOption::VALUE_NONE, 'Set value on default scope'); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if ($this->scope == self::SCOPE_STORE_VIEW || $this->scope == self::SCOPE_STORE_VIEW_GLOBAL) { |
|
93
|
|
|
$this->addArgument('store', InputArgument::OPTIONAL, 'Store code or ID'); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param InputInterface $input |
|
99
|
|
|
* @param OutputInterface $output |
|
100
|
|
|
* @return int|void |
|
101
|
|
|
*/ |
|
102
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
103
|
|
|
{ |
|
104
|
|
|
$this->detectMagento($output); |
|
105
|
|
|
if ($this->initMagento()) { |
|
106
|
|
|
$runOnStoreView = false; |
|
107
|
|
|
if ($this->scope == self::SCOPE_STORE_VIEW |
|
108
|
|
|
|| ($this->scope == self::SCOPE_STORE_VIEW_GLOBAL && !$input->getOption('global')) |
|
109
|
|
|
) { |
|
110
|
|
|
$runOnStoreView = true; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
if ($runOnStoreView) { |
|
114
|
|
|
$store = $this->_initStore($input, $output); |
|
115
|
|
|
} else { |
|
116
|
|
|
$storeManager = $this->getObjectManager()->get('Magento\Store\Model\StoreManagerInterface'); |
|
117
|
|
|
/* @var $storeManager \Magento\Store\Model\StoreManagerInterface */ |
|
118
|
|
|
$store = $storeManager->getStore(\Magento\Store\Model\Store::DEFAULT_STORE_ID); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
if ($input->getOption('on')) { |
|
123
|
|
|
$isFalse = true; |
|
124
|
|
|
} elseif ($input->getOption('off')) { |
|
125
|
|
|
$isFalse = false; |
|
126
|
|
|
} else { |
|
127
|
|
|
$scopeConfig = $this->getObjectManager()->get('\Magento\Framework\App\Config\ScopeConfigInterface'); |
|
128
|
|
|
/* @var $scopeConfig \Magento\Framework\App\Config\ScopeConfigInterface */ |
|
129
|
|
|
$isFalse = !$scopeConfig->isSetFlag( |
|
130
|
|
|
$this->configPath, |
|
131
|
|
|
\Magento\Store\Model\ScopeInterface::SCOPE_STORE, |
|
132
|
|
|
$store->getCode() |
|
|
|
|
|
|
133
|
|
|
); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
$this->_beforeSave($store, $isFalse); |
|
137
|
|
|
|
|
138
|
|
|
|
|
139
|
|
|
if ($store->getId() == \Magento\Store\Model\Store::DEFAULT_STORE_ID) { |
|
140
|
|
|
$scope = 'default'; // @TODO Constant was removed in Magento2 ? |
|
141
|
|
|
} else { |
|
142
|
|
|
$scope = \Magento\Store\Model\ScopeInterface::SCOPE_STORES; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
$configSetCommands = [ |
|
146
|
|
|
'command' => 'config:set', |
|
147
|
|
|
'path' => $this->configPath, |
|
148
|
|
|
'value' => $isFalse ? 1 : 0, |
|
149
|
|
|
'--scope' => $scope, |
|
150
|
|
|
'--scope-id' => $store->getId(), |
|
151
|
|
|
]; |
|
152
|
|
|
|
|
153
|
|
|
$input = new ArrayInput($configSetCommands); |
|
154
|
|
|
$this->getApplication()->setAutoExit(false); |
|
155
|
|
|
$this->getApplication()->run($input, new NullOutput()); |
|
156
|
|
|
|
|
157
|
|
|
$comment = '<comment>' . $this->toggleComment . '</comment> ' |
|
158
|
|
|
. '<info>' . (!$isFalse ? $this->falseName : $this->trueName) . '</info>' |
|
159
|
|
|
. ($runOnStoreView ? ' <comment>for store</comment> <info>' . $store->getCode() . '</info>' : ''); |
|
|
|
|
|
|
160
|
|
|
$output->writeln($comment); |
|
161
|
|
|
|
|
162
|
|
|
$this->_afterSave($store, $isFalse); |
|
163
|
|
|
|
|
164
|
|
|
$input = new StringInput('cache:flush'); |
|
165
|
|
|
$this->getApplication()->run($input, new NullOutput()); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param InputInterface $input |
|
170
|
|
|
* @param OutputInterface $output |
|
171
|
|
|
* @return mixed |
|
172
|
|
|
* @throws Exception |
|
173
|
|
|
*/ |
|
174
|
|
|
protected function _initStore(InputInterface $input, OutputInterface $output) |
|
175
|
|
|
{ |
|
176
|
|
|
/** @var $parameter ParameterHelper */ |
|
177
|
|
|
$parameter = $this->getHelper('parameter'); |
|
178
|
|
|
|
|
179
|
|
|
return $parameter->askStore($input, $output, 'store', $this->withAdminStore); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @param \Magento\Store\Model\Store $store |
|
184
|
|
|
* @param bool $disabled |
|
185
|
|
|
*/ |
|
186
|
|
|
protected function _beforeSave(\Magento\Store\Model\Store $store, $disabled) |
|
|
|
|
|
|
187
|
|
|
{ |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* @param \Magento\Store\Model\Store $store |
|
192
|
|
|
* @param bool $disabled |
|
193
|
|
|
*/ |
|
194
|
|
|
protected function _afterSave(\Magento\Store\Model\Store $store, $disabled) |
|
|
|
|
|
|
195
|
|
|
{ |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|
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: