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