1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- <?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\Pool;
- use Hyperf\Collection\Arr;
- use Hyperf\Contract\ConfigInterface;
- use Hyperf\Contract\ConnectionInterface;
- use Hyperf\DbConnection\Connection;
- use Hyperf\DbConnection\Frequency;
- use Hyperf\Pool\Pool;
- use InvalidArgumentException;
- use Psr\Container\ContainerInterface;
- use function Hyperf\Support\make;
- class DbPool extends Pool
- {
- protected array $config;
- public function __construct(ContainerInterface $container, protected string $name)
- {
- $config = $container->get(ConfigInterface::class);
- $key = sprintf('databases.%s', $this->name);
- if (! $config->has($key)) {
- throw new InvalidArgumentException(sprintf('config[%s] is not exist!', $key));
- }
- // Rewrite the `name` of the configuration item to ensure that the model query builder gets the right connection.
- $config->set("{$key}.name", $name);
- $this->config = $config->get($key);
- $options = Arr::get($this->config, 'pool', []);
- $this->frequency = make(Frequency::class, [$this]);
- parent::__construct($container, $options);
- }
- public function getName(): string
- {
- return $this->name;
- }
- protected function createConnection(): ConnectionInterface
- {
- return new Connection($this->container, $this, $this->config);
- }
- }
|