NacosClientFactory.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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\ConfigNacos;
  12. use Hyperf\Contract\ConfigInterface;
  13. use Hyperf\Nacos\Config;
  14. use InvalidArgumentException;
  15. use Psr\Container\ContainerInterface;
  16. class NacosClientFactory
  17. {
  18. public function __invoke(ContainerInterface $container)
  19. {
  20. $config = $container->get(ConfigInterface::class);
  21. $options = $config->get('config_center.drivers.nacos.client', []) ?: $config->get('nacos', []);
  22. if (empty($options)) {
  23. throw new InvalidArgumentException("The config 'config_center.drivers.nacos.client' is missing.");
  24. }
  25. if (! empty($options['uri'])) {
  26. $baseUri = $options['uri'];
  27. } else {
  28. $baseUri = sprintf('http://%s:%d', $options['host'] ?? '127.0.0.1', $options['port'] ?? 8848);
  29. }
  30. return new NacosClient(new Config([
  31. 'base_uri' => $baseUri,
  32. 'username' => $options['username'] ?? null,
  33. 'password' => $options['password'] ?? null,
  34. 'access_key' => $options['access_key'] ?? null,
  35. 'access_secret' => $options['access_secret'] ?? null,
  36. 'guzzle_config' => $options['guzzle']['config'] ?? null,
  37. 'host' => $options['host'] ?? null,
  38. 'port' => $options['port'] ?? null,
  39. 'grpc' => $options['grpc'] ?? [],
  40. ]));
  41. }
  42. }