SocialiteManager.php 3.9 KB

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