Application.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace addons\third\library;
  3. class Application
  4. {
  5. /**
  6. * 配置信息
  7. * @var array
  8. */
  9. private $config = [];
  10. /**
  11. * 服务提供者
  12. * @var array
  13. */
  14. private $providers = [
  15. 'qq' => 'Qq',
  16. 'weibo' => 'Weibo',
  17. 'wechat' => 'Wechat',
  18. ];
  19. /**
  20. * 服务对象信息
  21. * @var array
  22. */
  23. protected $services = [];
  24. public function __construct($options = [])
  25. {
  26. $options = array_intersect_key($options, $this->providers);
  27. $options = array_merge($this->config, is_array($options) ? $options : []);
  28. foreach ($options as $key => &$option) {
  29. $option['app_id'] = isset($option['app_id']) ? $option['app_id'] : '';
  30. $option['app_secret'] = isset($option['app_secret']) ? $option['app_secret'] : '';
  31. // 如果未定义回调地址则自动生成
  32. $option['callback'] = isset($option['callback']) && $option['callback'] ? $option['callback'] : addon_url('third/index/callback', [':platform' => $key], false, true);
  33. }
  34. $this->config = $options;
  35. //注册服务器提供者
  36. $this->registerProviders();
  37. }
  38. /**
  39. * 注册服务提供者
  40. */
  41. private function registerProviders()
  42. {
  43. foreach ($this->providers as $k => $v) {
  44. $this->services[$k] = function () use ($k, $v) {
  45. $options = $this->config[$k];
  46. $objname = __NAMESPACE__ . "\\{$v}";
  47. return new $objname($options);
  48. };
  49. }
  50. }
  51. public function __set($key, $value)
  52. {
  53. $this->services[$key] = $value;
  54. }
  55. public function __get($key)
  56. {
  57. return isset($this->services[$key]) ? $this->services[$key]($this) : null;
  58. }
  59. }