|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @Author: nguyen |
| 4 | + * @Date: 2020-12-15 14:01:01 |
| 5 | + * @Last Modified by: Alex Dong |
| 6 | + * @Last Modified time: 2022-12-15 09:25:12 |
| 7 | + */ |
| 8 | + |
| 9 | +namespace Magepow\Core\Console\Command; |
| 10 | + |
| 11 | +use Symfony\Component\Console\Command\Command; // for parent class |
| 12 | +use Symfony\Component\Console\Input\InputInterface; // for InputInterface used in execute method |
| 13 | +use Symfony\Component\Console\Output\OutputInterface; // for OutputInterface used in execute method |
| 14 | +use Symfony\Component\Filesystem\Filesystem; |
| 15 | + |
| 16 | +class CleanUp extends Command |
| 17 | +{ |
| 18 | + |
| 19 | + private $dirs = [ |
| 20 | + // 'generated', |
| 21 | + 'var/log', |
| 22 | + 'var/report', |
| 23 | + 'var/session' |
| 24 | + ]; |
| 25 | + |
| 26 | + private $tables = [ |
| 27 | + 'adminnotification_inbox', |
| 28 | + 'admin_passwords', |
| 29 | + 'admin_user_session', |
| 30 | + 'captcha_log', |
| 31 | + 'customer_visitor', |
| 32 | + 'customer_log', |
| 33 | + 'cron_schedule', |
| 34 | + 'report_event', |
| 35 | + 'report_viewed_product_index', |
| 36 | + ]; |
| 37 | + |
| 38 | + protected function configure() |
| 39 | + { |
| 40 | + // bin/magento cleanUp |
| 41 | + $this->setName('cleanUp') |
| 42 | + ->setDescription('Clear TMP Tables'); |
| 43 | + |
| 44 | + parent::configure(); |
| 45 | + } |
| 46 | + |
| 47 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 48 | + { |
| 49 | + $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); |
| 50 | + $resource = $objectManager->get('Magento\Framework\App\ResourceConnection'); |
| 51 | + $connection = $resource->getConnection('\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION'); |
| 52 | + $fs = new Filesystem(); |
| 53 | + try { |
| 54 | + foreach ($this->tables as $table) { |
| 55 | + $connection->truncateTable($table); |
| 56 | + $output->writeln("TRUNCATE $table"); |
| 57 | + } |
| 58 | + foreach ($this->dirs as $dir) { |
| 59 | + if($fs->exists($dir)){ |
| 60 | + $fs->remove(array($dir)); |
| 61 | + $output->writeln("Cleared $dir"); |
| 62 | + }else { |
| 63 | + $output->writeln("Can\'t find directy $dir"); |
| 64 | + } |
| 65 | + } |
| 66 | + } catch (IOExceptionInterface $e) { |
| 67 | + echo "An error occurred while deleting your directory at " . $e->getPath(); |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments