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
|
|
|
* @link https://github.com/overtrue/wechat |
24
|
|
|
* @link http://overtrue.me |
25
|
|
|
*/ |
26
|
|
|
namespace EasyWeChat\Foundation; |
27
|
|
|
|
28
|
|
|
use Doctrine\Common\Cache\Cache as CacheInterface; |
29
|
|
|
use Doctrine\Common\Cache\FilesystemCache; |
30
|
|
|
use EasyWeChat\Core\AccessToken; |
31
|
|
|
use EasyWeChat\Core\Http; |
32
|
|
|
use EasyWeChat\Support\Log; |
33
|
|
|
use Monolog\Handler\NullHandler; |
34
|
|
|
use Monolog\Handler\StreamHandler; |
35
|
|
|
use Monolog\Logger; |
36
|
|
|
use Pimple\Container; |
37
|
|
|
use Symfony\Component\HttpFoundation\Request; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Class Application. |
41
|
|
|
* |
42
|
|
|
* @property \EasyWeChat\Server\Guard $server |
43
|
|
|
* @property \EasyWeChat\User\User $user |
44
|
|
|
* @property \EasyWeChat\User\Tag $user_tag |
45
|
|
|
* @property \EasyWeChat\User\Group $user_group |
46
|
|
|
* @property \EasyWeChat\Js\Js $js |
47
|
|
|
* @property \Overtrue\Socialite\SocialiteManager $oauth |
48
|
|
|
* @property \EasyWeChat\Menu\Menu $menu |
49
|
|
|
* @property \EasyWeChat\Notice\Notice $notice |
50
|
|
|
* @property \EasyWeChat\Material\Material $material |
51
|
|
|
* @property \EasyWeChat\Material\Temporary $material_temporary |
52
|
|
|
* @property \EasyWeChat\Staff\Staff $staff |
53
|
|
|
* @property \EasyWeChat\Url\Url $url |
54
|
|
|
* @property \EasyWeChat\QRCode\QRCode $qrcode |
55
|
|
|
* @property \EasyWeChat\Semantic\Semantic $semantic |
56
|
|
|
* @property \EasyWeChat\Stats\Stats $stats |
57
|
|
|
* @property \EasyWeChat\Payment\Merchant $merchant |
58
|
|
|
* @property \EasyWeChat\Payment\Payment $payment |
59
|
|
|
* @property \EasyWeChat\Payment\LuckyMoney\LuckyMoney $lucky_money |
60
|
|
|
* @property \EasyWeChat\Payment\MerchantPay\MerchantPay $merchant_pay |
61
|
|
|
* @property \EasyWeChat\Reply\Reply $reply |
62
|
|
|
* @property \EasyWeChat\Broadcast\Broadcast $broadcast |
63
|
|
|
*/ |
64
|
|
|
class Application extends Container |
65
|
|
|
{ |
66
|
|
|
/** |
67
|
|
|
* Service Providers. |
68
|
|
|
* |
69
|
|
|
* @var array |
70
|
|
|
*/ |
71
|
|
|
protected $providers = [ |
72
|
|
|
ServiceProviders\ServerServiceProvider::class, |
73
|
|
|
ServiceProviders\UserServiceProvider::class, |
74
|
|
|
ServiceProviders\JsServiceProvider::class, |
75
|
|
|
ServiceProviders\OAuthServiceProvider::class, |
76
|
|
|
ServiceProviders\MenuServiceProvider::class, |
77
|
|
|
ServiceProviders\NoticeServiceProvider::class, |
78
|
|
|
ServiceProviders\MaterialServiceProvider::class, |
79
|
|
|
ServiceProviders\StaffServiceProvider::class, |
80
|
|
|
ServiceProviders\UrlServiceProvider::class, |
81
|
|
|
ServiceProviders\QRCodeServiceProvider::class, |
82
|
|
|
ServiceProviders\SemanticServiceProvider::class, |
83
|
|
|
ServiceProviders\StatsServiceProvider::class, |
84
|
|
|
ServiceProviders\PaymentServiceProvider::class, |
85
|
|
|
ServiceProviders\POIServiceProvider::class, |
86
|
|
|
ServiceProviders\ReplyServiceProvider::class, |
87
|
|
|
ServiceProviders\BroadcastServiceProvider::class, |
88
|
|
|
]; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Application constructor. |
92
|
|
|
* |
93
|
|
|
* @param array $config |
94
|
|
|
*/ |
95
|
4 |
|
public function __construct($config) |
96
|
|
|
{ |
97
|
4 |
|
parent::__construct(); |
98
|
|
|
|
99
|
|
|
$this['config'] = function () use ($config) { |
100
|
4 |
|
return new Config($config); |
101
|
|
|
}; |
102
|
|
|
|
103
|
4 |
|
if ($this['config']['debug']) { |
104
|
|
|
error_reporting(E_ALL); |
105
|
|
|
} |
106
|
|
|
|
107
|
4 |
|
$this->registerProviders(); |
108
|
4 |
|
$this->registerBase(); |
109
|
4 |
|
$this->initializeLogger(); |
110
|
|
|
|
111
|
4 |
|
Http::setDefaultOptions($this['config']->get('guzzle', ['timeout' => 5.0])); |
112
|
|
|
|
113
|
4 |
|
foreach (['app_id', 'secret'] as $key) { |
114
|
4 |
|
!isset($config[$key]) || $config[$key] = '***'.substr($config[$key], -5); |
115
|
4 |
|
} |
116
|
|
|
|
117
|
4 |
|
Log::debug('Current config:', $config); |
118
|
4 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Add a provider. |
122
|
|
|
* |
123
|
|
|
* @param string $provider |
124
|
|
|
* |
125
|
|
|
* @return Application |
126
|
|
|
*/ |
127
|
1 |
|
public function addProvider($provider) |
128
|
|
|
{ |
129
|
1 |
|
array_push($this->providers, $provider); |
130
|
|
|
|
131
|
1 |
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Set providers. |
136
|
|
|
* |
137
|
|
|
* @param array $providers |
138
|
|
|
*/ |
139
|
1 |
|
public function setProviders(array $providers) |
140
|
|
|
{ |
141
|
1 |
|
$this->providers = []; |
142
|
|
|
|
143
|
1 |
|
foreach ($providers as $provider) { |
144
|
1 |
|
$this->addProvider($provider); |
145
|
1 |
|
} |
146
|
1 |
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Return all providers. |
150
|
|
|
* |
151
|
|
|
* @return array |
152
|
|
|
*/ |
153
|
2 |
|
public function getProviders() |
154
|
|
|
{ |
155
|
2 |
|
return $this->providers; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Magic get access. |
160
|
|
|
* |
161
|
|
|
* @param string $id |
162
|
|
|
* |
163
|
|
|
* @return mixed |
164
|
|
|
*/ |
165
|
1 |
|
public function __get($id) |
166
|
|
|
{ |
167
|
1 |
|
return $this->offsetGet($id); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Magic set access. |
172
|
|
|
* |
173
|
|
|
* @param string $id |
174
|
|
|
* @param mixed $value |
175
|
|
|
*/ |
176
|
1 |
|
public function __set($id, $value) |
177
|
|
|
{ |
178
|
1 |
|
$this->offsetSet($id, $value); |
179
|
1 |
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Register providers. |
183
|
|
|
*/ |
184
|
4 |
|
private function registerProviders() |
185
|
|
|
{ |
186
|
4 |
|
foreach ($this->providers as $provider) { |
187
|
4 |
|
$this->register(new $provider()); |
188
|
4 |
|
} |
189
|
4 |
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Register basic providers. |
193
|
|
|
*/ |
194
|
4 |
|
private function registerBase() |
195
|
|
|
{ |
196
|
|
|
$this['request'] = function () { |
197
|
|
|
return Request::createFromGlobals(); |
198
|
|
|
}; |
199
|
|
|
|
200
|
4 |
|
if (!empty($this['config']['cache']) && $this['config']['cache'] instanceof CacheInterface) { |
201
|
|
|
$this['cache'] = $this['config']['cache']; |
202
|
|
|
} else { |
203
|
|
|
$this['cache'] = function () { |
204
|
|
|
return new FilesystemCache(sys_get_temp_dir()); |
205
|
|
|
}; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
$this['access_token'] = function () { |
209
|
|
|
return new AccessToken( |
210
|
|
|
$this['config']['app_id'], |
211
|
|
|
$this['config']['secret'], |
212
|
|
|
$this['cache'] |
213
|
|
|
); |
214
|
|
|
}; |
215
|
4 |
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Initialize logger. |
219
|
|
|
*/ |
220
|
4 |
|
private function initializeLogger() |
221
|
|
|
{ |
222
|
4 |
|
if (Log::hasLogger()) { |
223
|
4 |
|
return; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
$logger = new Logger('easywechat'); |
227
|
|
|
|
228
|
|
|
if (!$this['config']['debug'] || defined('PHPUNIT_RUNNING')) { |
229
|
|
|
$logger->pushHandler(new NullHandler()); |
230
|
|
|
} elseif ($logFile = $this['config']['log.file']) { |
231
|
|
|
$logger->pushHandler(new StreamHandler($logFile, $this['config']->get('log.level', Logger::WARNING))); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
Log::setLogger($logger); |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|