Browse Source

[update] add remove:all console option for empty an EPESI installation

Secret User 1 year ago
parent
commit
f7df056d1c
2 changed files with 58 additions and 0 deletions
  1. 1
    0
      console.php
  2. 57
    0
      console/RemoveAllCommand.php

+ 1
- 0
console.php View File

@@ -43,4 +43,5 @@ $application->add(new \Epesi\Console\Develop\CreateModuleCommand());
43 43
 $application->add(new \Epesi\Console\Develop\CreatePatchCommand());
44 44
 $application->add(new \Epesi\Console\ShellCommand());
45 45
 $application->add(new \Epesi\Console\RebuildAllCommand());
46
+$application->add(new \Epesi\Console\RemoveAllCommand());
46 47
 $application->run($input);

+ 57
- 0
console/RemoveAllCommand.php View File

@@ -0,0 +1,57 @@
1
+<?php
2
+
3
+namespace Epesi\Console;
4
+use DB;
5
+use Cache;
6
+use Symfony\Component\Console\Command\Command;
7
+use Symfony\Component\Console\Input\InputInterface;
8
+use Symfony\Component\Console\Output\OutputInterface;
9
+
10
+
11
+class RemoveAllCommand extends Command
12
+{
13
+    protected function configure()
14
+    {
15
+        $this
16
+            ->setName('remove:all')
17
+            ->setDescription('Truncate database and remove all data from EPESI')
18
+        ;
19
+    }
20
+
21
+    protected function execute(InputInterface $input, OutputInterface $output)
22
+    {
23
+        $dir = __DIR__ . '/../data/';
24
+
25
+        if(is_dir($dir)) {
26
+            $this->truncateDir($output, $dir);
27
+        }
28
+        else {
29
+            $output->writeln('Data directory doesn\'t exist or you don\'t have required permissions');
30
+        }
31
+        $this->truncateDb($output);
32
+        $output->writeln('EPESI removed. Please install application again');
33
+    }
34
+
35
+    private function truncateDir($output, $dir) {
36
+        $files = array_diff(scandir($dir), ['.', '..']);
37
+        foreach ($files as $file) {
38
+            if(!is_dir("$dir/$file")) unlink("$dir/$file");
39
+            else {
40
+                $handle = opendir("$dir/$file");
41
+                closedir($handle);
42
+                exec('rm -rf '."$dir/$file");
43
+            }
44
+        }
45
+        $output->writeln('Data folder cleared!');
46
+    }
47
+
48
+    private function truncateDb ($output) {
49
+        Cache::clear();
50
+        DB::Execute('SET FOREIGN_KEY_CHECKS=0;');
51
+        foreach(DB::MetaTables() as $k => $v) {
52
+            DB::DropTable($v);
53
+        }
54
+        DB::Execute('SET FOREIGN_KEY_CHECKS=1;');
55
+        $output->writeln('Database truncated!');
56
+    }
57
+}

Loading…
Cancel
Save