2021-08-11 15:03:28 +08:00
|
|
|
<?php
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace Database;
|
|
|
|
|
|
|
|
|
|
|
2021-11-30 15:10:01 +08:00
|
|
|
use Note\Inject;
|
2021-08-11 15:03:28 +08:00
|
|
|
use Exception;
|
|
|
|
|
use Kiri\Abstracts\Config;
|
|
|
|
|
use Kiri\Abstracts\Providers;
|
|
|
|
|
use Kiri\Application;
|
|
|
|
|
use Kiri\Events\EventProvider;
|
|
|
|
|
use Kiri\Exception\ConfigException;
|
|
|
|
|
use Kiri\Kiri;
|
2021-11-09 10:58:57 +08:00
|
|
|
use Server\Events\OnWorkerStart;
|
2021-08-11 15:03:28 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class DatabasesProviders
|
|
|
|
|
* @package Database
|
|
|
|
|
*/
|
|
|
|
|
class DatabasesProviders extends Providers
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
{
|
2021-11-09 10:58:57 +08:00
|
|
|
$config = $this->_settings($this->getConfig($name));
|
|
|
|
|
|
|
|
|
|
return Kiri::getDi()->get(Connection::class)->configure($config);
|
2021-08-11 15:03:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @throws ConfigException
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function createPool(OnWorkerStart $onWorkerStart)
|
|
|
|
|
{
|
|
|
|
|
$databases = Config::get('databases.connections', []);
|
|
|
|
|
if (empty($databases)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-11-09 10:58:57 +08:00
|
|
|
$connection = Kiri::getDi()->get(Connection::class);
|
|
|
|
|
foreach ($databases as $database) {
|
2021-08-11 15:03:28 +08:00
|
|
|
/** @var Connection $connection */
|
2021-11-09 10:58:57 +08:00
|
|
|
$connection->configure($database)->fill();
|
2021-08-11 15:03:28 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|