SocialiteManager.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace Overtrue\Socialite;
  3. use Closure;
  4. use JetBrains\PhpStorm\Pure;
  5. class SocialiteManager implements Contracts\FactoryInterface
  6. {
  7. protected Config $config;
  8. protected array $resolved = [];
  9. protected static array $customCreators = [];
  10. protected const PROVIDERS = [
  11. Providers\Alipay::NAME => Providers\Alipay::class,
  12. Providers\Azure::NAME => Providers\Azure::class,
  13. Providers\Coding::NAME => Providers\Coding::class,
  14. Providers\DingTalk::NAME => Providers\DingTalk::class,
  15. Providers\DouYin::NAME => Providers\DouYin::class,
  16. Providers\Douban::NAME => Providers\Douban::class,
  17. Providers\Facebook::NAME => Providers\Facebook::class,
  18. Providers\FeiShu::NAME => Providers\FeiShu::class,
  19. Providers\Figma::NAME => Providers\Figma::class,
  20. Providers\GitHub::NAME => Providers\GitHub::class,
  21. Providers\Gitee::NAME => Providers\Gitee::class,
  22. Providers\Google::NAME => Providers\Google::class,
  23. Providers\Lark::NAME => Providers\Lark::class,
  24. Providers\Line::NAME => Providers\Line::class,
  25. Providers\Linkedin::NAME => Providers\Linkedin::class,
  26. Providers\OpenWeWork::NAME => Providers\OpenWeWork::class,
  27. Providers\Outlook::NAME => Providers\Outlook::class,
  28. Providers\QCloud::NAME => Providers\QCloud::class,
  29. Providers\QQ::NAME => Providers\QQ::class,
  30. Providers\Taobao::NAME => Providers\Taobao::class,
  31. Providers\Tapd::NAME => Providers\Tapd::class,
  32. Providers\TouTiao::NAME => Providers\TouTiao::class,
  33. Providers\WeChat::NAME => Providers\WeChat::class,
  34. Providers\WeWork::NAME => Providers\WeWork::class,
  35. Providers\Weibo::NAME => Providers\Weibo::class,
  36. Providers\XiGua::NAME => Providers\XiGua::class,
  37. ];
  38. #[Pure]
  39. public function __construct(array $config)
  40. {
  41. $this->config = new Config($config);
  42. }
  43. public function config(Config $config): self
  44. {
  45. $this->config = $config;
  46. return $this;
  47. }
  48. public function create(string $name): Contracts\ProviderInterface
  49. {
  50. $name = \strtolower($name);
  51. if (! isset($this->resolved[$name])) {
  52. $this->resolved[$name] = $this->createProvider($name);
  53. }
  54. return $this->resolved[$name];
  55. }
  56. public function extend(string $name, Closure $callback): self
  57. {
  58. self::$customCreators[\strtolower($name)] = $callback;
  59. return $this;
  60. }
  61. public function getResolvedProviders(): array
  62. {
  63. return $this->resolved;
  64. }
  65. public function buildProvider(string $provider, array $config): Contracts\ProviderInterface
  66. {
  67. $instance = new $provider($config);
  68. $instance instanceof Contracts\ProviderInterface || throw new Exceptions\InvalidArgumentException("The {$provider} must be instanceof ProviderInterface.");
  69. return $instance;
  70. }
  71. /**
  72. * @throws Exceptions\InvalidArgumentException
  73. */
  74. protected function createProvider(string $name): Contracts\ProviderInterface
  75. {
  76. $config = $this->config->get($name, []);
  77. $provider = $config['provider'] ?? $name;
  78. if (isset(self::$customCreators[$provider])) {
  79. return $this->callCustomCreator($provider, $config);
  80. }
  81. if (! $this->isValidProvider($provider)) {
  82. throw new Exceptions\InvalidArgumentException("Provider [{$name}] not supported.");
  83. }
  84. return $this->buildProvider(self::PROVIDERS[$provider] ?? $provider, $config);
  85. }
  86. protected function callCustomCreator(string $name, array $config): Contracts\ProviderInterface
  87. {
  88. return self::$customCreators[$name]($config);
  89. }
  90. protected function isValidProvider(string $provider): bool
  91. {
  92. return isset(self::PROVIDERS[$provider]) || \is_subclass_of($provider, Contracts\ProviderInterface::class);
  93. }
  94. }