Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
InstallStoreConfigurationCommand.php
Go to the documentation of this file.
1 <?php
8 
12 use Symfony\Component\Console\Input\InputOption;
13 use Symfony\Component\Console\Input\InputInterface;
14 use Symfony\Component\Console\Output\OutputInterface;
19 use Magento\Framework\Validator\Locale as LocaleValidator;
20 use Magento\Framework\Validator\Timezone as TimezoneValidator;
21 use Magento\Framework\Validator\Currency as CurrencyValidator;
22 use Magento\Framework\Validator\Url as UrlValidator;
23 
28 {
32  private $installerFactory;
33 
39  private $deploymentConfig;
40 
47  private $objectManager;
48 
52  private $localeValidator;
53 
57  private $timezoneValidator;
58 
62  private $currencyValidator;
63 
67  private $urlValidator;
68 
80  public function __construct(
81  InstallerFactory $installerFactory,
82  DeploymentConfig $deploymentConfig,
83  ObjectManagerProvider $objectManagerProvider,
84  LocaleValidator $localeValidator,
85  TimezoneValidator $timezoneValidator,
86  CurrencyValidator $currencyValidator,
87  UrlValidator $urlValidator
88  ) {
89  $this->installerFactory = $installerFactory;
90  $this->deploymentConfig = $deploymentConfig;
91  $this->objectManager = $objectManagerProvider->get();
92  $this->localeValidator = $localeValidator;
93  $this->timezoneValidator = $timezoneValidator;
94  $this->currencyValidator = $currencyValidator;
95  $this->urlValidator = $urlValidator;
96  parent::__construct();
97  }
98 
102  protected function configure()
103  {
104  $this->setName('setup:store-config:set')
105  ->setDescription('Installs the store configuration. Deprecated since 2.2.0. Use config:set instead')
106  ->setDefinition($this->getOptionsList());
107  parent::configure();
108  }
109 
113  protected function execute(InputInterface $input, OutputInterface $output)
114  {
115  if (!$this->deploymentConfig->isAvailable()) {
116  $output->writeln(
117  "<info>Store settings can't be saved because the Magento application is not installed.</info>"
118  );
119  // we must have an exit code higher than zero to indicate something was wrong
120  return \Magento\Framework\Console\Cli::RETURN_FAILURE;
121  }
122  $errors = $this->validate($input);
123  if ($errors) {
124  $output->writeln($errors);
125  // we must have an exit code higher than zero to indicate something was wrong
126  return \Magento\Framework\Console\Cli::RETURN_FAILURE;
127  }
128  $installer = $this->installerFactory->create(new ConsoleLogger($output));
129  $installer->installUserConfig($input->getOptions());
130  return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
131  }
132 
138  public function getOptionsList()
139  {
140  return [
141  new InputOption(
143  null,
144  InputOption::VALUE_REQUIRED,
145  'URL the store is supposed to be available at. '
146  . 'Deprecated, use config:set with path web/unsecure/base_url'
147  ),
148  new InputOption(
150  null,
151  InputOption::VALUE_REQUIRED,
152  'Default language code. '
153  . 'Deprecated, use config:set with path general/locale/code'
154  ),
155  new InputOption(
157  null,
158  InputOption::VALUE_REQUIRED,
159  'Default time zone code. '
160  . 'Deprecated, use config:set with path general/locale/timezone'
161  ),
162  new InputOption(
164  null,
165  InputOption::VALUE_REQUIRED,
166  'Default currency code. '
167  . 'Deprecated, use config:set with path currency/options/base, currency/options/default'
168  . ' and currency/options/allow'
169  ),
170  new InputOption(
172  null,
173  InputOption::VALUE_REQUIRED,
174  'Use rewrites. '
175  . 'Deprecated, use config:set with path web/seo/use_rewrites'
176  ),
177  new InputOption(
179  null,
180  InputOption::VALUE_REQUIRED,
181  'Use secure URLs. Enable this option only if SSL is available. '
182  . 'Deprecated, use config:set with path web/secure/use_in_frontend'
183  ),
184  new InputOption(
186  null,
187  InputOption::VALUE_REQUIRED,
188  'Base URL for SSL connection. '
189  . 'Deprecated, use config:set with path web/secure/base_url'
190  ),
191  new InputOption(
193  null,
194  InputOption::VALUE_REQUIRED,
195  'Run admin interface with SSL. '
196  . 'Deprecated, use config:set with path web/secure/use_in_adminhtml'
197  ),
198  new InputOption(
200  null,
201  InputOption::VALUE_REQUIRED,
202  'Whether to use a "security key" feature in Magento Admin URLs and forms. '
203  . 'Deprecated, use config:set with path admin/security/use_form_key'
204  ),
205  ];
206  }
207 
216  public function validate(InputInterface $input)
217  {
218  $errors = [];
219  $errorMsg = '';
220  $options = $input->getOptions();
221  foreach ($options as $key => $value) {
222  if (!$value) {
223  continue;
224  }
225  switch ($key) {
227  if (strcmp($value, '{{base_url}}') == 0) {
228  break;
229  }
230  $errorMsg = $this->validateUrl(
231  $value,
233  ['http', 'https']
234  );
235 
236  break;
238  $errorMsg = $this->validateCodes(
239  $this->localeValidator,
240  $value,
242  );
243  break;
245  $errorMsg = $this->validateCodes(
246  $this->timezoneValidator,
247  $value,
249  );
250  break;
252  $errorMsg = $this->validateCodes(
253  $this->currencyValidator,
254  $value,
256  );
257  break;
259  $errorMsg = $this->validateBinaryValue($value, StoreConfigurationDataMapper::KEY_USE_SEF_URL);
260  break;
262  $errorMsg = $this->validateBinaryValue($value, StoreConfigurationDataMapper::KEY_IS_SECURE);
263  break;
265  $errorMsg = $this->validateUrl(
266  $value,
268  ['https']
269  );
270  break;
272  $errorMsg = $this->validateBinaryValue($value, StoreConfigurationDataMapper::KEY_IS_SECURE_ADMIN);
273  break;
275  $errorMsg = $this->validateBinaryValue(
276  $value,
278  );
279  break;
281  $errorMsg = $this->validateBinaryValue(
282  $value,
284  );
285  break;
286  }
287  if ($errorMsg !== '') {
288  $errors[] = $errorMsg;
289  }
290  }
291  return $errors;
292  }
293 
301  private function validateBinaryValue($value, $key)
302  {
303  $errorMsg = '';
304  if ($value !== '0' && $value !== '1') {
305  $errorMsg = '<error>' . 'Command option \'' . $key . '\': Invalid value. Possible values (0|1).</error>';
306  }
307  return $errorMsg;
308  }
309 
318  private function validateCodes($lists, $code, $type)
319  {
320  $errorMsg = '';
321  if (!$lists->isValid($code)) {
322  $errorMsg = '<error>' . 'Command option \'' . $type . '\': Invalid value. To see possible values, '
323  . "run command 'bin/magento info:" . $type . ':list\'.</error>';
324  }
325  return $errorMsg;
326  }
327 
336  private function validateUrl($url, $option, array $allowedSchemes)
337  {
338  $errorMsg = '';
339 
340  if (!$this->urlValidator->isValid($url, $allowedSchemes)) {
341  $errorTemplate = '<error>Command option \'%s\': Invalid URL \'%s\'.'
342  . ' Domain Name should contain only letters, digits and hyphen.'
343  . ' And you should use only following schemes: \'%s\'.</error>';
344  $errorMsg = sprintf(
345  $errorTemplate,
346  $option,
347  $url,
348  implode(', ', $allowedSchemes)
349  );
350  }
351 
352  return $errorMsg;
353  }
354 }
__construct(InstallerFactory $installerFactory, DeploymentConfig $deploymentConfig, ObjectManagerProvider $objectManagerProvider, LocaleValidator $localeValidator, TimezoneValidator $timezoneValidator, CurrencyValidator $currencyValidator, UrlValidator $urlValidator)
$block setTitle( 'CMS Block Title') -> setIdentifier('fixture_block') ->setContent('< h1 >Fixture Block Title</h1 >< a href=" store url</a><p> Config value
Definition: block.php:9
$deploymentConfig
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16
$errors
Definition: overview.phtml:9