Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RollbackCommand.php
Go to the documentation of this file.
1 <?php
7 
15 use Symfony\Component\Console\Input\InputInterface;
16 use Symfony\Component\Console\Input\InputOption;
17 use Symfony\Component\Console\Output\OutputInterface;
18 use Symfony\Component\Console\Question\ConfirmationQuestion;
19 
26 {
30  const INPUT_KEY_CODE_BACKUP_FILE = 'code-file';
31  const INPUT_KEY_MEDIA_BACKUP_FILE = 'media-file';
32  const INPUT_KEY_DB_BACKUP_FILE = 'db-file';
33 
39  private $objectManager;
40 
44  private $backupRollbackFactory;
45 
51  private $deploymentConfig;
52 
56  private $maintenanceModeEnabler;
57 
68  public function __construct(
69  ObjectManagerProvider $objectManagerProvider,
70  MaintenanceMode $maintenanceMode,
71  DeploymentConfig $deploymentConfig,
72  MaintenanceModeEnabler $maintenanceModeEnabler = null
73  ) {
74  $this->objectManager = $objectManagerProvider->get();
75  $this->backupRollbackFactory = $this->objectManager->get(\Magento\Framework\Setup\BackupRollbackFactory::class);
76  $this->deploymentConfig = $deploymentConfig;
77  $this->maintenanceModeEnabler =
78  $maintenanceModeEnabler ?: $this->objectManager->get(MaintenanceModeEnabler::class);
79  parent::__construct();
80  }
81 
85  protected function configure()
86  {
87  $options = [
88  new InputOption(
89  self::INPUT_KEY_CODE_BACKUP_FILE,
90  'c',
91  InputOption::VALUE_REQUIRED,
92  'Basename of the code backup file in var/backups'
93  ),
94  new InputOption(
95  self::INPUT_KEY_MEDIA_BACKUP_FILE,
96  'm',
97  InputOption::VALUE_REQUIRED,
98  'Basename of the media backup file in var/backups'
99  ),
100  new InputOption(
101  self::INPUT_KEY_DB_BACKUP_FILE,
102  'd',
103  InputOption::VALUE_REQUIRED,
104  'Basename of the db backup file in var/backups'
105  ),
106  ];
107  $this->setName('setup:rollback')
108  ->setDescription('Rolls back Magento Application codebase, media and database')
109  ->setDefinition($options);
110  parent::configure();
111  }
112 
116  protected function execute(InputInterface $input, OutputInterface $output)
117  {
118  if (!$this->deploymentConfig->isAvailable() && ($input->getOption(self::INPUT_KEY_MEDIA_BACKUP_FILE)
119  || $input->getOption(self::INPUT_KEY_DB_BACKUP_FILE))
120  ) {
121  $output->writeln("<info>No information is available: the Magento application is not installed.</info>");
122  // we must have an exit code higher than zero to indicate something was wrong
123  return \Magento\Framework\Console\Cli::RETURN_FAILURE;
124  }
125  $returnValue = $this->maintenanceModeEnabler->executeInMaintenanceMode(
126  function () use ($input, $output, &$returnValue) {
127  try {
128  $helper = $this->getHelper('question');
129  $question = new ConfirmationQuestion(
130  '<info>You are about to remove current code and/or database tables. Are you sure?[y/N]<info>',
131  false
132  );
133  if (!$helper->ask($input, $output, $question) && $input->isInteractive()) {
134  return \Magento\Framework\Console\Cli::RETURN_FAILURE;
135  }
136  $questionKeep = new ConfirmationQuestion(
137  '<info>Do you want to keep the backups?[y/N]<info>',
138  false
139  );
140  $keepSourceFile = $helper->ask($input, $output, $questionKeep);
141 
142  $this->doRollback($input, $output, $keepSourceFile);
143  $output->writeln('<info>Please set file permission of bin/magento to executable</info>');
144 
145  return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
146  } catch (\Exception $e) {
147  $output->writeln('<error>' . $e->getMessage() . '</error>');
148  // we must have an exit code higher than zero to indicate something was wrong
149  return \Magento\Framework\Console\Cli::RETURN_FAILURE;
150  }
151  },
152  $output,
153  false
154  );
155  return $returnValue;
156  }
157 
167  private function doRollback(InputInterface $input, OutputInterface $output, $keepSourceFile)
168  {
169  $inputOptionProvided = false;
170  $rollbackHandler = $this->backupRollbackFactory->create($output);
171  if ($input->getOption(self::INPUT_KEY_CODE_BACKUP_FILE)) {
172  $rollbackHandler->codeRollback(
173  $input->getOption(self::INPUT_KEY_CODE_BACKUP_FILE),
175  $keepSourceFile
176  );
177  $inputOptionProvided = true;
178  }
179  if ($input->getOption(self::INPUT_KEY_MEDIA_BACKUP_FILE)) {
180  $rollbackHandler->codeRollback(
181  $input->getOption(self::INPUT_KEY_MEDIA_BACKUP_FILE),
183  $keepSourceFile
184  );
185  $inputOptionProvided = true;
186  }
187  if ($input->getOption(self::INPUT_KEY_DB_BACKUP_FILE)) {
188  $this->setAreaCode();
189  $rollbackHandler->dbRollback($input->getOption(self::INPUT_KEY_DB_BACKUP_FILE), $keepSourceFile);
190  $inputOptionProvided = true;
191  }
192  if (!$inputOptionProvided) {
193  throw new \InvalidArgumentException(
194  'Not enough information provided to roll back.'
195  );
196  }
197  }
198 
204  private function setAreaCode()
205  {
206  $areaCode = 'adminhtml';
208  $appState = $this->objectManager->get(\Magento\Framework\App\State::class);
209  $appState->setAreaCode($areaCode);
211  $configLoader = $this->objectManager->get(\Magento\Framework\ObjectManager\ConfigLoaderInterface::class);
212  $this->objectManager->configure($configLoader->load($areaCode));
213  }
214 }
$helper
Definition: iframe.phtml:13
return false
Definition: gallery.phtml:36
$deploymentConfig
execute(InputInterface $input, OutputInterface $output)
__construct(ObjectManagerProvider $objectManagerProvider, MaintenanceMode $maintenanceMode, DeploymentConfig $deploymentConfig, MaintenanceModeEnabler $maintenanceModeEnabler=null)