Completed
Push — master ( a4261d...fc5e52 )
by John
14s
created

Update::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 1
1
<?php
2
3
namespace App\Console\Commands\Babel;
4
5
use Illuminate\Console\Command;
6
use Exception;
7
use function GuzzleHttp\json_decode;
8
use Symfony\Component\Console\Output\BufferedOutput;
9
10
class Update extends Command
11
{
12
    /**
13
     * The name and signature of the console command.
14
     *
15
     * @var string
16
     */
17
    protected $signature = 'babel:update {extension : The package name of the extension}';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'Update a given Babel Extension to NOJ';
25
26
    /**
27
     * Create a new command instance.
28
     *
29
     * @return void
30
     */
31
    public function __construct()
32
    {
33
        parent::__construct();
34
    }
35
36
    /**
37
     * Execute the console command.
38
     *
39
     * @return mixed
40
     */
41
    public function handle()
42
    {
43
        $extension = $this->argument('extension');
44
        $output = new BufferedOutput();
45
        $this->line("Updating <fg=green>$extension</>");
46
        try {
47
            $BabelConfig=json_decode(file_get_contents(babel_path("Extension/$extension/babel.json")), true);
0 ignored issues
show
Unused Code introduced by
The assignment to $BabelConfig is dead and can be removed.
Loading history...
48
        }catch(Exception $e){
49
            $this->line("\n  <bg=red;fg=white> Exception </> : <fg=yellow>babel.json parse error, The extension may not exist.</>\n");
50
            if($this->confirm("Would you like to download it from the marketspace first?")){
51
                $this->call("babel:require", ['extension' => $extension]);
52
                $output->fetch();
53
            }
54
            return;
55
        }
56
        $this->backup($extension);
57
        $this->delDir(babel_path("Extension/$extension/"));
58
        try {
59
            $this->call("babel:require", ['extension' => $extension, '--exception' => true]);
60
            $output->fetch();
61
            $this->delDir(babel_path("Tmp/backup/$extension/"));
62
            $this->line("Updated <fg=green>$extension</>");
63
        } catch(Exception $e) {
64
            $this->line($e->getMessage());
65
            $this->roolbackBackup(($extension));
66
        }
67
    }
68
69
    private function backup($extension)
70
    {
71
        if(!is_dir(babel_path("Tmp/backup/"))) mkdir(babel_path("Tmp/backup/"));
72
        if(is_dir(babel_path("Tmp/backup/$extension/"))) $this->delDir(babel_path("Tmp/backup/$extension/"));
73
        rename(babel_path("Extension/$extension/"), babel_path("Tmp/backup/$extension/"));
74
    }
75
76
    private function roolbackBackup($extension)
77
    {
78
        if(is_dir(babel_path("Extension/$extension/"))) $this->delDir(babel_path("Extension/$extension/"));
79
        rename(babel_path("Tmp/backup/$extension/"), babel_path("Extension/$extension/"));
80
    }
81
82
    private function delDir($dir){
83
        if(!is_dir($dir)) return;
84
        $it = new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS);
85
        $files = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::CHILD_FIRST);
86
        foreach($files as $file) {
87
            if ($file->isDir()){
88
                rmdir($file->getRealPath());
89
            } else {
90
                unlink($file->getRealPath());
91
            }
92
        }
93
        rmdir($dir);
94
    }
95
}
96