NavToastrInstallCommand::packagePath()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Laravel Navigation Toastr package.
5
 *
6
 * (c) Mujtech Mujeeb <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Mujhtech\NavToastr\Console;
13
14
use Illuminate\Console\Command;
15
use Mujhtech\NavToastr\CommandHelper;
16
17
class NavToastrInstallCommand extends Command {
18
19
    protected $signature = 'nav-toastr:install '.
20
        '{--force : Overwrite existing views by default}'.
21
        '{--type= : Installation type, Available type: none, enhanced & full.}'.
22
        '{--only= : Install only specific part, Available parts: assets & config. This option can not used with the with option.}'.
23
        '{--interactive : The installation will guide you through the process}';
24
25
    protected $description = 'Install all the required files for NavToastr';
26
27
28
    protected $package_path = __DIR__.'/../../';
29
30
    protected $assets_path = 'vendor/nav-toastr/assets/';
31
32
    protected $assets_package_path = 'public/nav-toastr/';
33
34
35
36
    protected $assets = [
37
        'css' => [
38
            'name' => 'Css Folder',
39
            'package_path' => 'css',
40
            'assets_path' => 'css',
41
        ],
42
        'js' => [
43
            'name' => 'JS Folder',
44
            'package_path' => 'js',
45
            'assets_path' => 'js',
46
        ],
47
    ];
48
49
    
50
51
    /**
52
     * Execute the console command.
53
     *
54
     * @return void
55
     */
56
    public function handle()
57
    {
58
        if ($this->option('only')) {
59
            switch ($this->option('only')) {
60
            case 'config':
61
                $this->exportConfig();
62
                break;
63
            case 'assets':
64
                $this->exportAssets();
65
                break;
66
            default:
67
                $this->error('Invalid option!');
68
                break;
69
            }
70
71
            return;
72
        }
73
74
        if ($this->option('type') == 'basic' || $this->option('type') != 'none' || ! $this->option('type')) {
75
            $this->exportConfig();
76
            $this->exportAssets();
77
        }
78
79
        $this->info('Laravel NavToastr Installation complete.');
80
    }
81
82
83
    /**
84
     * Install the config file.
85
     */
86
    protected function exportConfig()
87
    {
88
        if ($this->option('interactive')) {
89
            if (! $this->confirm('Install the package config file?')) {
90
                return;
91
            }
92
        }
93
        if (file_exists(config_path('nav-toastr.php')) && ! $this->option('force')) {
94
            if (! $this->confirm('The Laravel NavToastr configuration file already exists. Do you want to replace it?')) {
95
                return;
96
            }
97
        }
98
        copy(
99
            $this->packagePath('config/nav-toastr.php'),
100
            config_path('nav-toastr.php')
101
        );
102
103
        $this->comment('Configuration Files installed successfully.');
104
    }
105
106
107
    /**
108
     * Copy all the content of the Assets Folder to Public Directory.
109
     */
110
    protected function exportAssets()
111
    {
112
        if ($this->option('interactive')) {
113
            if (! $this->confirm('Install the basic package assets?')) {
114
                return;
115
            }
116
        }
117
118
        foreach ($this->assets as $asset_key => $asset) {
119
            $this->copyAssets($asset_key, $this->option('force'));
120
        }
121
122
        $this->comment('Basic Assets Installation complete.');
123
    }
124
125
126
127
    protected function copyAssets($asset_name, $force = false)
128
    {
129
        if (! isset($this->assets[$asset_name])) {
130
            return;
131
        }
132
133
        $asset = $this->assets[$asset_name];
134
135
        if (is_array($asset['package_path'])) {
136
            foreach ($asset['package_path'] as $key => $asset_package_path) {
137
                $asset_assets_path = $asset['assets_path'][$key];
138
                CommandHelper::copyDirectory(
139
                    $this->packagePath($this->assets_package_path).$asset_package_path,
140
                    public_path($this->assets_path).$asset_assets_path,
141
                    $force,
142
                    ($asset['recursive'] ?? true),
143
                    ($asset['ignore'] ?? [])
144
                );
145
            }
146
        } else {
147
            CommandHelper::copyDirectory(
148
                $this->packagePath($this->assets_package_path).$asset['package_path'],
149
                public_path($this->assets_path).$asset['assets_path'],
150
                $force,
151
                ($asset['recursive'] ?? true),
152
                ($asset['ignore'] ?? [])
153
            );
154
        }
155
156
    }
157
158
    /**
159
     * Get Package Path.
160
     */
161
    protected function packagePath($path)
162
    {
163
        return $this->package_path.$path;
164
    }
165
166
    /**
167
     * Get Protected.
168
     *
169
     * @return array
170
     */
171
    public function getProtected($var)
172
    {
173
        return $this->{$var};
174
    }
175
176
177
}