DbPool.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\DbConnection\Pool;
  12. use Hyperf\Collection\Arr;
  13. use Hyperf\Contract\ConfigInterface;
  14. use Hyperf\Contract\ConnectionInterface;
  15. use Hyperf\DbConnection\Connection;
  16. use Hyperf\DbConnection\Frequency;
  17. use Hyperf\Pool\Pool;
  18. use InvalidArgumentException;
  19. use Psr\Container\ContainerInterface;
  20. use function Hyperf\Support\make;
  21. class DbPool extends Pool
  22. {
  23. protected array $config;
  24. public function __construct(ContainerInterface $container, protected string $name)
  25. {
  26. $config = $container->get(ConfigInterface::class);
  27. $key = sprintf('databases.%s', $this->name);
  28. if (! $config->has($key)) {
  29. throw new InvalidArgumentException(sprintf('config[%s] is not exist!', $key));
  30. }
  31. // Rewrite the `name` of the configuration item to ensure that the model query builder gets the right connection.
  32. $config->set("{$key}.name", $name);
  33. $this->config = $config->get($key);
  34. $options = Arr::get($this->config, 'pool', []);
  35. $this->frequency = make(Frequency::class, [$this]);
  36. parent::__construct($container, $options);
  37. }
  38. public function getName(): string
  39. {
  40. return $this->name;
  41. }
  42. protected function createConnection(): ConnectionInterface
  43. {
  44. return new Connection($this->container, $this, $this->config);
  45. }
  46. }