From 0278a4aac2bc84a23e17a4fdd5a8e4861ec144aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Fri, 18 Feb 2022 15:28:29 +0800 Subject: [PATCH] modify plugin name --- Connection.php | 53 ++++++++---------------------------------- DatabasesProviders.php | 42 +++++++++------------------------ 2 files changed, 21 insertions(+), 74 deletions(-) diff --git a/Connection.php b/Connection.php index 2aecff0..b95b831 100644 --- a/Connection.php +++ b/Connection.php @@ -75,14 +75,18 @@ class Connection extends Component * @return void * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface + * @throws Exception */ public function init() { - $this->getEventProvider()->on(OnWorkerStop::class, [$this, 'clear_connection'], 0); - $this->getEventProvider()->on(OnWorkerExit::class, [$this, 'clear_connection'], 0); - $this->getEventProvider()->on(BeginTransaction::class, [$this, 'beginTransaction'], 0); - $this->getEventProvider()->on(Rollback::class, [$this, 'rollback'], 0); - $this->getEventProvider()->on(Commit::class, [$this, 'commit'], 0); + $provider = $this->getEventProvider(); + $provider->on(OnWorkerStop::class, [$this, 'clear_connection'], 0); + $provider->on(OnWorkerExit::class, [$this, 'clear_connection'], 0); + $provider->on(BeginTransaction::class, [$this, 'beginTransaction'], 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 */ - public function fill() + public function connectPoolInstance() { $connections = $this->connections(); $pool = Config::get('databases.pool.max', 10); @@ -109,43 +113,6 @@ class Connection extends Component if (!empty($this->slaveConfig) && $this->cds != $this->slaveConfig['cds']) { $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 - ]; } diff --git a/DatabasesProviders.php b/DatabasesProviders.php index 40d7279..4bbf8bf 100644 --- a/DatabasesProviders.php +++ b/DatabasesProviders.php @@ -9,11 +9,7 @@ use Kiri; use Kiri\Abstracts\Config; use Kiri\Abstracts\Providers; use Kiri\Application; -use Kiri\Events\OnBeforeCommandExecute; use Kiri\Exception\ConfigException; -use Kiri\Server\Events\OnServerBeforeStart; -use Psr\Container\ContainerExceptionInterface; -use Psr\Container\NotFoundExceptionInterface; /** * Class DatabasesProviders @@ -26,12 +22,19 @@ class DatabasesProviders extends Providers /** * @param Application $application * @return void - * @throws ContainerExceptionInterface - * @throws NotFoundExceptionInterface + * @throws ConfigException */ 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); } - - /** - * @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 * @return array @@ -79,6 +58,7 @@ class DatabasesProviders extends Providers return [ 'id' => $database['id'], 'cds' => $database['cds'], + 'class' => Connection::class, 'username' => $database['username'], 'password' => $database['password'], 'tablePrefix' => $database['tablePrefix'],