Completed
Push — master ( 884570...d17502 )
by Song
02:25
created

MinifyCommand::clearMinifiedFiles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Console;
4
5
use Encore\Admin\Admin;
6
use Encore\Admin\Facades\Admin as AdminFacade;
7
use Illuminate\Console\Command;
8
use Illuminate\Support\Str;
9
use MatthiasMullie\Minify;
10
11
class MinifyCommand extends Command
12
{
13
    /**
14
     * The console command name.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'admin:minify {--clear}';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Minify the CSS and JS';
26
27
    /**
28
     * Execute the console command.
29
     */
30
    public function handle()
31
    {
32
        if (!class_exists(Minify\Js::class)) {
33
            $this->error('To use `admin:minify` command, please install [matthiasmullie/minify] first.');
34
        }
35
36
        if ($this->option('clear')) {
37
            return $this->clearMinifiedFiles();
38
        }
39
40
        AdminFacade::bootstrap();
41
42
        $this->minifyCSS();
43
        $this->minifyJS();
44
45
        $this->generateManifest();
46
47
        $this->comment('JS and CSS are successfully minified:');
48
        $this->line('  ' . Admin::$min['js']);
49
        $this->line('  ' . Admin::$min['css']);
50
51
        $this->line('');
52
53
        $this->comment('Manifest successfully generated:');
54
        $this->line('  ' . Admin::$manifest);
55
    }
56
57
    protected function clearMinifiedFiles()
58
    {
59
        @unlink(public_path(Admin::$manifest));
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
60
        @unlink(public_path(Admin::$min['js']));
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
61
        @unlink(public_path(Admin::$min['css']));
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
62
63
        $this->comment('Following files are cleared:');
64
65
        $this->line('  ' . Admin::$min['js']);
66
        $this->line('  ' . Admin::$min['css']);
67
        $this->line('  ' . Admin::$manifest);
68
    }
69
70 View Code Duplication
    protected function minifyCSS()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72
        $css = collect(array_merge(Admin::$css, Admin::baseCss()))
73
            ->unique()->map(function ($css) {
74
75
                if (Str::contains($css, '?')) {
76
                    $css = substr($css, 0, strpos($css, '?'));
77
                }
78
79
                return public_path($css);
80
            });
81
82
        $minifier = new Minify\CSS();
83
84
        $minifier->add(...$css);
85
86
        $minifier->minify(public_path(Admin::$min['css']));
87
    }
88
89 View Code Duplication
    protected function minifyJS()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
    {
91
        $js = collect(array_merge(Admin::$js, Admin::baseJs()))
92
            ->unique()->map(function ($js) {
93
                if (Str::contains($js, '?')) {
94
                    $js = substr($js, 0, strpos($js, '?'));
95
                }
96
97
                return public_path($js);
98
            });
99
100
        $minifier = new Minify\JS();
101
102
        $minifier->add(...$js);
103
104
        $minifier->minify(public_path(Admin::$min['js']));
105
    }
106
107
    protected function generateManifest()
108
    {
109
        $json = collect(Admin::$min)->flatMap(function ($value) {
110
111
            return [$value => sprintf('%s?id=%s', $value, md5(uniqid()))];
112
113
        })->toJson(JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
114
115
        file_put_contents(public_path(Admin::$manifest), $json);
116
    }
117
}