|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yaro\Jarboe\Console\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Illuminate\Console\Command; |
|
7
|
|
|
use SplFileObject; |
|
8
|
|
|
|
|
9
|
|
|
class Install extends Command |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* The name and signature of the console command. |
|
13
|
|
|
* |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $signature = 'jarboe:install'; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The console command description. |
|
20
|
|
|
* |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $description = 'Configure package'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Execute the console command. |
|
27
|
|
|
* |
|
28
|
|
|
* @return mixed |
|
29
|
|
|
*/ |
|
30
|
|
|
public function handle() |
|
31
|
|
|
{ |
|
32
|
|
|
$this->publishAssets(); |
|
33
|
|
|
$this->publishConfigs(); |
|
34
|
|
|
$this->copyNavigationView(); |
|
35
|
|
|
$this->copyMigrationFiles(); |
|
36
|
|
|
$this->copyThirdPartyMigrationFiles(); |
|
37
|
|
|
|
|
38
|
|
|
if ($this->confirm("Add admin's guard and provider to auth config file?")) { |
|
39
|
|
|
$this->addAdminsGuardToConfigFile(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
if ($this->confirm('Run migrations?')) { |
|
43
|
|
|
$this->call('migrate'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$this->info('Completed'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
private function copyNavigationView() |
|
50
|
|
|
{ |
|
51
|
|
|
$this->comment('Creating navigation view:'); |
|
52
|
|
|
if ($this->isNavigationViewExists()) { |
|
53
|
|
|
$this->comment(' - already exists'); |
|
54
|
|
|
return; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
shell_exec(sprintf( |
|
58
|
|
|
'mkdir -p "%s" && cp "%s" "%s"', |
|
59
|
|
|
base_path('resources/views/vendor/jarboe/inc/'), |
|
60
|
|
|
base_path('vendor/yaro/jarboe/src/resources/views/inc/navigation.blade.php'), |
|
61
|
|
|
base_path('resources/views/vendor/jarboe/inc/navigation.blade.php') |
|
62
|
|
|
)); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
private function isNavigationViewExists() |
|
66
|
|
|
{ |
|
67
|
|
|
return file_exists(base_path('resources/views/vendor/jarboe/inc/navigation.blade.php')); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
private function copyMigrationFiles() |
|
71
|
|
|
{ |
|
72
|
|
|
$this->comment('Creating migration files:'); |
|
73
|
|
|
|
|
74
|
|
|
$date = Carbon::now(); |
|
75
|
|
|
|
|
76
|
|
View Code Duplication |
if (!$this->isMigrationFileExist('create_admins_table.php')) { |
|
|
|
|
|
|
77
|
|
|
shell_exec(sprintf( |
|
78
|
|
|
'cp "%s" "%s"', |
|
79
|
|
|
base_path('vendor/yaro/jarboe/src/database/migrations/2018_06_28_152903_create_admins_table.php'), |
|
80
|
|
|
database_path(sprintf('migrations/%s_create_admins_table.php', $date->format('Y_m_d_His'))) |
|
81
|
|
|
)); |
|
82
|
|
|
$date->addSecond(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
View Code Duplication |
if (!$this->isMigrationFileExist('add_otp_secret_column_to_admins_table.php')) { |
|
|
|
|
|
|
86
|
|
|
shell_exec(sprintf( |
|
87
|
|
|
'cp "%s" "%s"', |
|
88
|
|
|
base_path('vendor/yaro/jarboe/src/database/migrations/2019_05_18_210659_add_otp_secret_column_to_admins_table.php'), |
|
89
|
|
|
database_path(sprintf('migrations/%s_add_otp_secret_column_to_admins_table.php', $date->format('Y_m_d_His'))) |
|
90
|
|
|
)); |
|
91
|
|
|
$date->addSecond(); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
private function isMigrationFileExist($filename) |
|
96
|
|
|
{ |
|
97
|
|
|
return (bool)glob(database_path('migrations/*_'. $filename)); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
private function copyThirdPartyMigrationFiles() |
|
101
|
|
|
{ |
|
102
|
|
|
$this->comment('Publishing third-party migration files:'); |
|
103
|
|
|
if ($this->isThirdPartyMigrationFilesExist()) { |
|
104
|
|
|
$this->comment(' - already exists'); |
|
105
|
|
|
return; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$this->call('vendor:publish', [ |
|
109
|
|
|
'--provider' => 'Spatie\Permission\PermissionServiceProvider', |
|
110
|
|
|
'--tag' => 'migrations', |
|
111
|
|
|
]); |
|
112
|
|
|
$this->call('vendor:publish', [ |
|
113
|
|
|
'--provider' => 'Spatie\Permission\PermissionServiceProvider', |
|
114
|
|
|
'--tag' => 'config', |
|
115
|
|
|
]); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
private function isThirdPartyMigrationFilesExist() |
|
119
|
|
|
{ |
|
120
|
|
|
return (bool)glob(database_path('migrations/*_create_permission_tables.php')); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
private function publishAssets() |
|
124
|
|
|
{ |
|
125
|
|
|
$this->comment('Publishing public assets:'); |
|
126
|
|
|
if ($this->isAssetsPublished()) { |
|
127
|
|
|
$this->comment(' - already exists'); |
|
128
|
|
|
return; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$this->call('vendor:publish', [ |
|
132
|
|
|
'--provider' => "Yaro\Jarboe\ServiceProvider", |
|
133
|
|
|
'--tag' => 'public', |
|
134
|
|
|
'--force' => true, |
|
135
|
|
|
]); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
private function isAssetsPublished() |
|
139
|
|
|
{ |
|
140
|
|
|
return file_exists(public_path('vendor/jarboe')); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
private function publishConfigs() |
|
144
|
|
|
{ |
|
145
|
|
|
$this->comment('Publishing config files:'); |
|
146
|
|
|
if ($this->isConfigsPublished()) { |
|
147
|
|
|
$this->comment(' - already exists'); |
|
148
|
|
|
return; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
$this->call('vendor:publish', [ |
|
152
|
|
|
'--provider' => "Yaro\Jarboe\ServiceProvider", |
|
153
|
|
|
'--tag' => 'config', |
|
154
|
|
|
]); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
private function isConfigsPublished() |
|
158
|
|
|
{ |
|
159
|
|
|
return file_exists(config_path('jarboe')); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
private function addAdminsGuardToConfigFile() |
|
163
|
|
|
{ |
|
164
|
|
|
$shouldAddGuardsSection = true; |
|
165
|
|
|
if (config('auth.guards.admin')) { |
|
166
|
|
|
$shouldAddGuardsSection = false; |
|
167
|
|
|
$this->comment(' - guard [admin] already exist'); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
$shouldAddProvidersSection = true; |
|
171
|
|
|
if (config('auth.guards.providers.admins')) { |
|
172
|
|
|
$shouldAddProvidersSection = false; |
|
173
|
|
|
$this->comment(' - provider [admins] already exist'); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
if (!$shouldAddGuardsSection && !$shouldAddProvidersSection) { |
|
177
|
|
|
return; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
$configFile = config_path('auth.php'); |
|
181
|
|
|
|
|
182
|
|
|
$output = ''; |
|
183
|
|
|
$file = new SplFileObject($configFile, 'r'); |
|
184
|
|
|
foreach ($file as $lineNumber => $line) { |
|
185
|
|
|
$output .= $line; |
|
186
|
|
|
if ($line == " 'guards' => [\n" && $shouldAddGuardsSection) { |
|
187
|
|
|
$output .= " 'admin' => [\n"; |
|
188
|
|
|
$output .= " 'driver' => 'session',\n"; |
|
189
|
|
|
$output .= " 'provider' => 'admins',\n"; |
|
190
|
|
|
$output .= " ],\n\n"; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
if ($line == " 'providers' => [\n" && $shouldAddProvidersSection) { |
|
194
|
|
|
$output .= " 'admins' => [\n"; |
|
195
|
|
|
$output .= " 'driver' => 'eloquent',\n"; |
|
196
|
|
|
$output .= " 'model' => \Yaro\Jarboe\Models\Admin::class,\n"; |
|
197
|
|
|
$output .= " ],\n\n"; |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
$file = new SplFileObject($configFile, 'w+'); |
|
202
|
|
|
$file->fwrite($output); |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|
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.