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
|
|
|
* MiniProgramServiceProvider.php. |
14
|
|
|
* |
15
|
|
|
* This file is part of the wechat. |
16
|
|
|
* |
17
|
|
|
* (c) mingyoung <[email protected]> |
18
|
|
|
* |
19
|
|
|
* This source file is subject to the MIT license that is bundled |
20
|
|
|
* with this source code in the file LICENSE. |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
namespace EasyWeChat\Foundation\ServiceProviders; |
24
|
|
|
|
25
|
|
|
use EasyWeChat\Encryption\Encryptor; |
26
|
|
|
use EasyWeChat\MiniProgram\AccessToken; |
27
|
|
|
use EasyWeChat\MiniProgram\MiniProgram; |
28
|
|
|
use EasyWeChat\MiniProgram\Server\Guard; |
29
|
|
|
use Pimple\Container; |
30
|
|
|
use Pimple\ServiceProviderInterface; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Class MiniProgramServiceProvider. |
34
|
|
|
*/ |
35
|
|
|
class MiniProgramServiceProvider implements ServiceProviderInterface |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* Registers services on the given container. |
39
|
|
|
* |
40
|
|
|
* This method should only be used to configure services and parameters. |
41
|
|
|
* It should not get services. |
42
|
|
|
* |
43
|
|
|
* @param Container $pimple A container instance |
44
|
|
|
*/ |
45
|
|
|
public function register(Container $pimple) |
46
|
|
|
{ |
47
|
|
|
$pimple['mini_program_access_token'] = function ($pimple) { |
48
|
|
|
$config = $pimple['config']->get('mini_program'); |
49
|
|
|
|
50
|
|
|
return new AccessToken( |
51
|
|
|
$config['app_id'], |
52
|
|
|
$config['secret'], |
53
|
|
|
$pimple['cache'] |
54
|
|
|
); |
55
|
|
|
}; |
56
|
|
|
|
57
|
|
|
$pimple['mini_program_encryptor'] = function ($pimple) { |
58
|
|
|
$config = $pimple['config']->get('mini_program'); |
59
|
|
|
|
60
|
|
|
return new Encryptor( |
61
|
|
|
$config['app_id'], |
62
|
|
|
$config['token'], |
63
|
|
|
$config['aes_key'] |
64
|
|
|
); |
65
|
|
|
}; |
66
|
|
|
|
67
|
|
|
$pimple['mini_program'] = function ($pimple) { |
68
|
|
|
$config = $pimple['config']->get('mini_program'); |
69
|
|
|
|
70
|
|
|
$server = new Guard($config['token']); |
71
|
|
|
|
72
|
|
|
$server->debug($pimple['config']['debug']); |
73
|
|
|
|
74
|
|
|
$server->setEncryptor($pimple['mini_program_encryptor']); |
75
|
|
|
|
76
|
|
|
return new MiniProgram( |
77
|
|
|
$server, |
78
|
|
|
$pimple['mini_program_access_token'], |
79
|
|
|
$config |
80
|
|
|
); |
81
|
|
|
}; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|