PoolFactory.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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\JsonRpc\Pool;
  12. use Hyperf\Di\Container;
  13. use Hyperf\Di\Exception\NotFoundException;
  14. use Psr\Container\ContainerInterface;
  15. class PoolFactory
  16. {
  17. /**
  18. * @var RpcPool[]
  19. */
  20. protected array $pools = [];
  21. public function __construct(protected ContainerInterface $container)
  22. {
  23. }
  24. /**
  25. * @throws NotFoundException
  26. */
  27. public function getPool(string $name, array $config): RpcPool
  28. {
  29. if (isset($this->pools[$name])) {
  30. return $this->pools[$name];
  31. }
  32. if ($this->container instanceof Container) {
  33. $pool = $this->container->make(RpcPool::class, ['name' => $name, 'config' => $config]);
  34. } else {
  35. $pool = new RpcPool($this->container, $name, $config);
  36. }
  37. return $this->pools[$name] = $pool;
  38. }
  39. }