modify plugin name

This commit is contained in:
2022-02-18 15:28:29 +08:00
parent 0bd7139232
commit 0278a4aac2
2 changed files with 21 additions and 74 deletions
+10 -43
View File
@@ -75,14 +75,18 @@ class Connection extends Component
* @return void * @return void
* @throws ContainerExceptionInterface * @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface * @throws NotFoundExceptionInterface
* @throws Exception
*/ */
public function init() public function init()
{ {
$this->getEventProvider()->on(OnWorkerStop::class, [$this, 'clear_connection'], 0); $provider = $this->getEventProvider();
$this->getEventProvider()->on(OnWorkerExit::class, [$this, 'clear_connection'], 0); $provider->on(OnWorkerStop::class, [$this, 'clear_connection'], 0);
$this->getEventProvider()->on(BeginTransaction::class, [$this, 'beginTransaction'], 0); $provider->on(OnWorkerExit::class, [$this, 'clear_connection'], 0);
$this->getEventProvider()->on(Rollback::class, [$this, 'rollback'], 0); $provider->on(BeginTransaction::class, [$this, 'beginTransaction'], 0);
$this->getEventProvider()->on(Commit::class, [$this, 'commit'], 0); $provider->on(Rollback::class, [$this, 'rollback'], 0);
$provider->on(Commit::class, [$this, 'commit'], 0);
$this->connectPoolInstance();
} }
@@ -100,7 +104,7 @@ class Connection extends Component
/** /**
* @throws Exception * @throws Exception
*/ */
public function fill() public function connectPoolInstance()
{ {
$connections = $this->connections(); $connections = $this->connections();
$pool = Config::get('databases.pool.max', 10); $pool = Config::get('databases.pool.max', 10);
@@ -109,43 +113,6 @@ class Connection extends Component
if (!empty($this->slaveConfig) && $this->cds != $this->slaveConfig['cds']) { if (!empty($this->slaveConfig) && $this->cds != $this->slaveConfig['cds']) {
$connections->initConnections('Mysql:' . $this->slaveConfig['cds'], false, $pool); $connections->initConnections('Mysql:' . $this->slaveConfig['cds'], false, $pool);
} }
// $name = $connections->name('Mysql:' . $this->cds, true);
//
// $config = $this->_config();
// for ($i = 0; $i < $pool; $i++) {
// $connections->addItem($name, $connections->create($name, $config)());
// }
//
// if (empty($this->slaveConfig)) {
// return;
// }
//
// $config['cds'] = $this->slaveConfig['cds'];
// $config['username'] = $this->slaveConfig['username'];
// $config['password'] = $this->slaveConfig['password'];
//
// $name = $connections->name('Mysql:' . $config['cds'], false);
// for ($i = 0; $i < $pool; $i++) {
// $connections->addItem($name, $connections->create($name, $config)());
// }
}
/**
* @return array
*/
private function _config(): array
{
return [
'cds' => $this->cds,
'username' => $this->username,
'password' => $this->password,
'attributes' => $this->attributes,
'connect_timeout' => $this->connect_timeout,
'read_timeout' => $this->read_timeout,
'dbname' => $this->database,
'pool' => $this->pool
];
} }
+11 -31
View File
@@ -9,11 +9,7 @@ use Kiri;
use Kiri\Abstracts\Config; use Kiri\Abstracts\Config;
use Kiri\Abstracts\Providers; use Kiri\Abstracts\Providers;
use Kiri\Application; use Kiri\Application;
use Kiri\Events\OnBeforeCommandExecute;
use Kiri\Exception\ConfigException; use Kiri\Exception\ConfigException;
use Kiri\Server\Events\OnServerBeforeStart;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
/** /**
* Class DatabasesProviders * Class DatabasesProviders
@@ -26,12 +22,19 @@ class DatabasesProviders extends Providers
/** /**
* @param Application $application * @param Application $application
* @return void * @return void
* @throws ContainerExceptionInterface * @throws ConfigException
* @throws NotFoundExceptionInterface
*/ */
public function onImport(Application $application) public function onImport(Application $application)
{ {
$this->getEventProvider()->on(CreateConnectionPool::class, [$this, 'createPool']); $databases = Config::get('databases.connections', []);
if (empty($databases)) {
return;
}
$app = Kiri::app();
foreach ($databases as $key => $database) {
$app->set($key, $this->_settings($database));
}
} }
@@ -45,30 +48,6 @@ class DatabasesProviders extends Providers
return Kiri::app()->get($name); return Kiri::app()->get($name);
} }
/**
* @throws ConfigException
* @throws Exception
*/
public function createPool(OnServerBeforeStart $onWorkerStart)
{
$databases = Config::get('databases.connections', []);
if (empty($databases)) {
return;
}
$app = Kiri::app();
foreach ($databases as $key => $database) {
$database = $this->_settings($database);
$connection = Kiri::getDi()->create(Connection::class, [$database]);
$connection->fill();
$app->set($key, $connection);
}
}
/** /**
* @param $database * @param $database
* @return array * @return array
@@ -79,6 +58,7 @@ class DatabasesProviders extends Providers
return [ return [
'id' => $database['id'], 'id' => $database['id'],
'cds' => $database['cds'], 'cds' => $database['cds'],
'class' => Connection::class,
'username' => $database['username'], 'username' => $database['username'],
'password' => $database['password'], 'password' => $database['password'],
'tablePrefix' => $database['tablePrefix'], 'tablePrefix' => $database['tablePrefix'],