1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Turahe\LaravelInstaller\Controllers; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\View\View; |
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
use Illuminate\Routing\Controller; |
9
|
|
|
use Illuminate\Routing\Redirector; |
10
|
|
|
use Illuminate\Support\Facades\DB; |
11
|
|
|
use Illuminate\Http\RedirectResponse; |
12
|
|
|
use Illuminate\Support\Facades\Validator; |
13
|
|
|
use Turahe\LaravelInstaller\Events\EnvironmentSaved; |
14
|
|
|
use Turahe\LaravelInstaller\Helpers\EnvironmentManager; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class EnvironmentController. |
18
|
|
|
*/ |
19
|
|
|
class EnvironmentController extends Controller |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var EnvironmentManager |
23
|
|
|
*/ |
24
|
|
|
protected EnvironmentManager $EnvironmentManager; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param EnvironmentManager $environmentManager |
28
|
|
|
*/ |
29
|
|
|
public function __construct(EnvironmentManager $environmentManager) |
30
|
|
|
{ |
31
|
|
|
$this->EnvironmentManager = $environmentManager; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Display the Environment menu page. |
36
|
|
|
* |
37
|
|
|
* @return View |
38
|
|
|
*/ |
39
|
|
|
public function environmentMenu(): View |
40
|
|
|
{ |
41
|
|
|
return view('installer::environment'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Display the Environment page. |
46
|
|
|
* |
47
|
|
|
* @return View |
48
|
|
|
*/ |
49
|
|
|
public function environmentWizard() |
50
|
|
|
{ |
51
|
|
|
$envConfig = $this->EnvironmentManager->getEnvContent(); |
52
|
|
|
|
53
|
|
|
return view('installer::environment-wizard', compact('envConfig')); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Display the Environment page. |
58
|
|
|
* |
59
|
|
|
* @return View |
60
|
|
|
*/ |
61
|
|
|
public function environmentClassic(): View |
62
|
|
|
{ |
63
|
|
|
$envConfig = $this->EnvironmentManager->getEnvContent(); |
64
|
|
|
|
65
|
|
|
return view('installer::environment-classic', compact('envConfig')); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Processes the newly saved environment configuration (Classic). |
70
|
|
|
* |
71
|
|
|
* @param Request $input |
72
|
|
|
* @param Redirector $redirect |
73
|
|
|
* @return RedirectResponse |
74
|
|
|
*/ |
75
|
|
|
public function saveClassic(Request $input, Redirector $redirect): RedirectResponse |
76
|
|
|
{ |
77
|
|
|
$message = $this->EnvironmentManager->saveFileClassic($input); |
78
|
|
|
|
79
|
|
|
event(new EnvironmentSaved($input)); |
80
|
|
|
|
81
|
|
|
return $redirect->route('LaravelInstaller::environmentClassic') |
82
|
|
|
->with(['message' => $message]); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Processes the newly saved environment configuration (Form Wizard). |
87
|
|
|
* |
88
|
|
|
* @param Request $request |
89
|
|
|
* @param Redirector $redirect |
90
|
|
|
* @return RedirectResponse |
91
|
|
|
*/ |
92
|
|
|
public function saveWizard(Request $request, Redirector $redirect): RedirectResponse |
93
|
|
|
{ |
94
|
|
|
$rules = config('installer.environment.form.rules'); |
95
|
|
|
$messages = [ |
96
|
|
|
'environment_custom.required_if' => trans('installer_messages.environment.wizard.form.name_required'), |
97
|
|
|
]; |
98
|
|
|
|
99
|
|
|
$validator = Validator::make($request->all(), $rules, $messages); |
100
|
|
|
|
101
|
|
|
if ($validator->fails()) { |
102
|
|
|
return $redirect->route('LaravelInstaller::environmentWizard')->withInput()->withErrors($validator->errors()); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if (! $this->checkDatabaseConnection($request)) { |
106
|
|
|
return $redirect->route('LaravelInstaller::environmentWizard')->withInput()->withErrors([ |
107
|
|
|
'database_connection' => trans('installer_messages.environment.wizard.form.db_connection_failed'), |
108
|
|
|
]); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$results = $this->EnvironmentManager->saveFileWizard($request); |
112
|
|
|
|
113
|
|
|
event(new EnvironmentSaved($request)); |
114
|
|
|
|
115
|
|
|
return $redirect->route('LaravelInstaller::database') |
116
|
|
|
->with(['results' => $results]); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* TODO: We can remove this code if PR will be merged: |
121
|
|
|
* https://github.com/RachidLaasri/LaravelInstaller/pull/162 |
122
|
|
|
* Validate database connection with user credentials (Form Wizard). |
123
|
|
|
* |
124
|
|
|
* @param Request $request |
125
|
|
|
* @return bool |
126
|
|
|
*/ |
127
|
|
|
private function checkDatabaseConnection(Request $request) |
128
|
|
|
{ |
129
|
|
|
$connection = $request->input('database_connection'); |
130
|
|
|
|
131
|
|
|
$settings = config("database.connections.$connection"); |
132
|
|
|
|
133
|
|
|
config([ |
134
|
|
|
'database' => [ |
135
|
|
|
'default' => $connection, |
136
|
|
|
'connections' => [ |
137
|
|
|
$connection => array_merge($settings, [ |
138
|
|
|
'driver' => $connection, |
139
|
|
|
'host' => $request->input('database_hostname'), |
140
|
|
|
'port' => $request->input('database_port'), |
141
|
|
|
'database' => $request->input('database_name'), |
142
|
|
|
'username' => $request->input('database_username'), |
143
|
|
|
'password' => $request->input('database_password'), |
144
|
|
|
]), |
145
|
|
|
], |
146
|
|
|
], |
147
|
|
|
]); |
148
|
|
|
|
149
|
|
|
DB::purge(); |
150
|
|
|
|
151
|
|
|
try { |
152
|
|
|
DB::connection()->getPdo(); |
153
|
|
|
|
154
|
|
|
return true; |
155
|
|
|
} catch (Exception $e) { |
156
|
|
|
return false; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|