From 409f75eff66f669097bd6dc3ce12d313e0b424e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Mon, 17 Apr 2023 01:39:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Connection.php | 53 ++++++++++++++++++------------------------ DatabasesProviders.php | 10 +------- 2 files changed, 24 insertions(+), 39 deletions(-) diff --git a/Connection.php b/Connection.php index 204d151..b34ac3c 100644 --- a/Connection.php +++ b/Connection.php @@ -75,20 +75,7 @@ class Connection extends Component private ?Schema $_schema = null; - - - /** - * @param EventProvider $eventProvider - * @param Pool $connections - * @param ContainerInterface $container - * @param array $config - * @throws Exception - */ - public function __construct(public EventProvider $eventProvider, public Pool $connections, public ContainerInterface $container, array $config = []) - { - parent::__construct(); - } - + /** * @return void @@ -96,20 +83,24 @@ class Connection extends Component */ public function init(): void { - $this->eventProvider->on(BeginTransaction::class, [$this, 'beginTransaction'], 0); - $this->eventProvider->on(Rollback::class, [$this, 'rollback'], 0); - $this->eventProvider->on(Commit::class, [$this, 'commit'], 0); + + $eventProvider = Kiri::getDi()->get(EventProvider::class); + $eventProvider->on(BeginTransaction::class, [$this, 'beginTransaction'], 0); + $eventProvider->on(Rollback::class, [$this, 'rollback'], 0); + $eventProvider->on(Commit::class, [$this, 'commit'], 0); $this->initConnections(); } - - + + /** * @return void + * @throws ReflectionException */ public function initConnections(): void { - $this->connections->initConnections($this->cds, $this->pool['max'] ?? 1, $this->gender([ + $connections = Kiri::getDi()->get(Pool::class); + $connections->initConnections($this->cds, $this->pool['max'] ?? 1, $this->gender([ 'cds' => $this->cds, 'username' => $this->username, 'password' => $this->password, @@ -175,11 +166,8 @@ class Connection extends Component */ public function getConnection(): PDO { - return $this->connections->get($this->cds); - if ($client === false) { - throw new Exception('waite db client timeout.'); - } - return $client; + $connections = Kiri::getDi()->get(Pool::class); + return $connections->get($this->cds); } @@ -237,7 +225,8 @@ class Connection extends Component if ($pdo->inTransaction()) { $pdo->rollback(); } - $this->connections->push($this->cds, $pdo); + $connections = Kiri::getDi()->get(Pool::class); + $connections->push($this->cds, $pdo); Context::remove($this->cds); } } @@ -254,7 +243,8 @@ class Connection extends Component if ($pdo->inTransaction()) { $pdo->commit(); } - $this->connections->push($this->cds, $pdo); + $connections = Kiri::getDi()->get(Pool::class); + $connections->push($this->cds, $pdo); Context::remove($this->cds); } } @@ -280,7 +270,8 @@ class Connection extends Component */ public function release(PDO $PDO) { - $this->connections->push($this->cds, $PDO); + $connections = Kiri::getDi()->get(Pool::class); + $connections->push($this->cds, $PDO); } @@ -291,7 +282,8 @@ class Connection extends Component */ public function clear_connection() { - $this->connections->clean($this->cds); + $connections = Kiri::getDi()->get(Pool::class); + $connections->clean($this->cds); } @@ -300,7 +292,8 @@ class Connection extends Component */ public function disconnect() { - $this->connections->clean($this->cds); + $connections = Kiri::getDi()->get(Pool::class); + $connections->clean($this->cds); } } diff --git a/DatabasesProviders.php b/DatabasesProviders.php index 07c141c..9118e95 100644 --- a/DatabasesProviders.php +++ b/DatabasesProviders.php @@ -23,19 +23,11 @@ use Kiri\Di\Inject\Container; class DatabasesProviders extends Providers { - /** - * @var EventProvider - */ - #[Container(EventProvider::class)] - public EventProvider $provider; - /** * @param LocalService $application * @return void - * @throws ContainerExceptionInterface - * @throws NotFoundExceptionInterface - * @throws Exception + * @throws \ReflectionException */ public function onImport(LocalService $application): void {