Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MaintenanceAllowIpsCommand.php
Go to the documentation of this file.
1 <?php
8 
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Input\InputArgument;
14 use Symfony\Component\Console\Input\InputOption;
15 use Symfony\Component\Console\Output\OutputInterface;
16 
21 {
25  const INPUT_KEY_IP = 'ip';
26  const INPUT_KEY_NONE = 'none';
27  const INPUT_KEY_ADD = 'add';
28 
32  private $maintenanceMode;
33 
37  private $ipValidator;
38 
45  public function __construct(MaintenanceMode $maintenanceMode, IpValidator $ipValidator)
46  {
47  $this->maintenanceMode = $maintenanceMode;
48  $this->ipValidator = $ipValidator;
49  parent::__construct();
50  }
51 
57  protected function configure()
58  {
59  $arguments = [
60  new InputArgument(
61  self::INPUT_KEY_IP,
62  InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
63  'Allowed IP addresses'
64  ),
65  ];
66  $options = [
67  new InputOption(
68  self::INPUT_KEY_NONE,
69  null,
70  InputOption::VALUE_NONE,
71  'Clear allowed IP addresses'
72  ),
73  new InputOption(
74  self::INPUT_KEY_ADD,
75  null,
76  InputOption::VALUE_NONE,
77  'Add the IP address to existing list'
78  ),
79  ];
80  $this->setName('maintenance:allow-ips')
81  ->setDescription('Sets maintenance mode exempt IPs')
82  ->setDefinition(array_merge($arguments, $options));
83  parent::configure();
84  }
85 
89  protected function execute(InputInterface $input, OutputInterface $output)
90  {
91  if (!$input->getOption(self::INPUT_KEY_NONE)) {
92  $addresses = $input->getArgument(self::INPUT_KEY_IP);
93  $messages = $this->validate($addresses);
94  if (!empty($messages)) {
95  $output->writeln('<error>' . implode('</error>' . PHP_EOL . '<error>', $messages));
96  // we must have an exit code higher than zero to indicate something was wrong
97  return \Magento\Framework\Console\Cli::RETURN_FAILURE;
98  }
99 
100  if (!empty($addresses)) {
101  if ($input->getOption(self::INPUT_KEY_ADD)) {
102  $addresses = array_unique(array_merge($this->maintenanceMode->getAddressInfo(), $addresses));
103  }
104  $this->maintenanceMode->setAddresses(implode(',', $addresses));
105  $output->writeln(
106  '<info>Set exempt IP-addresses: ' . implode(' ', $this->maintenanceMode->getAddressInfo()) .
107  '</info>'
108  );
109  }
110  } else {
111  $this->maintenanceMode->setAddresses('');
112  $output->writeln('<info>Set exempt IP-addresses: none</info>');
113  }
114  return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
115  }
116 
123  protected function validate(array $addresses)
124  {
125  return $this->ipValidator->validateIps($addresses, false);
126  }
127 }
execute(InputInterface $input, OutputInterface $output)
$addresses
Definition: address_list.php:7
$arguments
__construct(MaintenanceMode $maintenanceMode, IpValidator $ipValidator)