2020-08-31 13:58:40 +08:00
|
|
|
<?php
|
2020-10-29 18:17:25 +08:00
|
|
|
declare(strict_types=1);
|
2020-08-31 13:58:40 +08:00
|
|
|
|
|
|
|
|
namespace Database;
|
|
|
|
|
|
|
|
|
|
|
2021-08-03 18:18:09 +08:00
|
|
|
use Annotation\Inject;
|
2020-08-31 13:58:40 +08:00
|
|
|
use Exception;
|
2021-08-03 18:18:09 +08:00
|
|
|
use Server\Events\OnWorkerStart;
|
|
|
|
|
use Snowflake\Abstracts\Config;
|
2020-09-02 18:21:22 +08:00
|
|
|
use Snowflake\Abstracts\Providers;
|
|
|
|
|
use Snowflake\Application;
|
2021-08-03 18:18:09 +08:00
|
|
|
use Snowflake\Events\EventProvider;
|
2020-08-31 13:58:40 +08:00
|
|
|
use Snowflake\Exception\ConfigException;
|
|
|
|
|
use Snowflake\Snowflake;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class DatabasesProviders
|
|
|
|
|
* @package Database
|
|
|
|
|
*/
|
2020-09-02 18:21:22 +08:00
|
|
|
class DatabasesProviders extends Providers
|
2020-08-31 13:58:40 +08:00
|
|
|
{
|
|
|
|
|
|
2021-08-03 18:18:09 +08:00
|
|
|
private array $_pooLength = ['min' => 0, 'max' => 1];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var EventProvider
|
|
|
|
|
*/
|
|
|
|
|
#[Inject(EventProvider::class)]
|
|
|
|
|
public EventProvider $eventProvider;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Application $application
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function onImport(Application $application)
|
|
|
|
|
{
|
|
|
|
|
$application->set('db', $this);
|
|
|
|
|
|
|
|
|
|
$this->_pooLength = Config::get('databases.pool', ['min' => 0, 'max' => 1]);
|
|
|
|
|
|
|
|
|
|
$this->eventProvider->on(OnWorkerStart::class, [$this, 'createPool']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $name
|
|
|
|
|
* @return Connection
|
|
|
|
|
* @throws ConfigException
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function get($name): Connection
|
|
|
|
|
{
|
|
|
|
|
$application = Snowflake::app();
|
|
|
|
|
if (!$application->has('databases.' . $name)) {
|
|
|
|
|
$application->set('databases.' . $name, $this->_settings($this->getConfig($name)));
|
|
|
|
|
}
|
|
|
|
|
return $application->get('databases.' . $name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @throws ConfigException
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function createPool(OnWorkerStart $onWorkerStart)
|
|
|
|
|
{
|
|
|
|
|
$databases = Config::get('databases.connections', []);
|
|
|
|
|
if (empty($databases)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$application = Snowflake::app();
|
|
|
|
|
foreach ($databases as $name => $database) {
|
|
|
|
|
/** @var Connection $connection */
|
|
|
|
|
$application->set('databases.' . $name, $this->_settings($database));
|
|
|
|
|
$application->get('databases.' . $name)->fill();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $database
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
private function _settings($database): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'class' => Connection::class,
|
|
|
|
|
'id' => $database['id'],
|
|
|
|
|
'cds' => $database['cds'],
|
|
|
|
|
'username' => $database['username'],
|
|
|
|
|
'password' => $database['password'],
|
|
|
|
|
'tablePrefix' => $database['tablePrefix'],
|
|
|
|
|
'database' => $database['database'],
|
|
|
|
|
'maxNumber' => $this->_pooLength['max'],
|
|
|
|
|
'minNumber' => $this->_pooLength['min'],
|
|
|
|
|
'charset' => $database['charset'] ?? 'utf8mb4',
|
|
|
|
|
'slaveConfig' => $database['slaveConfig']
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $name
|
|
|
|
|
* @return mixed
|
|
|
|
|
* @throws ConfigException
|
|
|
|
|
*/
|
|
|
|
|
public function getConfig($name): mixed
|
|
|
|
|
{
|
|
|
|
|
return Config::get('databases.connections.' . $name, null, true);
|
|
|
|
|
}
|
2020-08-31 13:58:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|