Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractMaintenanceCommand.php
Go to the documentation of this file.
1 <?php
7 
10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Output\OutputInterface;
13 
15 {
19  const INPUT_KEY_IP = 'ip';
20 
24  protected $maintenanceMode;
25 
29  protected $ipValidator;
30 
38  {
39  $this->maintenanceMode = $maintenanceMode;
40  $this->ipValidator = $ipValidator;
41  parent::__construct();
42  }
43 
49  protected function configure()
50  {
51  $options = [
52  new InputOption(
53  self::INPUT_KEY_IP,
54  null,
55  InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
56  "Allowed IP addresses (use 'none' to clear allowed IP list)"
57  ),
58  ];
59  $this->setDefinition($options);
60  parent::configure();
61  }
62 
68  abstract protected function isEnable();
69 
75  abstract protected function getDisplayString();
76 
80  protected function execute(InputInterface $input, OutputInterface $output)
81  {
82  $addresses = $input->getOption(self::INPUT_KEY_IP);
83  $messages = $this->validate($addresses);
84  if (!empty($messages)) {
85  $output->writeln('<error>' . implode('</error>' . PHP_EOL . '<error>', $messages));
86  // we must have an exit code higher than zero to indicate something was wrong
87  return \Magento\Framework\Console\Cli::RETURN_FAILURE;
88  }
89 
90  $this->maintenanceMode->set($this->isEnable());
91  $output->writeln($this->getDisplayString());
92 
93  if (!empty($addresses)) {
94  $addresses = implode(',', $addresses);
95  $addresses = ('none' == $addresses) ? '' : $addresses;
96  $this->maintenanceMode->setAddresses($addresses);
97  $output->writeln(
98  '<info>Set exempt IP-addresses: ' . (implode(', ', $this->maintenanceMode->getAddressInfo()) ?: 'none')
99  . '</info>'
100  );
101  }
102  return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
103  }
104 
111  protected function validate(array $addresses)
112  {
113  return $this->ipValidator->validateIps($addresses, true);
114  }
115 }
$addresses
Definition: address_list.php:7
__construct(MaintenanceMode $maintenanceMode, IpValidator $ipValidator)
execute(InputInterface $input, OutputInterface $output)