2017-09-22 21:19:57 -05:00
< ? php
2024-03-12 22:39:16 -04:00
namespace App\Console\Commands\Environment ;
2017-09-22 21:19:57 -05:00
2024-08-09 08:23:03 +02:00
use App\Traits\EnvironmentWriterTrait ;
2017-09-22 21:19:57 -05:00
use Illuminate\Console\Command ;
use Illuminate\Contracts\Console\Kernel ;
use Illuminate\Database\DatabaseManager ;
2025-09-24 13:34:19 +02:00
use PDOException ;
2017-09-22 21:19:57 -05:00
class DatabaseSettingsCommand extends Command
{
use EnvironmentWriterTrait ;
2024-04-30 00:03:00 +02:00
public const DATABASE_DRIVERS = [
'sqlite' => 'SQLite (recommended)' ,
2024-06-11 21:01:14 +02:00
'mariadb' => 'MariaDB' ,
2024-04-30 00:03:00 +02:00
'mysql' => 'MySQL' ,
];
2017-09-22 21:19:57 -05:00
protected $description = 'Configure database settings for the Panel.' ;
protected $signature = ' p : environment : database
2024-04-30 00:03:00 +02:00
{ -- driver = : The database driver backend to use . }
{ -- database = : The database to use . }
2024-06-11 21:01:14 +02:00
{ -- host = : The connection address for the MySQL / MariaDB server . }
{ -- port = : The connection port for the MySQL / MariaDB server . }
{ -- username = : Username to use when connecting to the MySQL / MariaDB server . }
{ -- password = : Password to use for the MySQL / MariaDB database . } ' ;
2017-09-22 21:19:57 -05:00
2025-03-03 14:41:19 -05:00
/** @var array<array-key, mixed> */
2022-10-14 10:59:20 -06:00
protected array $variables = [];
2017-09-22 21:19:57 -05:00
/**
* DatabaseSettingsCommand constructor .
*/
2022-10-14 10:59:20 -06:00
public function __construct ( private DatabaseManager $database , private Kernel $console )
2017-09-22 21:19:57 -05:00
{
parent :: __construct ();
}
/**
* Handle command execution .
*/
2022-10-14 10:59:20 -06:00
public function handle () : int
2017-09-22 21:19:57 -05:00
{
2024-10-08 23:45:50 +02:00
$this -> error ( 'Changing the database driver will NOT move any database data!' );
$this -> error ( 'Please make sure you made a database backup first!' );
$this -> error ( 'After changing the driver you will have to manually move the old data to the new database.' );
if ( ! $this -> confirm ( 'Do you want to continue?' )) {
return 1 ;
}
2024-04-30 00:03:00 +02:00
$selected = config ( 'database.default' , 'sqlite' );
$this -> variables [ 'DB_CONNECTION' ] = $this -> option ( 'driver' ) ? ? $this -> choice (
'Database Driver' ,
self :: DATABASE_DRIVERS ,
array_key_exists ( $selected , self :: DATABASE_DRIVERS ) ? $selected : null
2021-01-23 12:33:34 -08:00
);
2017-09-22 21:19:57 -05:00
2024-04-30 00:03:00 +02:00
if ( $this -> variables [ 'DB_CONNECTION' ] === 'mysql' ) {
2025-02-11 22:16:48 +01:00
$this -> output -> note ( trans ( 'commands.database_settings.DB_HOST_note' ));
2024-04-30 00:03:00 +02:00
$this -> variables [ 'DB_HOST' ] = $this -> option ( 'host' ) ? ? $this -> ask (
'Database Host' ,
config ( 'database.connections.mysql.host' , '127.0.0.1' )
);
$this -> variables [ 'DB_PORT' ] = $this -> option ( 'port' ) ? ? $this -> ask (
'Database Port' ,
config ( 'database.connections.mysql.port' , 3306 )
);
$this -> variables [ 'DB_DATABASE' ] = $this -> option ( 'database' ) ? ? $this -> ask (
'Database Name' ,
config ( 'database.connections.mysql.database' , 'panel' )
);
2025-02-11 22:16:48 +01:00
$this -> output -> note ( trans ( 'commands.database_settings.DB_USERNAME_note' ));
2024-04-30 00:03:00 +02:00
$this -> variables [ 'DB_USERNAME' ] = $this -> option ( 'username' ) ? ? $this -> ask (
'Database Username' ,
config ( 'database.connections.mysql.username' , 'pelican' )
);
$askForMySQLPassword = true ;
if ( ! empty ( config ( 'database.connections.mysql.password' )) && $this -> input -> isInteractive ()) {
$this -> variables [ 'DB_PASSWORD' ] = config ( 'database.connections.mysql.password' );
2025-02-11 22:16:48 +01:00
$askForMySQLPassword = $this -> confirm ( trans ( 'commands.database_settings.DB_PASSWORD_note' ));
2024-04-30 00:03:00 +02:00
}
2017-09-22 21:19:57 -05:00
2024-04-30 00:03:00 +02:00
if ( $askForMySQLPassword ) {
$this -> variables [ 'DB_PASSWORD' ] = $this -> option ( 'password' ) ? ? $this -> secret ( 'Database Password' );
}
2017-09-22 21:19:57 -05:00
2024-04-30 00:03:00 +02:00
try {
2024-06-11 21:01:14 +02:00
// Test connection
config () -> set ( 'database.connections._panel_command_test' , [
'driver' => 'mysql' ,
'host' => $this -> variables [ 'DB_HOST' ],
'port' => $this -> variables [ 'DB_PORT' ],
'database' => $this -> variables [ 'DB_DATABASE' ],
'username' => $this -> variables [ 'DB_USERNAME' ],
'password' => $this -> variables [ 'DB_PASSWORD' ],
'charset' => 'utf8mb4' ,
'collation' => 'utf8mb4_unicode_ci' ,
'strict' => true ,
]);
$this -> database -> connection ( '_panel_command_test' ) -> getPdo ();
2025-09-08 13:12:33 -04:00
} catch ( PDOException $exception ) {
2024-04-30 00:03:00 +02:00
$this -> output -> error ( sprintf ( 'Unable to connect to the MySQL server using the provided credentials. The error returned was "%s".' , $exception -> getMessage ()));
2025-02-11 22:16:48 +01:00
$this -> output -> error ( trans ( 'commands.database_settings.DB_error_2' ));
2017-09-22 21:19:57 -05:00
2025-02-11 22:16:48 +01:00
if ( $this -> confirm ( trans ( 'commands.database_settings.go_back' ))) {
2024-04-30 00:03:00 +02:00
$this -> database -> disconnect ( '_panel_command_test' );
2017-09-22 21:19:57 -05:00
2024-04-30 00:03:00 +02:00
return $this -> handle ();
}
2017-09-22 21:19:57 -05:00
2024-06-11 21:01:14 +02:00
return 1 ;
}
} elseif ( $this -> variables [ 'DB_CONNECTION' ] === 'mariadb' ) {
2025-02-11 22:16:48 +01:00
$this -> output -> note ( trans ( 'commands.database_settings.DB_HOST_note' ));
2024-06-11 21:01:14 +02:00
$this -> variables [ 'DB_HOST' ] = $this -> option ( 'host' ) ? ? $this -> ask (
'Database Host' ,
config ( 'database.connections.mariadb.host' , '127.0.0.1' )
);
$this -> variables [ 'DB_PORT' ] = $this -> option ( 'port' ) ? ? $this -> ask (
'Database Port' ,
config ( 'database.connections.mariadb.port' , 3306 )
);
$this -> variables [ 'DB_DATABASE' ] = $this -> option ( 'database' ) ? ? $this -> ask (
'Database Name' ,
config ( 'database.connections.mariadb.database' , 'panel' )
);
2025-02-11 22:16:48 +01:00
$this -> output -> note ( trans ( 'commands.database_settings.DB_USERNAME_note' ));
2024-06-11 21:01:14 +02:00
$this -> variables [ 'DB_USERNAME' ] = $this -> option ( 'username' ) ? ? $this -> ask (
'Database Username' ,
config ( 'database.connections.mariadb.username' , 'pelican' )
);
$askForMariaDBPassword = true ;
if ( ! empty ( config ( 'database.connections.mariadb.password' )) && $this -> input -> isInteractive ()) {
$this -> variables [ 'DB_PASSWORD' ] = config ( 'database.connections.mariadb.password' );
2025-02-11 22:16:48 +01:00
$askForMariaDBPassword = $this -> confirm ( trans ( 'commands.database_settings.DB_PASSWORD_note' ));
2024-06-11 21:01:14 +02:00
}
if ( $askForMariaDBPassword ) {
$this -> variables [ 'DB_PASSWORD' ] = $this -> option ( 'password' ) ? ? $this -> secret ( 'Database Password' );
}
try {
// Test connection
config () -> set ( 'database.connections._panel_command_test' , [
'driver' => 'mariadb' ,
'host' => $this -> variables [ 'DB_HOST' ],
'port' => $this -> variables [ 'DB_PORT' ],
'database' => $this -> variables [ 'DB_DATABASE' ],
'username' => $this -> variables [ 'DB_USERNAME' ],
'password' => $this -> variables [ 'DB_PASSWORD' ],
'charset' => 'utf8mb4' ,
'collation' => 'utf8mb4_unicode_ci' ,
'strict' => true ,
]);
$this -> database -> connection ( '_panel_command_test' ) -> getPdo ();
2025-09-08 13:12:33 -04:00
} catch ( PDOException $exception ) {
2024-06-11 21:01:14 +02:00
$this -> output -> error ( sprintf ( 'Unable to connect to the MariaDB server using the provided credentials. The error returned was "%s".' , $exception -> getMessage ()));
2025-02-11 22:16:48 +01:00
$this -> output -> error ( trans ( 'commands.database_settings.DB_error_2' ));
2024-06-11 21:01:14 +02:00
2025-02-11 22:16:48 +01:00
if ( $this -> confirm ( trans ( 'commands.database_settings.go_back' ))) {
2024-06-11 21:01:14 +02:00
$this -> database -> disconnect ( '_panel_command_test' );
return $this -> handle ();
}
2024-04-30 00:03:00 +02:00
return 1 ;
2017-09-22 21:19:57 -05:00
}
2024-04-30 00:03:00 +02:00
} elseif ( $this -> variables [ 'DB_CONNECTION' ] === 'sqlite' ) {
$this -> variables [ 'DB_DATABASE' ] = $this -> option ( 'database' ) ? ? $this -> ask (
'Database Path' ,
2025-03-03 14:41:19 -05:00
( string ) env ( 'DB_DATABASE' , 'database.sqlite' )
2024-04-30 00:03:00 +02:00
);
2017-09-22 21:19:57 -05:00
}
$this -> writeToEnvironment ( $this -> variables );
$this -> info ( $this -> console -> output ());
return 0 ;
}
}