1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the overtrue/wechat. |
5
|
|
|
* |
6
|
|
|
* (c) overtrue <[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
|
|
|
/** |
13
|
|
|
* Application.php. |
14
|
|
|
* |
15
|
|
|
* Part of Overtrue\WeChat. |
16
|
|
|
* |
17
|
|
|
* For the full copyright and license information, please view the LICENSE |
18
|
|
|
* file that was distributed with this source code. |
19
|
|
|
* |
20
|
|
|
* @author overtrue <[email protected]> |
21
|
|
|
* @copyright 2015 |
22
|
|
|
* |
23
|
|
|
* @see https://github.com/overtrue |
24
|
|
|
* @see http://overtrue.me |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace EasyWeChat\Foundation; |
28
|
|
|
|
29
|
|
|
use Doctrine\Common\Cache\Cache as CacheInterface; |
30
|
|
|
use Doctrine\Common\Cache\FilesystemCache; |
31
|
|
|
use EasyWeChat\Core\AccessToken; |
32
|
|
|
use EasyWeChat\Core\Http; |
33
|
|
|
use EasyWeChat\Support\Log; |
34
|
|
|
use Monolog\Handler\HandlerInterface; |
35
|
|
|
use Monolog\Handler\NullHandler; |
36
|
|
|
use Monolog\Handler\StreamHandler; |
37
|
|
|
use Monolog\Logger; |
38
|
|
|
use Pimple\Container; |
39
|
|
|
use Symfony\Component\HttpFoundation\Request; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Class Application. |
43
|
|
|
* |
44
|
|
|
* @property \EasyWeChat\Core\AccessToken $access_token |
45
|
|
|
* @property \EasyWeChat\Server\Guard $server |
46
|
|
|
* @property \EasyWeChat\User\User $user |
47
|
|
|
* @property \EasyWeChat\User\Tag $user_tag |
48
|
|
|
* @property \EasyWeChat\User\Group $user_group |
49
|
|
|
* @property \EasyWeChat\Js\Js $js |
50
|
|
|
* @property \Overtrue\Socialite\Providers\WeChatProvider $oauth |
51
|
|
|
* @property \EasyWeChat\Menu\Menu $menu |
52
|
|
|
* @property \EasyWeChat\Notice\Notice $notice |
53
|
|
|
* @property \EasyWeChat\Material\Material $material |
54
|
|
|
* @property \EasyWeChat\Material\Temporary $material_temporary |
55
|
|
|
* @property \EasyWeChat\Staff\Staff $staff |
56
|
|
|
* @property \EasyWeChat\Url\Url $url |
57
|
|
|
* @property \EasyWeChat\QRCode\QRCode $qrcode |
58
|
|
|
* @property \EasyWeChat\Semantic\Semantic $semantic |
59
|
|
|
* @property \EasyWeChat\Stats\Stats $stats |
60
|
|
|
* @property \EasyWeChat\Payment\Merchant $merchant |
61
|
|
|
* @property \EasyWeChat\Payment\Payment $payment |
62
|
|
|
* @property \EasyWeChat\Payment\LuckyMoney\LuckyMoney $lucky_money |
63
|
|
|
* @property \EasyWeChat\Payment\MerchantPay\MerchantPay $merchant_pay |
64
|
|
|
* @property \EasyWeChat\Payment\CashCoupon\CashCoupon $cash_coupon |
65
|
|
|
* @property \EasyWeChat\Reply\Reply $reply |
66
|
|
|
* @property \EasyWeChat\Broadcast\Broadcast $broadcast |
67
|
|
|
* @property \EasyWeChat\Card\Card $card |
68
|
|
|
* @property \EasyWeChat\Device\Device $device |
69
|
|
|
* @property \EasyWeChat\ShakeAround\ShakeAround $shakearound |
70
|
|
|
* @property \EasyWeChat\OpenPlatform\OpenPlatform $open_platform |
71
|
|
|
* @property \EasyWeChat\MiniProgram\MiniProgram $mini_program |
72
|
|
|
*/ |
73
|
|
|
class Application extends Container |
74
|
|
|
{ |
75
|
|
|
/** |
76
|
|
|
* Service Providers. |
77
|
|
|
* |
78
|
|
|
* @var array |
79
|
|
|
*/ |
80
|
|
|
protected $providers = [ |
81
|
|
|
ServiceProviders\ServerServiceProvider::class, |
82
|
|
|
ServiceProviders\UserServiceProvider::class, |
83
|
|
|
ServiceProviders\JsServiceProvider::class, |
84
|
|
|
ServiceProviders\OAuthServiceProvider::class, |
85
|
|
|
ServiceProviders\MenuServiceProvider::class, |
86
|
|
|
ServiceProviders\NoticeServiceProvider::class, |
87
|
|
|
ServiceProviders\MaterialServiceProvider::class, |
88
|
|
|
ServiceProviders\StaffServiceProvider::class, |
89
|
|
|
ServiceProviders\UrlServiceProvider::class, |
90
|
|
|
ServiceProviders\QRCodeServiceProvider::class, |
91
|
|
|
ServiceProviders\SemanticServiceProvider::class, |
92
|
|
|
ServiceProviders\StatsServiceProvider::class, |
93
|
|
|
ServiceProviders\PaymentServiceProvider::class, |
94
|
|
|
ServiceProviders\POIServiceProvider::class, |
95
|
|
|
ServiceProviders\ReplyServiceProvider::class, |
96
|
|
|
ServiceProviders\BroadcastServiceProvider::class, |
97
|
|
|
ServiceProviders\CardServiceProvider::class, |
98
|
|
|
ServiceProviders\DeviceServiceProvider::class, |
99
|
|
|
ServiceProviders\ShakeAroundServiceProvider::class, |
100
|
|
|
ServiceProviders\OpenPlatformServiceProvider::class, |
101
|
|
|
ServiceProviders\MiniProgramServiceProvider::class, |
102
|
|
|
]; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Application constructor. |
106
|
|
|
* |
107
|
|
|
* @param array $config |
108
|
|
|
*/ |
109
|
8 |
|
public function __construct($config) |
110
|
|
|
{ |
111
|
8 |
|
parent::__construct(); |
112
|
|
|
|
113
|
|
|
$this['config'] = function () use ($config) { |
114
|
8 |
|
return new Config($config); |
115
|
|
|
}; |
116
|
|
|
|
117
|
8 |
|
if ($this['config']['debug']) { |
118
|
|
|
error_reporting(E_ALL); |
119
|
|
|
} |
120
|
|
|
|
121
|
8 |
|
$this->registerProviders(); |
122
|
8 |
|
$this->registerBase(); |
123
|
8 |
|
$this->initializeLogger(); |
124
|
|
|
|
125
|
8 |
|
Http::setDefaultOptions($this['config']->get('guzzle', ['timeout' => 5.0])); |
126
|
|
|
|
127
|
8 |
|
$this->logConfiguration($config); |
128
|
8 |
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Log configuration. |
132
|
|
|
* |
133
|
|
|
* @param array $config |
134
|
|
|
*/ |
135
|
8 |
|
public function logConfiguration($config) |
136
|
|
|
{ |
137
|
8 |
|
$config = new Config($config); |
138
|
|
|
|
139
|
8 |
|
$keys = ['app_id', 'secret', 'open_platform.app_id', 'open_platform.secret', 'mini_program.app_id', 'mini_program.secret']; |
140
|
8 |
|
foreach ($keys as $key) { |
141
|
8 |
|
!$config->has($key) || $config[$key] = '***'.substr($config[$key], -5); |
142
|
8 |
|
} |
143
|
|
|
|
144
|
8 |
|
Log::debug('Current config:', $config->toArray()); |
145
|
8 |
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Add a provider. |
149
|
|
|
* |
150
|
|
|
* @param string $provider |
151
|
|
|
* |
152
|
|
|
* @return Application |
153
|
|
|
*/ |
154
|
1 |
|
public function addProvider($provider) |
155
|
|
|
{ |
156
|
1 |
|
array_push($this->providers, $provider); |
157
|
|
|
|
158
|
1 |
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Set providers. |
163
|
|
|
* |
164
|
|
|
* @param array $providers |
165
|
|
|
*/ |
166
|
1 |
|
public function setProviders(array $providers) |
167
|
|
|
{ |
168
|
1 |
|
$this->providers = []; |
169
|
|
|
|
170
|
1 |
|
foreach ($providers as $provider) { |
171
|
1 |
|
$this->addProvider($provider); |
172
|
1 |
|
} |
173
|
1 |
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Return all providers. |
177
|
|
|
* |
178
|
|
|
* @return array |
179
|
|
|
*/ |
180
|
2 |
|
public function getProviders() |
181
|
|
|
{ |
182
|
2 |
|
return $this->providers; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Magic get access. |
187
|
|
|
* |
188
|
|
|
* @param string $id |
189
|
|
|
* |
190
|
|
|
* @return mixed |
191
|
|
|
*/ |
192
|
4 |
|
public function __get($id) |
193
|
|
|
{ |
194
|
4 |
|
return $this->offsetGet($id); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Magic set access. |
199
|
|
|
* |
200
|
|
|
* @param string $id |
201
|
|
|
* @param mixed $value |
202
|
|
|
*/ |
203
|
1 |
|
public function __set($id, $value) |
204
|
|
|
{ |
205
|
1 |
|
$this->offsetSet($id, $value); |
206
|
1 |
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Register providers. |
210
|
|
|
*/ |
211
|
8 |
|
private function registerProviders() |
212
|
|
|
{ |
213
|
8 |
|
foreach ($this->providers as $provider) { |
214
|
8 |
|
$this->register(new $provider()); |
215
|
8 |
|
} |
216
|
8 |
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Register basic providers. |
220
|
|
|
*/ |
221
|
8 |
|
private function registerBase() |
222
|
|
|
{ |
223
|
|
|
$this['request'] = function () { |
224
|
1 |
|
return Request::createFromGlobals(); |
225
|
|
|
}; |
226
|
|
|
|
227
|
8 |
|
if (!empty($this['config']['cache']) && $this['config']['cache'] instanceof CacheInterface) { |
228
|
|
|
$this['cache'] = $this['config']['cache']; |
229
|
|
|
} else { |
230
|
|
|
$this['cache'] = function () { |
231
|
4 |
|
return new FilesystemCache(sys_get_temp_dir()); |
232
|
|
|
}; |
233
|
|
|
} |
234
|
|
|
|
235
|
1 |
|
$this['access_token'] = function () { |
236
|
1 |
|
return new AccessToken( |
237
|
1 |
|
$this['config']['app_id'], |
238
|
1 |
|
$this['config']['secret'], |
239
|
1 |
|
$this['cache'] |
240
|
1 |
|
); |
241
|
|
|
}; |
242
|
8 |
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Initialize logger. |
246
|
|
|
*/ |
247
|
8 |
|
private function initializeLogger() |
248
|
|
|
{ |
249
|
8 |
|
if (Log::hasLogger()) { |
250
|
8 |
|
return; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
$logger = new Logger('easywechat'); |
254
|
|
|
|
255
|
|
|
if (!$this['config']['debug'] || defined('PHPUNIT_RUNNING')) { |
256
|
|
|
$logger->pushHandler(new NullHandler()); |
257
|
|
|
} elseif ($this['config']['log.handler'] instanceof HandlerInterface) { |
258
|
|
|
$logger->pushHandler($this['config']['log.handler']); |
259
|
|
|
} elseif ($logFile = $this['config']['log.file']) { |
260
|
|
|
$logger->pushHandler(new StreamHandler( |
261
|
|
|
$logFile, |
262
|
|
|
$this['config']->get('log.level', Logger::WARNING), |
263
|
|
|
true, |
264
|
|
|
$this['config']->get('log.permission', null)) |
265
|
|
|
); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
Log::setLogger($logger); |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|