Repository navigation
Expand file tree
/
Copy pathPoolFactory.php
More file actions
61 lines (50 loc) · 1.62 KB
/
Copy pathPoolFactory.php
File metadata and controls
61 lines (50 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\DB\Pool;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Contract\PoolInterface;
use Hyperf\DB\Exception\DriverNotFoundException;
use Hyperf\DB\Exception\InvalidDriverException;
use Hyperf\DB\PgSQL\PgSQLPool;
use Psr\Container\ContainerInterface;
use function Hyperf\Support\make;
class PoolFactory
{
/**
* @var Pool[]
*/
protected array $pools = [];
public function __construct(protected ContainerInterface $container)
{
}
public function getPool(string $name): PoolInterface
{
if (isset($this->pools[$name])) {
return $this->pools[$name];
}
$config = $this->container->get(ConfigInterface::class);
$driver = $config->get(sprintf('db.%s.driver', $name), 'mysql');
$class = $this->getPoolName($driver);
$pool = make($class, [$this->container, $name]);
if (! $pool instanceof Pool) {
throw new InvalidDriverException(sprintf('Driver %s is not invalid.', $driver));
}
return $this->pools[$name] = $pool;
}
protected function getPoolName(string $driver): string
{
return match (strtolower($driver)) {
'mysql', 'pdo' => MySQLPool::class,
'pgsql' => PgSQLPool::class,
default => class_exists($driver) ? $driver : throw new DriverNotFoundException(sprintf('Driver %s is not found.', $driver)),
};
}
}