SocialiteManager.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace Overtrue\Socialite;
  3. use Closure;
  4. use InvalidArgumentException;
  5. use Overtrue\Socialite\Contracts\FactoryInterface;
  6. use Overtrue\Socialite\Contracts\ProviderInterface;
  7. class SocialiteManager implements FactoryInterface
  8. {
  9. protected Config $config;
  10. protected array $resolved = [];
  11. protected array $customCreators = [];
  12. protected array $providers = [
  13. Providers\QQ::NAME => Providers\QQ::class,
  14. Providers\Tapd::NAME => Providers\Tapd::class,
  15. Providers\Weibo::NAME => Providers\Weibo::class,
  16. Providers\Alipay::NAME => Providers\Alipay::class,
  17. Providers\QCloud::NAME => Providers\QCloud::class,
  18. Providers\GitHub::NAME => Providers\GitHub::class,
  19. Providers\Google::NAME => Providers\Google::class,
  20. Providers\WeChat::NAME => Providers\WeChat::class,
  21. Providers\Douban::NAME => Providers\Douban::class,
  22. Providers\WeWork::NAME => Providers\WeWork::class,
  23. Providers\DouYin::NAME => Providers\DouYin::class,
  24. Providers\Taobao::NAME => Providers\Taobao::class,
  25. Providers\FeiShu::NAME => Providers\FeiShu::class,
  26. Providers\Outlook::NAME => Providers\Outlook::class,
  27. Providers\Linkedin::NAME => Providers\Linkedin::class,
  28. Providers\Facebook::NAME => Providers\Facebook::class,
  29. Providers\DingTalk::NAME => Providers\DingTalk::class,
  30. Providers\OpenWeWork::NAME => Providers\OpenWeWork::class,
  31. Providers\Line::NAME => Providers\Line::class,
  32. Providers\Gitee::NAME => Providers\Gitee::class,
  33. ];
  34. public function __construct(array $config)
  35. {
  36. $this->config = new Config($config);
  37. }
  38. /**
  39. * @param \Overtrue\Socialite\Config $config
  40. *
  41. * @return $this
  42. */
  43. public function config(Config $config)
  44. {
  45. $this->config = $config;
  46. return $this;
  47. }
  48. /**
  49. * @param string $name
  50. *
  51. * @return \Overtrue\Socialite\Contracts\ProviderInterface
  52. */
  53. public function create(string $name): ProviderInterface
  54. {
  55. $name = strtolower($name);
  56. if (!isset($this->resolved[$name])) {
  57. $this->resolved[$name] = $this->createProvider($name);
  58. }
  59. return $this->resolved[$name];
  60. }
  61. /**
  62. * @param string $name
  63. * @param \Closure $callback
  64. *
  65. * @return $this
  66. */
  67. public function extend(string $name, Closure $callback): self
  68. {
  69. $this->customCreators[strtolower($name)] = $callback;
  70. return $this;
  71. }
  72. /**
  73. * @return \Overtrue\Socialite\Contracts\ProviderInterface[]
  74. */
  75. public function getResolvedProviders(): array
  76. {
  77. return $this->resolved;
  78. }
  79. /**
  80. * @param string $provider
  81. * @param array $config
  82. *
  83. * @return \Overtrue\Socialite\Contracts\ProviderInterface
  84. */
  85. public function buildProvider(string $provider, array $config): ProviderInterface
  86. {
  87. return new $provider($config);
  88. }
  89. /**
  90. * @param string $name
  91. *
  92. * @return ProviderInterface
  93. * @throws \InvalidArgumentException
  94. *
  95. */
  96. protected function createProvider(string $name)
  97. {
  98. $config = $this->config->get($name, []);
  99. $provider = $config['provider'] ?? $name;
  100. if (isset($this->customCreators[$provider])) {
  101. return $this->callCustomCreator($provider, $config);
  102. }
  103. if (!$this->isValidProvider($provider)) {
  104. throw new InvalidArgumentException("Provider [$provider] not supported.");
  105. }
  106. return $this->buildProvider($this->providers[$provider] ?? $provider, $config);
  107. }
  108. /**
  109. * @param string $driver
  110. * @param array $config
  111. *
  112. * @return ProviderInterface
  113. */
  114. protected function callCustomCreator(string $driver, array $config): ProviderInterface
  115. {
  116. return $this->customCreators[$driver]($config);
  117. }
  118. /**
  119. * @param string $provider
  120. *
  121. * @return bool
  122. */
  123. protected function isValidProvider(string $provider): bool
  124. {
  125. return isset($this->providers[$provider]) || is_subclass_of($provider, ProviderInterface::class);
  126. }
  127. }