Skip to content

Commit 1e02412

Browse files
committed
Add command bin/magento cleanUp
1 parent 9447477 commit 1e02412

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

Console/Command/CleanUp.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
}

etc/di.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0"?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
3+
<type name="Magento\Framework\Console\CommandList">
4+
<arguments>
5+
<argument name="commands" xsi:type="array">
6+
<item name="magepow_cleanUp" xsi:type="object">Magepow\Core\Console\Command\CleanUp</item>
7+
</argument>
8+
</arguments>
9+
</type>
10+
</config>

0 commit comments

Comments
 (0)