| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 | 
							- <?php
 
- declare(strict_types=1);
 
- /**
 
-  * This file is part of Hyperf.
 
-  *
 
-  * @link     https://www.hyperf.io
 
-  * @document https://hyperf.wiki
 
-  * @contact  group@hyperf.io
 
-  * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 
-  */
 
- namespace Hyperf\DbConnection;
 
- use Hyperf\Database\ConnectionInterface;
 
- use Hyperf\Database\ConnectionResolverInterface;
 
- use Hyperf\DbConnection\Pool\PoolFactory;
 
- use Hyperf\Utils\Context;
 
- use Hyperf\Utils\Coroutine;
 
- use Psr\Container\ContainerInterface;
 
- class ConnectionResolver implements ConnectionResolverInterface
 
- {
 
-     /**
 
-      * The default connection name.
 
-      *
 
-      * @var string
 
-      */
 
-     protected $default = 'default';
 
-     /**
 
-      * @var PoolFactory
 
-      */
 
-     protected $factory;
 
-     /**
 
-      * @var ContainerInterface
 
-      */
 
-     protected $container;
 
-     public function __construct(ContainerInterface $container)
 
-     {
 
-         $this->container = $container;
 
-         $this->factory = $container->get(PoolFactory::class);
 
-     }
 
-     /**
 
-      * Get a database connection instance.
 
-      *
 
-      * @param string $name
 
-      * @return ConnectionInterface
 
-      */
 
-     public function connection($name = null)
 
-     {
 
-         if (is_null($name)) {
 
-             $name = $this->getDefaultConnection();
 
-         }
 
-         $connection = null;
 
-         $id = $this->getContextKey($name);
 
-         if (Context::has($id)) {
 
-             $connection = Context::get($id);
 
-         }
 
-         if (! $connection instanceof ConnectionInterface) {
 
-             $pool = $this->factory->getPool($name);
 
-             $connection = $pool->get();
 
-             try {
 
-                 // PDO is initialized as an anonymous function, so there is no IO exception,
 
-                 // but if other exceptions are thrown, the connection will not return to the connection pool properly.
 
-                 $connection = $connection->getConnection();
 
-                 Context::set($id, $connection);
 
-             } finally {
 
-                 if (Coroutine::inCoroutine()) {
 
-                     defer(function () use ($connection, $id) {
 
-                         Context::set($id, null);
 
-                         $connection->release();
 
-                     });
 
-                 }
 
-             }
 
-         }
 
-         return $connection;
 
-     }
 
-     /**
 
-      * Get the default connection name.
 
-      *
 
-      * @return string
 
-      */
 
-     public function getDefaultConnection()
 
-     {
 
-         return $this->default;
 
-     }
 
-     /**
 
-      * Set the default connection name.
 
-      *
 
-      * @param string $name
 
-      */
 
-     public function setDefaultConnection($name)
 
-     {
 
-         $this->default = $name;
 
-     }
 
-     /**
 
-      * The key to identify the connection object in coroutine context.
 
-      * @param mixed $name
 
-      */
 
-     private function getContextKey($name): string
 
-     {
 
-         return sprintf('database.connection.%s', $name);
 
-     }
 
- }
 
 
  |