Passed
Pull Request — master (#1285)
by Diego
08:26
created

AdminlteAssetsResource::install()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
c 0
b 0
f 0
nc 3
nop 0
dl 0
loc 11
rs 10
1
<?php
2
3
namespace JeroenNoten\LaravelAdminLte\Console\PackageResources;
4
5
use Illuminate\Support\Facades\File;
6
use JeroenNoten\LaravelAdminLte\Helpers\CommandHelper;
7
8
class AdminlteAssetsResource extends PackageResource
9
{
10
    /**
11
     * Create a new resource instance.
12
     *
13
     * @return void
14
     */
15
    public function __construct()
16
    {
17
        // Fill the resource data.
18
19
        $this->description = 'The set of files required to use the AdminLTE template';
20
        $this->target = public_path('vendor');
21
        $this->required = true;
22
23
        // Define the array of required assets (source).
24
25
        $adminltePath = base_path('vendor/almasaeed2010/adminlte/');
26
27
        $this->source = [
28
            'adminlte' => [
29
                'name' => 'AdminLTE v3',
30
                'source' => $adminltePath,
31
                'target' => public_path('vendor/adminlte/'),
32
                'resources' => [
33
                    [
34
                        'source' => 'dist/css',
35
                        'recursive' => false,
36
                    ],
37
                    [
38
                        'source' => 'dist/js',
39
                        'recursive' => false,
40
                        'ignore' => [
41
                            'demo.js',
42
                        ],
43
                    ],
44
                    [
45
                        'source' => 'dist/img/AdminLTELogo.png',
46
                    ],
47
                ],
48
            ],
49
            'fontawesome' => [
50
                'name' => 'FontAwesome 5 Free',
51
                'source' => $adminltePath.'/plugins/fontawesome-free',
52
                'target' => public_path('vendor/fontawesome-free'),
53
            ],
54
            'bootstrap' => [
55
                'name' => 'Bootstrap 4 (only JS files)',
56
                'source' => $adminltePath.'/plugins/bootstrap',
57
                'target' => public_path('vendor/bootstrap'),
58
            ],
59
            'popper' => [
60
                'name' => 'Popper.js (Bootstrap 4 requirement)',
61
                'source' => $adminltePath.'/plugins/popper',
62
                'target' => public_path('vendor/popper'),
63
            ],
64
            'jquery' => [
65
                'name' => 'jQuery (Bootstrap 4 requirement)',
66
                'source' => $adminltePath.'/plugins/jquery',
67
                'target' => public_path('vendor/jquery'),
68
                'ignore' => [
69
                    'core.js',
70
                    'jquery.slim.js',
71
                    'jquery.slim.min.js',
72
                    'jquery.slim.min.map',
73
                ],
74
            ],
75
            'overlay' => [
76
                'name' => 'Overlay Scrollbars',
77
                'source' => $adminltePath.'/plugins/overlayScrollbars',
78
                'target' => public_path('vendor/overlayScrollbars'),
79
            ],
80
        ];
81
82
        // Fill the set of installation messages.
83
84
        $this->messages = [
85
            'install' => 'Do you want to publish the AdminLTE asset files?',
86
            'overwrite' => 'AdminLTE asset files were already published. Want to replace?',
87
            'success' => 'AdminLTE assets files published successfully',
88
        ];
89
    }
90
91
    /**
92
     * Installs or publishes the resource.
93
     *
94
     * @return bool
95
     */
96
    public function install()
97
    {
98
        // Install the AdminLTE asset files.
99
100
        foreach ($this->source as $asset) {
101
            if (! $this->installAsset($asset)) {
102
                return false;
103
            }
104
        }
105
106
        return true;
107
    }
108
109
    /**
110
     * Uninstalls the resource.
111
     *
112
     * @return bool
113
     */
114
    public function uninstall()
115
    {
116
        // Uninstall the AdminLTE asset files.
117
118
        foreach ($this->source as $asset) {
119
            if (! $this->uninstallAsset($asset)) {
120
                return false;
121
            }
122
        }
123
124
        return true;
125
    }
126
127
    /**
128
     * Checks whether the resource already exists in the target location.
129
     *
130
     * @return bool
131
     */
132
    public function exists()
133
    {
134
        foreach ($this->source as $asset) {
135
            if ($this->assetExists($asset)) {
136
                return true;
137
            }
138
        }
139
140
        return false;
141
    }
142
143
    /**
144
     * Checks whether the resource is correctly installed, i.e. if the source
145
     * items matches with the items available at the target location.
146
     *
147
     * @return bool
148
     */
149
    public function installed()
150
    {
151
        foreach ($this->source as $asset) {
152
            if (! $this->assetInstalled($asset)) {
153
                return false;
154
            }
155
        }
156
157
        return true;
158
    }
159
160
    /**
161
     * Installs the specified AdminLTE asset.
162
     *
163
     * @param  array  $asset  An array with the asset data
164
     * @return bool
165
     */
166
    protected function installAsset($asset)
167
    {
168
        // Check if we just need to publish the entire asset.
169
170
        if (! isset($asset['resources'])) {
171
            return $this->publishResource($asset);
172
        }
173
174
        // Otherwise, publish only the specified asset resources.
175
176
        foreach ($asset['resources'] as $res) {
177
            $res['target'] = $res['target'] ?? $res['source'];
178
            $res['target'] = $asset['target'].$res['target'];
179
            $res['source'] = $asset['source'].$res['source'];
180
181
            if (! $this->publishResource($res)) {
182
                return false;
183
            }
184
        }
185
186
        return true;
187
    }
188
189
    /**
190
     * Publishes the specified resource (usually a file or folder).
191
     *
192
     * @param  array  $res  An array with the resource data
193
     * @return bool
194
     */
195
    protected function publishResource($res)
196
    {
197
        // Check whether the resource is a file or a directory.
198
199
        if (File::isDirectory($res['source'])) {
200
            return CommandHelper::copyDirectory(
201
                $res['source'],
202
                $res['target'],
203
                $res['force'] ?? true,
204
                $res['recursive'] ?? true,
205
                $res['ignore'] ?? []
206
            );
207
        }
208
209
        File::ensureDirectoryExists(File::dirname($res['target']));
210
211
        return File::copy($res['source'], $res['target']);
212
    }
213
214
    /**
215
     * Checks whether the specified asset already exists in the target location.
216
     *
217
     * @param  array  $asset  An array with the asset data
218
     * @return bool
219
     */
220
    protected function assetExists($asset)
221
    {
222
        return File::exists($asset['target']);
223
    }
224
225
    /**
226
     * Checks whether the specified asset is correctly installed.
227
     *
228
     * @param  array  $asset  An array with the asset data
229
     * @return bool
230
     */
231
    protected function assetInstalled($asset)
232
    {
233
        // Check whether the asset has resources or not.
234
235
        if (! isset($asset['resources'])) {
236
            return $this->resourceInstalled($asset);
237
        }
238
239
        foreach ($asset['resources'] as $res) {
240
            $res['target'] = $res['target'] ?? $res['source'];
241
            $res['target'] = $asset['target'].$res['target'];
242
            $res['source'] = $asset['source'].$res['source'];
243
244
            if (! $this->resourceInstalled($res)) {
245
                return false;
246
            }
247
        }
248
249
        return true;
250
    }
251
252
    /**
253
     * Checks whether the specified resource is correctly installed.
254
     *
255
     * @param  array  $res  An array with the resource data
256
     * @return bool
257
     */
258
    protected function resourceInstalled($res)
259
    {
260
        // Check whether the resource is a file or a directory.
261
262
        if (File::isDirectory($res['source'])) {
263
            return (bool) CommandHelper::compareDirectories(
264
                $res['source'],
265
                $res['target'],
266
                $res['recursive'] ?? true,
267
                $res['ignore'] ?? []
268
            );
269
        }
270
271
        return CommandHelper::compareFiles($res['source'], $res['target']);
272
    }
273
274
    /**
275
     * Uninstalls the specified asset.
276
     *
277
     * @param  array  $asset  An array with the asset data
278
     * @return bool
279
     */
280
    protected function uninstallAsset($asset)
281
    {
282
        $target = $asset['target'];
283
284
        // Uninstall the specified asset. Note the asset target location is
285
        // always a folder. When the target folder does not exists, we consider
286
        // the asset as uninstalled.
287
288
        if (File::isDirectory($target)) {
289
            return File::deleteDirectory($target);
290
        }
291
292
        return true;
293
    }
294
}
295