Files
kiri-core/Database/DatabasesProviders.php
T

117 lines
3.2 KiB
PHP
Raw Normal View History

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-01-19 17:59:31 +08:00
use Annotation\IAnnotation;
2020-08-31 13:58:40 +08:00
use Exception;
2021-01-19 19:35:40 +08:00
use ReflectionException;
2020-09-02 18:21:22 +08:00
use Snowflake\Abstracts\Providers;
use Snowflake\Application;
2021-01-04 16:03:10 +08:00
use Snowflake\Event;
2021-01-19 19:35:40 +08:00
use Snowflake\Exception\ComponentException;
2020-08-31 13:58:40 +08:00
use Snowflake\Exception\ConfigException;
2021-02-23 15:26:43 +08:00
use Snowflake\Exception\NotFindClassException;
use Snowflake\Exception\NotFindPropertyException;
2020-08-31 13:58:40 +08:00
use Snowflake\Snowflake;
2020-09-04 01:05:33 +08:00
use Snowflake\Abstracts\Config;
2020-08-31 13:58:40 +08:00
/**
* 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-07-03 13:24:14 +08:00
/**
* @param Application $application
* @throws Exception
*/
public function onImport(Application $application)
{
$application->set('db', $this);
Event::on(Event::SERVER_TASK_START, [$this, 'createPool']);
Event::on(Event::SERVER_WORKER_START, [$this, 'createPool']);
}
/**
* @param $name
* @return Connection
* @throws ConfigException
* @throws Exception
*/
public function get($name): Connection
{
$application = Snowflake::app();
if ($application->has('databases.' . $name)) {
return $application->get('databases.' . $name);
}
$config = $this->getConfig($name);
$max = Config::get('databases.pool.max', 30);
return $application->set('databases.' . $name, [
'class' => Connection::class,
'id' => $config['id'],
'cds' => $config['cds'],
'username' => $config['username'],
'password' => $config['password'],
'tablePrefix' => $config['tablePrefix'],
'maxNumber' => $max,
'database' => $config['database'],
'charset' => $config['charset'] ?? 'utf8mb4',
'slaveConfig' => $config['slaveConfig']
]);
}
/**
* @throws ConfigException
* @throws Exception
*/
public function createPool()
{
$databases = Config::get('databases.connections', []);
if (empty($databases)) {
return;
}
$max = Config::get('databases.pool', ['max' => 10, 'min' => 10]);
$application = Snowflake::app();
foreach ($databases as $name => $database) {
/** @var Connection $connection */
$connection = $application->set('databases.' . $name, [
'class' => Connection::class,
'id' => $database['id'],
'cds' => $database['cds'],
'username' => $database['username'],
'password' => $database['password'],
'tablePrefix' => $database['tablePrefix'],
2021-07-03 13:07:43 +08:00
'database' => $database['database'],
2021-07-03 13:24:14 +08:00
'maxNumber' => $max['max'],
'minNumber' => $max['min'],
'charset' => $database['charset'] ?? 'utf8mb4',
'slaveConfig' => $database['slaveConfig']
]);
$connection->fill();
}
}
/**
* @param $name
* @return mixed
* @throws ConfigException
*/
public function getConfig($name): mixed
{
2021-07-06 17:45:26 +08:00
return Config::get('databases.connections.' . $name, null, true);
2021-07-03 13:24:14 +08:00
}
2020-08-31 13:58:40 +08:00
}