Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Extensions/DynamicDatabaseConnection.php
7432 views
1
<?php
2
3
namespace Pterodactyl\Extensions;
4
5
use Pterodactyl\Models\DatabaseHost;
6
use Illuminate\Contracts\Encryption\Encrypter;
7
use Illuminate\Config\Repository as ConfigRepository;
8
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
9
10
class DynamicDatabaseConnection
11
{
12
public const DB_CHARSET = 'utf8';
13
public const DB_COLLATION = 'utf8_unicode_ci';
14
public const DB_DRIVER = 'mysql';
15
16
/**
17
* DynamicDatabaseConnection constructor.
18
*/
19
public function __construct(
20
protected ConfigRepository $config,
21
protected Encrypter $encrypter,
22
protected DatabaseHostRepositoryInterface $repository,
23
) {
24
}
25
26
/**
27
* Adds a dynamic database connection entry to the runtime config.
28
*
29
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
30
*/
31
public function set(string $connection, DatabaseHost|int $host, string $database = 'mysql'): void
32
{
33
if (!$host instanceof DatabaseHost) {
34
$host = $this->repository->find($host);
35
}
36
37
$this->config->set('database.connections.' . $connection, [
38
'driver' => self::DB_DRIVER,
39
'host' => $host->host,
40
'port' => $host->port,
41
'database' => $database,
42
'username' => $host->username,
43
'password' => $this->encrypter->decrypt($host->password),
44
'charset' => self::DB_CHARSET,
45
'collation' => self::DB_COLLATION,
46
]);
47
}
48
}
49
50