Path: blob/1.0-develop/app/Extensions/DynamicDatabaseConnection.php
7432 views
<?php12namespace Pterodactyl\Extensions;34use Pterodactyl\Models\DatabaseHost;5use Illuminate\Contracts\Encryption\Encrypter;6use Illuminate\Config\Repository as ConfigRepository;7use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;89class DynamicDatabaseConnection10{11public const DB_CHARSET = 'utf8';12public const DB_COLLATION = 'utf8_unicode_ci';13public const DB_DRIVER = 'mysql';1415/**16* DynamicDatabaseConnection constructor.17*/18public function __construct(19protected ConfigRepository $config,20protected Encrypter $encrypter,21protected DatabaseHostRepositoryInterface $repository,22) {23}2425/**26* Adds a dynamic database connection entry to the runtime config.27*28* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException29*/30public function set(string $connection, DatabaseHost|int $host, string $database = 'mysql'): void31{32if (!$host instanceof DatabaseHost) {33$host = $this->repository->find($host);34}3536$this->config->set('database.connections.' . $connection, [37'driver' => self::DB_DRIVER,38'host' => $host->host,39'port' => $host->port,40'database' => $database,41'username' => $host->username,42'password' => $this->encrypter->decrypt($host->password),43'charset' => self::DB_CHARSET,44'collation' => self::DB_COLLATION,45]);46}47}484950