Passed
Pull Request — master (#49)
by yasin
03:38
created

Uninstall::deleteMigrations()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 12
rs 10
1
<?php
2
3
namespace EasyPanel\Commands\Actions;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Facades\DB;
7
use Illuminate\Support\Facades\File;
8
use Illuminate\Support\Facades\Schema;
9
use EasyPanel\Support\Contract\LangManager;
10
11
class Uninstall extends Command
12
{
13
14
    protected $signature = 'panel:uninstall {--f|force : Force mode}';
15
    protected $description = 'Uninstall the panel';
16
17
    public function handle()
18
    {
19
        $status = $this->option('force') ? true : $this->confirm("Do you really want to uninstall the panel ? (All files and components will be deleted)", true);
20
21
        if (!$status) {
22
            $this->info("The process was canceled");
23
            return;
24
        }
25
26
        // Delete folders and files which EasyPanel published
27
        $this->deleteFiles();
28
29
        // Drop tables which has been created by EasyPanel
30
        $this->dropTables();
31
32
        $this->deleteMigrations();
33
34
        $this->info("All files and components was deleted!");
35
    }
36
37
    private function dropTables()
38
    {
39
        Schema::dropIfExists('cruds');
40
        Schema::dropIfExists('panel_admins');
41
    }
42
43
    private function deleteFiles()
44
    {
45
        File::deleteDirectory(app_path('Http/Livewire/Admin'));
46
        File::deleteDirectory(app_path('CRUD'));
47
        File::deleteDirectory(resource_path('views/livewire/admin'));
48
        File::deleteDirectory(resource_path('views/vendor/admin'));
49
        File::deleteDirectory(resource_path('cruds'));
50
        File::deleteDirectory(public_path('assets/admin'));
51
        File::delete(config_path('easy_panel.php'));
52
        File::delete(LangManager::getFiles());
53
    }
54
55
    private function deleteMigrations()
56
    {
57
        $migrationFiles = File::allFiles(__DIR__."/../../../database/migrations");
58
59
        $migrationsTable = DB::table('migrations');
60
        foreach ($migrationFiles as $migration) {
61
            $migrationName = \Illuminate\Support\Str::between($migration->getFileName(), "999999", ".php");
62
63
            $migrationsTable->orWhere('migration', 'like', "%$migrationName");
64
        }
65
66
        $migrationsTable->delete();
67
    }
68
}
69