|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the light/easemob. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) lichunqiang <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace light\Easemob; |
|
13
|
|
|
|
|
14
|
|
|
use Doctrine\Common\Cache\Cache; |
|
15
|
|
|
use Doctrine\Common\Cache\FilesystemCache; |
|
16
|
|
|
use light\Easemob\Core\AccessToken; |
|
17
|
|
|
use light\Easemob\Core\Config; |
|
18
|
|
|
use light\Easemob\Core\Http; |
|
19
|
|
|
use light\Easemob\Providers\ChatProvider; |
|
20
|
|
|
use light\Easemob\Providers\ChatRoomProvider; |
|
21
|
|
|
use light\Easemob\Providers\FileProvider; |
|
22
|
|
|
use light\Easemob\Providers\GroupProvider; |
|
23
|
|
|
use light\Easemob\Providers\MessageProvider; |
|
24
|
|
|
use light\Easemob\Providers\UserProvider; |
|
25
|
|
|
use light\Easemob\Support\Log; |
|
26
|
|
|
use Monolog\Handler\NullHandler; |
|
27
|
|
|
use Monolog\Handler\StreamHandler; |
|
28
|
|
|
use Monolog\Logger; |
|
29
|
|
|
use Pimple\Container; |
|
30
|
|
|
use Pimple\ServiceProviderInterface; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Class Easemob. |
|
34
|
|
|
* |
|
35
|
|
|
* @property \light\Easemob\Rest\User user |
|
36
|
|
|
* @property \light\Easemob\Rest\Message message |
|
37
|
|
|
* @property \light\Easemob\Rest\File file |
|
38
|
|
|
* @property \light\Easemob\Rest\Chat chat |
|
39
|
|
|
* @property \light\Easemob\Rest\ChatRoom chatroom |
|
40
|
|
|
* @property \light\Easemob\Rest\Group group |
|
41
|
|
|
*/ |
|
42
|
|
|
class Easemob extends Container |
|
43
|
|
|
{ |
|
44
|
|
|
const BASE_URL = 'https://a1.easemob.com'; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Core service providers. |
|
48
|
|
|
* |
|
49
|
|
|
* @var array |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $coreProviders = [ |
|
52
|
|
|
UserProvider::class, |
|
53
|
|
|
ChatProvider::class, |
|
54
|
|
|
ChatRoomProvider::class, |
|
55
|
|
|
FileProvider::class, |
|
56
|
|
|
GroupProvider::class, |
|
57
|
|
|
MessageProvider::class, |
|
58
|
|
|
]; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @var string |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $api; |
|
64
|
|
|
|
|
65
|
12 |
|
public function __construct(array $config) |
|
66
|
|
|
{ |
|
67
|
12 |
|
parent::__construct(); |
|
68
|
|
|
|
|
69
|
|
|
$this['config'] = function () use ($config) { |
|
70
|
12 |
|
return new Config($config); |
|
71
|
|
|
}; |
|
72
|
|
|
|
|
73
|
|
|
//init api url |
|
74
|
12 |
|
$this->api = self::BASE_URL . '/' . $this['config']['enterpriseId'] . '/' . $this['config']['appId'] . '/'; |
|
75
|
|
|
|
|
76
|
12 |
|
$this->registerCoreProviders(); |
|
|
|
|
|
|
77
|
12 |
|
$this->registerProviders(); |
|
78
|
12 |
|
$this->initializeLogger(); |
|
79
|
|
|
|
|
80
|
12 |
|
Log::debug('Configuration:', ['config' => $this['config']]); |
|
81
|
12 |
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Get all providers. |
|
85
|
|
|
* |
|
86
|
|
|
* @return array |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getProviders() |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->coreProviders; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param ServiceProviderInterface $provider |
|
95
|
|
|
* |
|
96
|
|
|
* @return $this |
|
97
|
|
|
*/ |
|
98
|
3 |
|
public function setProvider(ServiceProviderInterface $provider) |
|
99
|
|
|
{ |
|
100
|
|
|
$this->coreProviders[] = $provider; |
|
101
|
|
|
|
|
102
|
3 |
|
return $this; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param array $providers |
|
107
|
|
|
*/ |
|
108
|
|
|
public function setProviders(array $providers) |
|
109
|
|
|
{ |
|
110
|
|
|
$this->coreProviders = []; |
|
111
|
|
|
|
|
112
|
|
|
foreach ($providers as $provider) { |
|
113
|
|
|
$this->setProvider($provider); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Register providers. |
|
119
|
|
|
*/ |
|
120
|
12 |
|
protected function registerProviders() |
|
121
|
|
|
{ |
|
122
|
12 |
|
foreach ($this->coreProviders as $provider) { |
|
123
|
12 |
|
$this->register(new $provider()); |
|
124
|
12 |
|
} |
|
125
|
12 |
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Register core providers. |
|
129
|
|
|
*/ |
|
130
|
12 |
|
protected function registerCoreProviders() |
|
131
|
|
|
{ |
|
132
|
|
|
$this['http'] = function () { |
|
133
|
3 |
|
return new Http($this->api); |
|
134
|
|
|
}; |
|
135
|
|
|
|
|
136
|
|
|
$this['cache'] = function () { |
|
137
|
3 |
|
return new FilesystemCache($this['config']['cachePath'] ?: sys_get_temp_dir()); |
|
138
|
|
|
}; |
|
139
|
|
|
|
|
140
|
3 |
|
$this['access_token'] = function () { |
|
141
|
3 |
|
return new AccessToken( |
|
142
|
3 |
|
$this['config']['clientId'], |
|
143
|
3 |
|
$this['config']['clientSecret'], |
|
144
|
3 |
|
$this['http'], |
|
145
|
3 |
|
$this['cache'] |
|
146
|
3 |
|
); |
|
147
|
|
|
}; |
|
148
|
12 |
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Init logger. |
|
152
|
|
|
*/ |
|
153
|
12 |
|
private function initializeLogger() |
|
154
|
|
|
{ |
|
155
|
12 |
|
$logger = new Logger('Easemob'); |
|
156
|
|
|
|
|
157
|
12 |
|
if ($this['config']['debug']) { |
|
158
|
|
|
$logger->pushHandler(new NullHandler()); |
|
159
|
12 |
|
} elseif ($logFile = $this['config']['log.file']) { |
|
160
|
12 |
|
$logger->pushHandler(new StreamHandler( |
|
161
|
12 |
|
$logFile, |
|
162
|
12 |
|
$this['config']->get('log.level') ?: Logger::WARNING |
|
163
|
12 |
|
)); |
|
164
|
12 |
|
} |
|
165
|
|
|
|
|
166
|
12 |
|
Log::setLogger($logger); |
|
167
|
12 |
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @param string $name |
|
171
|
|
|
* |
|
172
|
|
|
* @return mixed |
|
173
|
|
|
*/ |
|
174
|
3 |
|
public function __get($name) |
|
175
|
|
|
{ |
|
176
|
3 |
|
return $this->offsetGet($name); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @param string $name |
|
181
|
|
|
* @param mixed $value |
|
182
|
|
|
*/ |
|
183
|
|
|
public function __set($name, $value) |
|
184
|
|
|
{ |
|
185
|
|
|
$this->offsetSet($name, $value); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @param string $name |
|
190
|
|
|
* |
|
191
|
|
|
* @return bool |
|
192
|
|
|
*/ |
|
193
|
|
|
public function __isset($name) |
|
194
|
|
|
{ |
|
195
|
|
|
return $this->offsetExists($name); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @param string $name |
|
200
|
|
|
*/ |
|
201
|
|
|
public function __unset($name) |
|
202
|
|
|
{ |
|
203
|
|
|
$this->offsetUnset($name); |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
|
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call: