Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
InfoBackupsListCommand.php
Go to the documentation of this file.
1 <?php
8 
13 use Symfony\Component\Console\Command\Command;
14 use Symfony\Component\Console\Helper\TableFactory;
15 use Symfony\Component\Console\Input\InputInterface;
16 use Symfony\Component\Console\Output\OutputInterface;
18 
22 class InfoBackupsListCommand extends Command
23 {
29  private $file;
30 
36  private $directoryList;
37 
41  private $tableHelperFactory;
42 
48  public function __construct(
49  DirectoryList $directoryList,
50  File $file,
51  TableFactory $tableHelperFactory = null
52  ) {
53  $this->directoryList = $directoryList;
54  $this->file = $file;
55  $this->tableHelperFactory = $tableHelperFactory ?: ObjectManager::getInstance()->create(TableFactory::class);
56  parent::__construct();
57  }
58 
62  protected function configure()
63  {
64  $this->setName('info:backups:list')
65  ->setDescription('Prints list of available backup files');
66 
67  parent::configure();
68  }
69 
73  protected function execute(InputInterface $input, OutputInterface $output)
74  {
75  $backupsDir = $this->directoryList->getPath(DirectoryList::VAR_DIR)
77  if ($this->file->isExists($backupsDir)) {
78  $contents = $this->file->readDirectoryRecursively($backupsDir);
79  $tempTable = [];
80  foreach ($contents as $path) {
81  $partsOfPath = explode('/', str_replace('\\', '/', $path));
82  $fileName = $partsOfPath[count($partsOfPath) - 1];
83  // if filename starts with '.' skip, e.g. '.git'
84  if (!(strpos($fileName, '.') === 0)) {
85  $filenameWithoutExtension = explode('.', $fileName);
86  // actually first part of $filenameWithoutExtension contains only the filename without extension
87  // and filename contains the type of backup separated by '_'
88  $fileNameParts = explode('_', $filenameWithoutExtension[0]);
89  if (in_array(Factory::TYPE_MEDIA, $fileNameParts)) {
90  $tempTable[$fileName] = Factory::TYPE_MEDIA;
91  } elseif (in_array(Factory::TYPE_DB, $fileNameParts)) {
92  $tempTable[$fileName] = Factory::TYPE_DB;
93  } elseif (in_array('code', $fileNameParts)) {
94  $tempTable[$fileName] = 'code';
95  }
96  }
97  }
98  if (empty($tempTable)) {
99  $output->writeln('<info>No backup files found.</info>');
100  return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
101  }
102  $output->writeln("<info>Showing backup files in $backupsDir.</info>");
104  $tableHelper = $this->tableHelperFactory->create(['output' => $output]);
105  $tableHelper->setHeaders(['Backup Filename', 'Backup Type']);
106  asort($tempTable);
107  foreach ($tempTable as $key => $value) {
108  $tableHelper->addRow([$key, $value]);
109  }
110  $tableHelper->render();
111  } else {
112  $output->writeln('<info>No backup files found.</info>');
113  }
114 
115  return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
116  }
117 }
$contents
Definition: website.php:14
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$fileName
Definition: translate.phtml:15
$value
Definition: gender.phtml:16
__construct(DirectoryList $directoryList, File $file, TableFactory $tableHelperFactory=null)