123 lines
3.3 KiB
PHP
123 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\Artisan;
|
|
use Illuminate\Support\Facades\File;
|
|
use Symfony\Component\Process;
|
|
use Symfony\Component\Process\Exception\ProcessFailedException;
|
|
|
|
class AdminSetup extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'setup:first';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'This command for initialize dan .env';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return int
|
|
*/
|
|
|
|
public function handle()
|
|
{
|
|
$envFile = base_path('.env');
|
|
$envExample = base_path('.env.example');
|
|
|
|
// Copy .env.example jika belum ada
|
|
if (! File::exists($envFile)) {
|
|
File::copy($envExample, $envFile);
|
|
$this->info('.env berhasil dibuat dari .env.example');
|
|
}
|
|
|
|
$dbConnection = $this->choice(
|
|
'Pilih database yang digunakan',
|
|
['mysql', 'pgsql', 'sqlite'],
|
|
1 // default pgsql
|
|
);
|
|
|
|
$dbHost = $this->ask('Masukkan IP Address / Host', '127.0.0.1');
|
|
$dbPort = $this->ask(
|
|
'Masukkan Port Database',
|
|
$dbConnection === 'mysql' ? '3306' : '5432'
|
|
);
|
|
$dbDatabase = $this->ask('Masukkan Nama Database', 'parkir');
|
|
$dbUsername = $this->ask('Masukkan Username Database', 'postgres');
|
|
$dbPassword = $this->secret('Masukkan Password Database');
|
|
|
|
$env = file_get_contents($envFile);
|
|
|
|
$replacements = [
|
|
'APP_DEBUG' => 'true',
|
|
'DB_CONNECTION' => $dbConnection,
|
|
'DB_HOST' => $dbHost,
|
|
'DB_PORT' => $dbPort,
|
|
'DB_DATABASE' => $dbDatabase,
|
|
'DB_USERNAME' => $dbUsername,
|
|
'DB_PASSWORD' => $dbPassword,
|
|
];
|
|
|
|
foreach ($replacements as $key => $value) {
|
|
$env = preg_replace(
|
|
"/^{$key}=.*/m",
|
|
"{$key}={$value}",
|
|
$env
|
|
);
|
|
}
|
|
|
|
file_put_contents($envFile, $env);
|
|
|
|
$this->info('Konfigurasi .env berhasil diperbarui.');
|
|
|
|
$this->newLine();
|
|
|
|
$this->table(
|
|
['Key', 'Value'],
|
|
[
|
|
['DB_CONNECTION', $dbConnection],
|
|
['DB_HOST', $dbHost],
|
|
['DB_PORT', $dbPort],
|
|
['DB_DATABASE', $dbDatabase],
|
|
['DB_USERNAME', $dbUsername],
|
|
]
|
|
);
|
|
|
|
$this->newLine();
|
|
|
|
if ($this->confirm('Jalankan php artisan key:generate?', true)) {
|
|
Artisan::call('key:generate');
|
|
$this->line(Artisan::output());
|
|
}
|
|
|
|
// if ($this->confirm('Jalankan php artisan migrate?', false)) {
|
|
// Artisan::call('migrate', ['--force' => true]);
|
|
// $this->line(Artisan::output());
|
|
// }
|
|
|
|
if ($this->confirm('Jalankan php artisan config:clear?', true)) {
|
|
Artisan::call('config:clear');
|
|
$this->line(Artisan::output());
|
|
}
|
|
|
|
if ($this->confirm('Jalankan php artisan cache:clear?', true)) {
|
|
Artisan::call('cache:clear');
|
|
$this->line(Artisan::output());
|
|
}
|
|
|
|
$this->info('Setup selesai.');
|
|
|
|
return Command::SUCCESS;
|
|
}
|
|
}
|