Files
pterodactyl-panel/app/Extensions/DynamicDatabaseConnection.php
T

51 lines
1.4 KiB
PHP
Raw Normal View History

2017-06-17 19:48:31 -05:00
<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
2017-06-17 19:48:31 -05:00
namespace Pterodactyl\Extensions;
2017-06-17 20:52:32 -05:00
use Illuminate\Config\Repository as ConfigRepository;
2026-10-05 18:10:18 -04:00
use Illuminate\Contracts\Encryption\Encrypter;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Pterodactyl\Models\DatabaseHost;
2017-06-17 19:48:31 -05:00
class DynamicDatabaseConnection
{
2026-10-05 18:10:18 -04:00
public const string DB_CHARSET = 'utf8';
public const string DB_COLLATION = 'utf8_unicode_ci';
public const string DB_DRIVER = 'mysql';
2017-06-17 19:48:31 -05:00
/**
* DynamicDatabaseConnection constructor.
*/
public function __construct(
2022-10-14 10:59:20 -06:00
protected ConfigRepository $config,
protected Encrypter $encrypter,
2026-10-05 18:10:18 -04:00
) {}
2017-06-17 19:48:31 -05:00
/**
* Adds a dynamic database connection entry to the runtime config.
*
2026-10-05 18:10:18 -04:00
* @throws ModelNotFoundException
2017-06-17 19:48:31 -05:00
*/
2022-10-14 10:59:20 -06:00
public function set(string $connection, DatabaseHost|int $host, string $database = 'mysql'): void
2017-06-17 19:48:31 -05:00
{
2026-10-05 18:10:18 -04:00
if (! $host instanceof DatabaseHost) {
$host = DatabaseHost::query()->findOrFail($host);
2017-06-17 19:48:31 -05:00
}
2026-10-05 18:10:18 -04:00
$this->config->set('database.connections.'.$connection, [
2017-06-17 19:48:31 -05:00
'driver' => self::DB_DRIVER,
'host' => $host->host,
'port' => $host->port,
'database' => $database,
'username' => $host->username,
'password' => $this->encrypter->decrypt($host->password),
'charset' => self::DB_CHARSET,
'collation' => self::DB_COLLATION,
]);
}
}