|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SET\Http\Controllers\Installation; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use Illuminate\Support\Facades\Artisan; |
|
7
|
|
|
use RachidLaasri\LaravelInstaller\Helpers\EnvironmentManager; |
|
8
|
|
|
use SET\Http\Controllers\Controller; |
|
9
|
|
|
use Exception; |
|
10
|
|
|
|
|
11
|
|
|
class EnvironmentController extends Controller |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var EnvironmentManager |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $EnvironmentManager; |
|
17
|
|
|
protected $envPath; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param EnvironmentManager $environmentManager |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct(EnvironmentManager $environmentManager) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->EnvironmentManager = $environmentManager; |
|
25
|
|
|
$this->envPath = base_path('.env'); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Display the Environment page. |
|
30
|
|
|
* |
|
31
|
|
|
* @return \Illuminate\View\View |
|
32
|
|
|
*/ |
|
33
|
|
|
public function index() |
|
34
|
|
|
{ |
|
35
|
|
|
$envConfig = $this->EnvironmentManager->getEnvContent(); |
|
36
|
|
|
$fields = $this->breakApartEnv($envConfig); |
|
37
|
|
|
|
|
38
|
|
|
return view('vendor.installer.environment', compact('fields')); |
|
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Processes the newly saved environment configuration and redirects back. |
|
43
|
|
|
* |
|
44
|
|
|
* @param Request $input |
|
45
|
|
|
* |
|
46
|
|
|
* @return \Illuminate\Http\RedirectResponse |
|
47
|
|
|
*/ |
|
48
|
|
|
public function store(Request $input) |
|
49
|
|
|
{ |
|
50
|
|
|
$env = $this->flattenRequest($input); |
|
51
|
|
|
$message = $this->saveFile($env); |
|
52
|
|
|
|
|
53
|
|
|
Artisan::call('key:generate'); |
|
54
|
|
|
|
|
55
|
|
|
return redirect()->route('LaravelInstaller::requirements') |
|
56
|
|
|
->with(['message' => $message]); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Key should be before the first = symbol and value should be after. |
|
61
|
|
|
* Each new line (\r\n) should be a new array entry. |
|
62
|
|
|
* |
|
63
|
|
|
* @param $string |
|
64
|
|
|
* |
|
65
|
|
|
* @return array |
|
66
|
|
|
*/ |
|
67
|
|
|
private function breakApartEnv($string) |
|
68
|
|
|
{ |
|
69
|
|
|
preg_match_all('/([^=]*?)=([^\r\n]*?)[\r\n]+/', $string, $matches); |
|
70
|
|
|
|
|
71
|
|
|
return array_combine($matches[1], $matches[2]); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
private function flattenRequest(Request $input) |
|
75
|
|
|
{ |
|
76
|
|
|
$fields = $this->breakApartEnv($this->EnvironmentManager->getEnvContent()); |
|
77
|
|
|
$results = array_merge($fields, $input->toArray()); |
|
78
|
|
|
unset($results['_token']); |
|
79
|
|
|
|
|
80
|
|
|
$env = ''; |
|
81
|
|
|
foreach($results as $key => $value) { |
|
82
|
|
|
$env .= $key ."=". $value ."\r\n"; |
|
83
|
|
|
} |
|
84
|
|
|
return $env; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Save the edited content to the file. |
|
89
|
|
|
* |
|
90
|
|
|
* @param $string |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
|
|
public function saveFile($string) |
|
94
|
|
|
{ |
|
95
|
|
|
$message = trans('messages.environment.success'); |
|
96
|
|
|
|
|
97
|
|
|
try { |
|
98
|
|
|
file_put_contents($this->envPath, $string); |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
catch(Exception $e) { |
|
101
|
|
|
$message = trans('messages.environment.errors'); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $message; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|