Completed
Push — master ( c8a423...a23ea0 )
by Song
02:21
created

src/Console/MinifyCommand.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
     * @var array
29
     */
30
    protected $assets = [
31
        'css' => [],
32
        'js'  => [],
33
    ];
34
35
    /**
36
     * @var array
37
     */
38
    protected $excepts = [];
39
40
    /**
41
     * Execute the console command.
42
     */
43
    public function handle()
44
    {
45
        if (!class_exists(Minify\Minify::class)) {
46
            $this->error('To use `admin:minify` command, please install [matthiasmullie/minify] first.');
47
48
            return;
49
        }
50
51
        if ($this->option('clear')) {
52
            return $this->clearMinifiedFiles();
53
        }
54
55
        $this->loadExcepts();
56
57
        AdminFacade::bootstrap();
58
59
        $this->minifyCSS();
60
        $this->minifyJS();
61
62
        $this->generateManifest();
63
64
        $this->comment('JS and CSS are successfully minified:');
65
        $this->line('  '.Admin::$min['js']);
66
        $this->line('  '.Admin::$min['css']);
67
68
        $this->line('');
69
70
        $this->comment('Manifest successfully generated:');
71
        $this->line('  '.Admin::$manifest);
72
    }
73
74
    protected function loadExcepts()
75
    {
76
        $excepts = config('admin.minify_assets.excepts');
77
78
        if (is_array($excepts)) {
79
            $this->excepts = array_filter($excepts);
80
        }
81
    }
82
83
    protected function clearMinifiedFiles()
84
    {
85
        @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...
86
        @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...
87
        @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...
88
89
        $this->comment('Following files are cleared:');
90
91
        $this->line('  '.Admin::$min['js']);
92
        $this->line('  '.Admin::$min['css']);
93
        $this->line('  '.Admin::$manifest);
94
    }
95
96 View Code Duplication
    protected function minifyCSS()
97
    {
98
        $css = collect(array_merge(Admin::$css, Admin::baseCss()))
99
            ->unique()->map(function ($css) {
100
                if (url()->isValidUrl($css)) {
101
                    $this->assets['css'][] = $css;
102
103
                    return;
104
                }
105
106
                if (in_array($css, $this->excepts)) {
107
                    $this->assets['css'][] = $css;
108
109
                    return;
110
                }
111
112
                if (Str::contains($css, '?')) {
113
                    $css = substr($css, 0, strpos($css, '?'));
114
                }
115
116
                return public_path($css);
117
            })->filter();
118
119
        $minifier = new Minify\CSS();
120
121
        $minifier->add(...$css);
122
123
        $minifier->minify(public_path(Admin::$min['css']));
124
    }
125
126 View Code Duplication
    protected function minifyJS()
127
    {
128
        $js = collect(array_merge(Admin::$js, Admin::baseJs()))
129
            ->unique()->map(function ($js) {
130
                if (url()->isValidUrl($js)) {
131
                    $this->assets['js'][] = $js;
132
133
                    return;
134
                }
135
136
                if (in_array($js, $this->excepts)) {
137
                    $this->assets['js'][] = $js;
138
139
                    return;
140
                }
141
142
                if (Str::contains($js, '?')) {
143
                    $js = substr($js, 0, strpos($js, '?'));
144
                }
145
146
                return public_path($js);
147
            })->filter();
148
149
        $minifier = new Minify\JS();
150
151
        $minifier->add(...$js);
152
153
        $minifier->minify(public_path(Admin::$min['js']));
154
    }
155
156
    protected function generateManifest()
157
    {
158
        $min = collect(Admin::$min)->mapWithKeys(function ($path, $type) {
159
            return [$type => sprintf('%s?id=%s', $path, md5(uniqid()))];
160
        });
161
162
        array_unshift($this->assets['css'], $min['css']);
163
        array_unshift($this->assets['js'], $min['js']);
164
165
        $json = json_encode($this->assets, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
166
167
        file_put_contents(public_path(Admin::$manifest), $json);
168
    }
169
}
170