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
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
2020-09-02 18:21:22 +08:00
|
|
|
/**
|
|
|
|
|
* @param Application $application
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function onImport(Application $application)
|
|
|
|
|
{
|
|
|
|
|
$application->set('db', $this);
|
2021-01-04 16:03:10 +08:00
|
|
|
|
2021-04-27 18:24:38 +08:00
|
|
|
Event::on(Event::SERVER_TASK_START, [$this, 'createPool']);
|
2021-04-27 15:57:50 +08:00
|
|
|
Event::on(Event::SERVER_WORKER_START, [$this, 'createPool']);
|
2020-09-02 18:21:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-08-31 13:58:40 +08:00
|
|
|
/**
|
|
|
|
|
* @param $name
|
2020-09-11 18:53:09 +08:00
|
|
|
* @return Connection
|
2020-08-31 13:58:40 +08:00
|
|
|
* @throws ConfigException
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
public function get($name): Connection
|
2020-08-31 13:58:40 +08:00
|
|
|
{
|
2020-09-03 11:39:20 +08:00
|
|
|
$application = Snowflake::app();
|
2020-09-02 15:33:52 +08:00
|
|
|
if ($application->has('databases.' . $name)) {
|
|
|
|
|
return $application->get('databases.' . $name);
|
2020-08-31 13:58:40 +08:00
|
|
|
}
|
2020-09-02 15:33:52 +08:00
|
|
|
$config = $this->getConfig($name);
|
|
|
|
|
return $application->set('databases.' . $name, [
|
|
|
|
|
'class' => Connection::class,
|
2020-09-03 23:50:27 +08:00
|
|
|
'id' => $config['id'],
|
2020-09-02 15:33:52 +08:00
|
|
|
'cds' => $config['cds'],
|
|
|
|
|
'username' => $config['username'],
|
|
|
|
|
'password' => $config['password'],
|
|
|
|
|
'tablePrefix' => $config['tablePrefix'],
|
2020-09-05 03:52:40 +08:00
|
|
|
'maxNumber' => $config['maxNumber'],
|
2020-09-18 16:43:55 +08:00
|
|
|
'charset' => $config['charset'] ?? 'utf8mb4',
|
2020-09-02 15:33:52 +08:00
|
|
|
'slaveConfig' => $config['slaveConfig']
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-09-17 13:56:57 +08:00
|
|
|
/**
|
|
|
|
|
* @throws ConfigException
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function createPool()
|
|
|
|
|
{
|
2021-04-04 03:01:12 +08:00
|
|
|
$databases = Config::get('databases', []);
|
2020-09-17 13:56:57 +08:00
|
|
|
if (empty($databases)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$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'],
|
|
|
|
|
'maxNumber' => $database['maxNumber'],
|
2020-09-18 16:43:55 +08:00
|
|
|
'charset' => $database['charset'] ?? 'utf8mb4',
|
2020-09-17 13:56:57 +08:00
|
|
|
'slaveConfig' => $database['slaveConfig']
|
|
|
|
|
]);
|
|
|
|
|
$connection->fill();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-09-02 15:33:52 +08:00
|
|
|
/**
|
|
|
|
|
* @param $name
|
2020-12-17 14:09:14 +08:00
|
|
|
* @return mixed
|
2020-09-02 15:33:52 +08:00
|
|
|
* @throws ConfigException
|
|
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
public function getConfig($name): mixed
|
2020-09-02 15:33:52 +08:00
|
|
|
{
|
2021-04-04 03:01:12 +08:00
|
|
|
return Config::get('databases.' . $name,null, true);
|
2020-08-31 13:58:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|