截流自动化的商城平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SocialiteManager.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. /*
  3. * This file is part of the overtrue/socialite.
  4. *
  5. * (c) overtrue <i@overtrue.me>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Overtrue\Socialite;
  11. use Closure;
  12. use InvalidArgumentException;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Session\Session;
  15. /**
  16. * Class SocialiteManager.
  17. */
  18. class SocialiteManager implements FactoryInterface
  19. {
  20. /**
  21. * The configuration.
  22. *
  23. * @var \Overtrue\Socialite\Config
  24. */
  25. protected $config;
  26. /**
  27. * The request instance.
  28. *
  29. * @var Request
  30. */
  31. protected $request;
  32. /**
  33. * The registered custom driver creators.
  34. *
  35. * @var array
  36. */
  37. protected $customCreators = [];
  38. /**
  39. * The initial drivers.
  40. *
  41. * @var array
  42. */
  43. protected $initialDrivers = [
  44. 'facebook' => 'Facebook',
  45. 'github' => 'GitHub',
  46. 'google' => 'Google',
  47. 'linkedin' => 'Linkedin',
  48. 'weibo' => 'Weibo',
  49. 'qq' => 'QQ',
  50. 'wechat' => 'WeChat',
  51. 'douban' => 'Douban',
  52. 'wework' => 'WeWork',
  53. 'outlook' => 'Outlook',
  54. 'douyin' => 'DouYin',
  55. 'taobao' => 'Taobao',
  56. 'feishu' => 'FeiShu',
  57. ];
  58. /**
  59. * The array of created "drivers".
  60. *
  61. * @var ProviderInterface[]
  62. */
  63. protected $drivers = [];
  64. /**
  65. * SocialiteManager constructor.
  66. *
  67. * @param array $config
  68. * @param Request|null $request
  69. */
  70. public function __construct(array $config, Request $request = null)
  71. {
  72. $this->config = new Config($config);
  73. if ($this->config->has('guzzle')) {
  74. Providers\AbstractProvider::setGuzzleOptions($this->config->get('guzzle'));
  75. }
  76. if ($request) {
  77. $this->setRequest($request);
  78. }
  79. }
  80. /**
  81. * Set config instance.
  82. *
  83. * @param \Overtrue\Socialite\Config $config
  84. *
  85. * @return $this
  86. */
  87. public function config(Config $config)
  88. {
  89. $this->config = $config;
  90. return $this;
  91. }
  92. /**
  93. * Get a driver instance.
  94. *
  95. * @param string $driver
  96. *
  97. * @return ProviderInterface
  98. */
  99. public function driver($driver)
  100. {
  101. $driver = strtolower($driver);
  102. if (!isset($this->drivers[$driver])) {
  103. $this->drivers[$driver] = $this->createDriver($driver);
  104. }
  105. return $this->drivers[$driver];
  106. }
  107. /**
  108. * @param \Symfony\Component\HttpFoundation\Request $request
  109. *
  110. * @return $this
  111. */
  112. public function setRequest(Request $request)
  113. {
  114. $this->request = $request;
  115. return $this;
  116. }
  117. /**
  118. * @return \Symfony\Component\HttpFoundation\Request
  119. */
  120. public function getRequest()
  121. {
  122. return $this->request ?: $this->createDefaultRequest();
  123. }
  124. /**
  125. * Create a new driver instance.
  126. *
  127. * @param string $driver
  128. *
  129. * @throws \InvalidArgumentException
  130. *
  131. * @return ProviderInterface
  132. */
  133. protected function createDriver($driver)
  134. {
  135. if (isset($this->customCreators[$driver])) {
  136. return $this->callCustomCreator($driver);
  137. }
  138. if (isset($this->initialDrivers[$driver])) {
  139. $provider = $this->initialDrivers[$driver];
  140. $provider = __NAMESPACE__.'\\Providers\\'.$provider.'Provider';
  141. return $this->buildProvider($provider, $this->formatConfig($this->config->get($driver)));
  142. }
  143. throw new InvalidArgumentException("Driver [$driver] not supported.");
  144. }
  145. /**
  146. * Call a custom driver creator.
  147. *
  148. * @param string $driver
  149. *
  150. * @return ProviderInterface
  151. */
  152. protected function callCustomCreator($driver)
  153. {
  154. return $this->customCreators[$driver]($this->config);
  155. }
  156. /**
  157. * Create default request instance.
  158. *
  159. * @return Request
  160. */
  161. protected function createDefaultRequest()
  162. {
  163. $request = Request::createFromGlobals();
  164. $session = new Session();
  165. $request->setSession($session);
  166. return $request;
  167. }
  168. /**
  169. * Register a custom driver creator Closure.
  170. *
  171. * @param string $driver
  172. * @param \Closure $callback
  173. *
  174. * @return $this
  175. */
  176. public function extend($driver, Closure $callback)
  177. {
  178. $driver = strtolower($driver);
  179. $this->customCreators[$driver] = $callback;
  180. return $this;
  181. }
  182. /**
  183. * Get all of the created "drivers".
  184. *
  185. * @return ProviderInterface[]
  186. */
  187. public function getDrivers()
  188. {
  189. return $this->drivers;
  190. }
  191. /**
  192. * Build an OAuth 2 provider instance.
  193. *
  194. * @param string $provider
  195. * @param array $config
  196. *
  197. * @return ProviderInterface
  198. */
  199. public function buildProvider($provider, $config)
  200. {
  201. return new $provider($this->getRequest(), $config);
  202. }
  203. /**
  204. * Format the server configuration.
  205. *
  206. * @param array $config
  207. *
  208. * @return array
  209. */
  210. public function formatConfig(array $config)
  211. {
  212. return array_merge([
  213. 'identifier' => $config['client_id'],
  214. 'secret' => $config['client_secret'],
  215. 'callback_uri' => $config['redirect'],
  216. ], $config);
  217. }
  218. }