Application.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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\Nacos;
  12. use Hyperf\Nacos\Exception\InvalidArgumentException;
  13. use Hyperf\Nacos\Provider\AuthProvider;
  14. use Hyperf\Nacos\Provider\ConfigProvider;
  15. use Hyperf\Nacos\Provider\InstanceProvider;
  16. use Hyperf\Nacos\Provider\OperatorProvider;
  17. use Hyperf\Nacos\Provider\ServiceProvider;
  18. /**
  19. * @property AuthProvider $auth
  20. * @property ConfigProvider $config
  21. * @property InstanceProvider $instance
  22. * @property OperatorProvider $operator
  23. * @property ServiceProvider $service
  24. * @property GrpcFactory $grpc
  25. */
  26. class Application
  27. {
  28. protected array $alias = [
  29. 'auth' => AuthProvider::class,
  30. 'config' => ConfigProvider::class,
  31. 'instance' => InstanceProvider::class,
  32. 'operator' => OperatorProvider::class,
  33. 'service' => ServiceProvider::class,
  34. 'grpc' => GrpcFactory::class,
  35. ];
  36. protected array $providers = [];
  37. public function __construct(protected Config $config)
  38. {
  39. }
  40. public function __get($name)
  41. {
  42. if (! isset($name) || ! isset($this->alias[$name])) {
  43. throw new InvalidArgumentException("{$name} is invalid.");
  44. }
  45. if (isset($this->providers[$name])) {
  46. return $this->providers[$name];
  47. }
  48. $class = $this->alias[$name];
  49. return $this->providers[$name] = new $class($this, $this->config);
  50. }
  51. }