1 | <?php |
||
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() |
||
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) |
||
176 | |||
177 | /** |
||
178 | * @param \Magento\Store\Model\Store $store |
||
179 | * @param bool $disabled |
||
180 | */ |
||
181 | protected function _beforeSave(\Magento\Store\Model\Store $store, $disabled) |
||
184 | |||
185 | /** |
||
186 | * @param \Magento\Store\Model\Store $store |
||
187 | * @param bool $disabled |
||
188 | */ |
||
189 | protected function _afterSave(\Magento\Store\Model\Store $store, $disabled) |
||
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: