|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: lenovo |
|
5
|
|
|
* Date: 6/14/2018 |
|
6
|
|
|
* Time: 9:01 PM |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace TimSDK; |
|
10
|
|
|
|
|
11
|
|
|
use TimSDK\Support\Arr; |
|
12
|
|
|
use TimSDK\Support\Str; |
|
13
|
|
|
use TimSDK\Foundation\Application; |
|
14
|
|
|
use TimSDK\Foundation\ResponseBag; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class TimCloud |
|
18
|
|
|
* @package TimSDK |
|
19
|
|
|
* @property \TimSDK\Core\IMCloud $im |
|
20
|
|
|
*/ |
|
21
|
|
|
class TimCloud extends Application |
|
22
|
|
|
{ |
|
23
|
|
|
protected $providers = [ |
|
24
|
|
|
\TimSDK\Core\ServiceProviders\IMCloudServiceProvider::class |
|
25
|
|
|
]; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct(array $config = [], array $prepends = []) |
|
28
|
|
|
{ |
|
29
|
|
|
if (Arr::has($config, ['private_key', 'public_key'])) { |
|
30
|
|
|
$config['private_key'] = $this->formatKey($config['private_key'], 'private'); |
|
31
|
|
|
$config['public_key'] = $this->formatKey($config['public_key'], 'public'); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$this->setBasePath(__DIR__); |
|
35
|
|
|
|
|
36
|
|
|
parent::__construct($config, $prepends); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function setAppId($appid) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->setConfig('app_id', $appid); |
|
42
|
|
|
|
|
43
|
|
|
return $this; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function setIdentifier($identifier) |
|
47
|
|
|
{ |
|
48
|
|
|
if (!empty($identifier)) { |
|
49
|
|
|
$this->setConfig('identifier', $identifier); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return $this; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function setPrivateKey($prikey) |
|
56
|
|
|
{ |
|
57
|
|
|
if (is_file($prikey)) { |
|
58
|
|
|
$prikey = file_get_contents($prikey); |
|
59
|
|
|
} else { |
|
60
|
|
|
$prikey = $this->formatKey($prikey, 'private'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if (!empty($prikey)) { |
|
64
|
|
|
$this->setConfig('private_key', $prikey); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $this; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function setPublicKey($pubkey) |
|
71
|
|
|
{ |
|
72
|
|
|
if (is_file($pubkey)) { |
|
73
|
|
|
$pubkey = file_get_contents($pubkey); |
|
74
|
|
|
} else { |
|
75
|
|
|
$pubkey = $this->formatKey($pubkey, 'public'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$this->setConfig('public_key', $pubkey); |
|
79
|
|
|
|
|
80
|
|
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function formatKey($key, $keyType) |
|
84
|
|
|
{ |
|
85
|
|
|
$keyType = strtoupper($keyType); |
|
86
|
|
|
|
|
87
|
|
|
if (Str::startsWith($key, "-----BEGIN $keyType KEY-----")) { |
|
88
|
|
|
return $key; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return "-----BEGIN $keyType KEY-----" . PHP_EOL . |
|
92
|
|
|
wordwrap(str_replace(["\r", "\n"], '', $key), 64, PHP_EOL, true) . |
|
93
|
|
|
PHP_EOL . "-----END $keyType KEY-----"; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Send a request |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $uri |
|
100
|
|
|
* @param string $body |
|
101
|
|
|
* @param array $options |
|
102
|
|
|
* @return ResponseBag |
|
103
|
|
|
*/ |
|
104
|
|
|
public function request($uri, $body = '', $options = []) |
|
105
|
|
|
{ |
|
106
|
|
|
return $this['im']->handle($uri, $body, $options); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
protected function setConfig($key, $value) |
|
110
|
|
|
{ |
|
111
|
|
|
if (! empty($value)) { |
|
112
|
|
|
$oldVal = $this['config']->get($key, null); |
|
113
|
|
|
if ($value !== $oldVal) { |
|
114
|
|
|
$this['config']->set($key, $value); |
|
115
|
|
|
$this['im']->needRefresh(); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|