1+ <?php
2+ /**
3+ *
4+ * @category: Magepow
5+ * @Copyright (c) 2014 Magepow (<https://magepow.net/>)
6+ * @authors: Magepow (<[email protected] >) 7+ * @license: <https://magepow.net/license-agreement>
8+ * @github: <https://github.com/magepownet>
9+ */
10+
11+ namespace Magepow \Core \Console \Command ;
12+
13+ use Symfony \Component \Console \Command \Command ; // for parent class
14+ use Symfony \Component \Console \Input \InputInterface ; // for InputInterface used in execute method
15+ use Symfony \Component \Console \Output \OutputInterface ; // for OutputInterface used in execute method
16+ use Symfony \Component \Filesystem \Filesystem ;
17+ use Magento \Framework \App \ResourceConnection ;
18+
19+ class CleanUp extends Command
20+ {
21+
22+ private $ dirs = [
23+ // 'generated',
24+ 'var/log ' ,
25+ 'var/report ' ,
26+ 'var/session '
27+ ];
28+
29+ private $ tables = [
30+ 'adminnotification_inbox ' ,
31+ 'admin_passwords ' ,
32+ 'admin_user_session ' ,
33+ 'captcha_log ' ,
34+ 'customer_visitor ' ,
35+ 'customer_log ' ,
36+ 'cron_schedule ' ,
37+ 'report_event ' ,
38+ 'report_viewed_product_index ' ,
39+ ];
40+
41+ /**
42+ *
43+ * @var Filesystem
44+ */
45+ private $ filesystem ;
46+
47+ /**
48+ *
49+ * @var ResourceConnection
50+ */
51+ private $ resource ;
52+
53+ /**
54+ * @param Filesystem $filesystem
55+ * @param ResourceConnection $resource
56+ * @param string $name
57+ */
58+ public function __construct (
59+ Filesystem $ filesystem = null ,
60+ ResourceConnection $ resource = null ,
61+ string $ name = null
62+ ) {
63+ $ this ->filesystem = $ filesystem ?: \Magento \Framework \App \ObjectManager::getInstance ()->get (Filesystem::class);
64+ $ this ->resource = $ resource ?: \Magento \Framework \App \ObjectManager::getInstance ()->get (ResourceConnection::class);
65+
66+ parent ::__construct ($ name );
67+ }
68+
69+ protected function configure ()
70+ {
71+ // command: bin/magento cleanUp
72+ $ this ->setName ('cleanUp ' )
73+ ->setDescription ('Clear TMP Tables & Logs ' );
74+
75+ parent ::configure ();
76+ }
77+
78+ protected function execute (InputInterface $ input , OutputInterface $ output )
79+ {
80+ $ connection = $ this ->resource ->getConnection ();
81+ $ fs = $ this ->filesystem ;
82+ try {
83+ foreach ($ this ->tables as $ table ) {
84+ $ connection ->truncateTable ($ table );
85+ $ output ->writeln ("TRUNCATE $ table " );
86+ }
87+ foreach ($ this ->dirs as $ dir ) {
88+ if ($ fs ->exists ($ dir )){
89+ $ fs ->remove (array ($ dir ));
90+ $ output ->writeln ("Cleared $ dir " );
91+ }else {
92+ $ output ->writeln ("Can\'t find directy $ dir " );
93+ }
94+ }
95+ } catch (IOExceptionInterface $ e ) {
96+ echo "An error occurred while deleting your directory at " . $ e ->getPath ();
97+ }
98+ }
99+ }
0 commit comments