Completed
Push — master ( 6d41ca...2bcfec )
by
unknown
07:51 queued 10s
created

AdminLteInstallCommand::copyAssets()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 27

Duplication

Lines 8
Ratio 29.63 %

Importance

Changes 0
Metric Value
dl 8
loc 27
rs 8.0555
c 0
b 0
f 0
cc 9
nc 9
nop 2
1
<?php
2
3
namespace JeroenNoten\LaravelAdminLte\Console;
4
5
use Illuminate\Console\Command;
6
use JeroenNoten\LaravelAdminLte\Http\Helpers\CommandHelper;
7
8
class AdminLteInstallCommand extends Command
9
{
10
    protected $signature = 'adminlte:install '.
11
        '{--force : Overwrite existing views by default}'.
12
        '{--type= : Installation type, Available type: none, enhanced & full.}'.
13
        '{--only= : Install only specific part, Available parts: assets, config, translations, auth_views, basic_views, basic_routes & main_views. This option can not used with the with option.}'.
14
        '{--with=* : Install basic assets with specific parts, Available parts: auth_views, basic_views, basic_routes & main_views}'.
15
        '{--interactive : The installation will guide you through the process}';
16
17
    protected $description = 'Install all the required files for AdminLTE and the authentication views and routes';
18
19
    protected $authViews = [
20
        'auth/login.blade.php'             => '@extends(\'adminlte::login\')',
21
        'auth/register.blade.php'          => '@extends(\'adminlte::register\')',
22
        'auth/passwords/confirm.blade.php' => '@extends(\'adminlte::passwords.confirm\')',
23
        'auth/passwords/email.blade.php'   => '@extends(\'adminlte::passwords.email\')',
24
        'auth/passwords/reset.blade.php'   => '@extends(\'adminlte::passwords.reset\')',
25
    ];
26
27
    protected $basicViews = [
28
        'home.stub' => 'home.blade.php',
29
    ];
30
31
    protected $package_path = __DIR__.'/../../';
32
33
    protected $assets_path = 'vendor/';
34
35
    protected $assets_package_path = 'vendor/almasaeed2010/adminlte/';
36
37
    protected $assets = [
38
        'adminlte' => [
39
            'name' => 'AdminLTE v3',
40
            'package_path' => [
41
                'dist/css/',
42
                'dist/js/',
43
            ],
44
            'assets_path' => [
45
                'adminlte/dist/css',
46
                'adminlte/dist/js',
47
            ],
48
            'images_path' => 'adminlte/dist/img/',
49
            'images' => [
50
                'dist/img/AdminLTELogo.png' => 'AdminLTELogo.png',
51
            ],
52
            'recursive' => false,
53
            'ignore' => [
54
                'demo.js',
55
            ],
56
        ],
57
        'fontawesomeFree' => [
58
            'name' => 'FontAwesome 5 Free',
59
            'package_path' => 'plugins/fontawesome-free',
60
            'assets_path' => 'fontawesome-free',
61
        ],
62
        'bootstrap' => [
63
            'name' => 'Bootstrap 4 (js files only)',
64
            'package_path' => 'plugins/bootstrap',
65
            'assets_path' => 'bootstrap',
66
        ],
67
        'popper' => [
68
            'name' => 'Popper.js (Bootstrap 4 requirement)',
69
            'package_path' => 'plugins/popper',
70
            'assets_path' => 'popper',
71
        ],
72
        'jquery' => [
73
            'name' => 'jQuery (Bootstrap 4 requirement)',
74
            'package_path' => 'plugins/jquery',
75
            'assets_path' => 'jquery',
76
            'ignore' => [
77
                'core.js', 'jquery.slim.js', 'jquery.slim.min.js', 'jquery.slim.min.map',
78
            ],
79
        ],
80
        'overlayScrollbars' => [
81
            'name' => 'Overlay Scrollbars',
82
            'package_path' => 'plugins/overlayScrollbars',
83
            'assets_path' => 'overlayScrollbars',
84
        ],
85
    ];
86
87
    /**
88
     * Execute the console command.
89
     *
90
     * @return void
91
     */
92
    public function handle()
93
    {
94
        if ($this->option('only')) {
95
            switch ($this->option('only')) {
96
            case 'assets':
97
                $this->exportAssets();
98
99
                break;
100
101
            case 'config':
102
                $this->exportConfig();
103
104
                break;
105
106
            case 'translations':
107
                $this->exportTranslations();
108
109
                break;
110
111
            case 'main_views':
112
                $this->exportMainViews();
113
114
                break;
115
116
            case 'auth_views':
117
                $this->exportAuthViews();
118
119
                break;
120
121
            case 'basic_views':
122
                $this->exportBasicViews();
123
124
                break;
125
126
            case 'basic_routes':
127
                $this->exportBasicRoutes();
128
129
                break;
130
131
            default:
132
                $this->error('Invalid option!');
133
                break;
134
            }
135
136
            return;
137
        }
138
139
        if ($this->option('type') == 'basic' || $this->option('type') != 'none' || ! $this->option('type')) {
140
            $this->exportAssets();
141
            $this->exportConfig();
142
            $this->exportTranslations();
143
        }
144
145
        if ($this->option('with')) {
146
            if (in_array('main_views', $this->option('with'))) {
147
                $this->exportMainViews();
148
            }
149
            if (in_array('auth_views', $this->option('with'))) {
150
                $this->exportAuthViews();
151
            }
152
            if (in_array('basic_views', $this->option('with'))) {
153
                $this->exportBasicViews();
154
            }
155
            if (in_array('basic_routes', $this->option('with'))) {
156
                $this->exportBasicRoutes();
157
            }
158
        }
159
        if ($this->option('type') != 'none') {
160
            if ($this->option('type') == 'enhanced' || $this->option('type') == 'full') {
161
                $this->exportAuthViews();
162
            }
163
            if ($this->option('type') == 'full') {
164
                $this->exportBasicViews();
165
                $this->exportBasicRoutes();
166
            }
167
        }
168
169
        $this->info('AdminLTE Installation complete.');
170
    }
171
172
    /**
173
     * Export the main views.
174
     *
175
     * @return void
176
     */
177 View Code Duplication
    protected function exportMainViews()
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...
178
    {
179
        if ($this->option('interactive')) {
180
            if (! $this->confirm('Install AdminLTE main views?')) {
181
                return;
182
            }
183
        }
184
185
        CommandHelper::directoryCopy($this->packagePath('resources/views'), base_path('resources/views/vendor/adminlte'), $this->option('force'), true);
186
187
        $this->comment('Main views installed successfully.');
188
    }
189
190
    /**
191
     * Export the authentication views.
192
     *
193
     * @return void
194
     */
195
    protected function exportAuthViews()
196
    {
197
        if ($this->option('interactive')) {
198
            if (! $this->confirm('Install AdminLTE authentication views?')) {
199
                return;
200
            }
201
        }
202
        CommandHelper::ensureDirectoriesExist($this->getViewPath('auth/passwords'));
203
        foreach ($this->authViews as $file => $content) {
204
            file_put_contents($this->getViewPath($file), $content);
205
        }
206
        $this->comment('Authentication views installed successfully.');
207
    }
208
209
    /**
210
     * Export the basic views.
211
     *
212
     * @return void
213
     */
214
    protected function exportBasicViews()
215
    {
216
        if ($this->option('interactive')) {
217
            if (! $this->confirm('Install AdminLTE basic views?')) {
218
                return;
219
            }
220
        }
221
        foreach ($this->basicViews as $key => $value) {
222
            if (file_exists($view = $this->getViewPath($value)) && ! $this->option('force')) {
223
                if (! $this->confirm("The [{$value}] view already exists. Do you want to replace it?")) {
224
                    continue;
225
                }
226
            }
227
            copy(
228
                __DIR__.'/stubs/'.$key,
229
                $view
230
            );
231
        }
232
        $this->comment('Basic views installed successfully.');
233
    }
234
235
    /**
236
     * Export the authentication routes.
237
     *
238
     * @return void
239
     */
240
    protected function exportBasicRoutes()
241
    {
242
        if ($this->option('interactive')) {
243
            if (! $this->confirm('Install AdminLTE basic routes?')) {
244
                return;
245
            }
246
        }
247
248
        $file = file_get_contents(base_path('routes/web.php'));
249
        $newRoutes = file_get_contents(__DIR__.'/stubs/routes.stub');
250
251
        if (! strpos($file, $newRoutes)) {
252
            file_put_contents(
253
                base_path('routes/web.php'),
254
                file_get_contents(__DIR__.'/stubs/routes.stub'),
255
                FILE_APPEND
256
            );
257
            $this->comment('Basic routes installed successfully.');
258
259
            return;
260
        }
261
        $this->comment('Basic routes already installed.');
262
    }
263
264
    /**
265
     * Export the translation files.
266
     *
267
     * @return void
268
     */
269 View Code Duplication
    protected function exportTranslations()
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...
270
    {
271
        if ($this->option('interactive')) {
272
            if (! $this->confirm('Install AdminLTE authentication translations?')) {
273
                return;
274
            }
275
        }
276
277
        CommandHelper::directoryCopy($this->packagePath('resources/lang'), base_path('resources/lang'), $this->option('force'), true);
278
279
        $this->comment('Translation files installed successfully.');
280
    }
281
282
    /**
283
     * Copy all the content of the Assets Folder to Public Directory.
284
     */
285
    protected function exportAssets()
286
    {
287
        if ($this->option('interactive')) {
288
            if (! $this->confirm('Install the basic package assets?')) {
289
                return;
290
            }
291
        }
292
293
        foreach ($this->assets as $asset_key => $asset) {
294
            $this->copyAssets($asset_key, $this->option('force'));
295
        }
296
297
        $this->comment('Basic Assets Installation complete.');
298
    }
299
300
    /**
301
     * Install the config file.
302
     */
303
    protected function exportConfig()
304
    {
305
        if ($this->option('interactive')) {
306
            if (! $this->confirm('Install the package config file?')) {
307
                return;
308
            }
309
        }
310
        if (file_exists(config_path('adminlte.php')) && ! $this->option('force')) {
311
            if (! $this->confirm('The AdminLTE configuration file already exists. Do you want to replace it?')) {
312
                return;
313
            }
314
        }
315
        copy(
316
            $this->packagePath('config/adminlte.php'),
317
            config_path('adminlte.php')
318
        );
319
320
        $this->comment('Configuration Files installed successfully.');
321
    }
322
323
    /**
324
     * Get Package Path.
325
     */
326
    protected function packagePath($path)
327
    {
328
        return $this->package_path.$path;
329
    }
330
331
    /**
332
     * Get full view path relative to the application's configured view path.
333
     *
334
     * @param  string  $path
335
     * @return string
336
     */
337
    public function getViewPath($path)
338
    {
339
        return implode(DIRECTORY_SEPARATOR, [
340
            config('view.paths')[0] ?? resource_path('views'), $path,
341
        ]);
342
    }
343
344
    /**
345
     * Copy Assets Data.
346
     *
347
     * @param  string  $asset_name
348
     * @param  bool $force
349
     * @return void
350
     */
351
    protected function copyAssets($asset_name, $force = false)
352
    {
353
        if (! isset($this->assets[$asset_name])) {
354
            return;
355
        }
356
357
        $asset = $this->assets[$asset_name];
358
359 View Code Duplication
        if (is_array($asset['package_path'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
360
            foreach ($asset['package_path'] as $key => $asset_package_path) {
361
                $asset_assets_path = $asset['assets_path'][$key];
362
                CommandHelper::directoryCopy(base_path($this->assets_package_path).$asset_package_path, public_path($this->assets_path).$asset_assets_path, $force, ($asset['recursive'] ?? true), ($asset['ignore'] ?? []), ($asset['ignore_ending'] ?? null));
363
            }
364
        } else {
365
            CommandHelper::directoryCopy(base_path($this->assets_package_path).$asset['package_path'], public_path($this->assets_path).$asset['assets_path'], $force, ($asset['recursive'] ?? true), ($asset['ignore'] ?? []), ($asset['ignore_ending'] ?? null));
366
        }
367
368
        if (isset($asset['images_path']) && isset($asset['images'])) {
369
            CommandHelper::ensureDirectoriesExist(public_path($this->assets_path).$asset['images_path']);
370
            foreach ($asset['images'] as $image_package_path => $image_assets_path) {
371
                if (file_exists(public_path($this->assets_path).$asset['images_path'].$image_assets_path) && ! $force) {
372
                    continue;
373
                }
374
                copy(base_path($this->assets_package_path).$image_package_path, public_path($this->assets_path).$asset['images_path'].$image_assets_path);
375
            }
376
        }
377
    }
378
379
    /**
380
     * Get Protected.
381
     *
382
     * @return array
383
     */
384
    public function getProtected($var)
385
    {
386
        return $this->{$var};
387
    }
388
}
389