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
|
|
|
* AbstractComponent.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 mingyoung <[email protected]> |
21
|
|
|
* @author lixiao <[email protected]> |
22
|
|
|
* @copyright 2016 |
23
|
|
|
* |
24
|
|
|
* @see https://github.com/overtrue |
25
|
|
|
* @see http://overtrue.me |
26
|
|
|
*/ |
27
|
|
|
|
28
|
|
|
namespace EasyWeChat\OpenPlatform\Components; |
29
|
|
|
|
30
|
|
|
use EasyWeChat\Core\AbstractAPI; |
31
|
|
|
use EasyWeChat\Core\AccessToken; |
32
|
|
|
use EasyWeChat\Core\Exception; |
33
|
|
|
use Symfony\Component\HttpFoundation\Request; |
34
|
|
|
|
35
|
|
|
abstract class AbstractComponent extends AbstractAPI |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* Config. |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $config; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* AppId, component app id. |
46
|
|
|
* |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
private $appId; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Request. |
53
|
|
|
* |
54
|
|
|
* @var Request |
55
|
|
|
*/ |
56
|
|
|
protected $request; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* AbstractComponent constructor. |
60
|
|
|
* |
61
|
|
|
* @param AccessToken $accessToken |
62
|
|
|
* @param array $config |
63
|
|
|
* @param $request |
64
|
|
|
*/ |
65
|
|
|
public function __construct($accessToken, array $config, $request = null) |
66
|
|
|
{ |
67
|
|
|
parent::__construct($accessToken); |
68
|
|
|
$this->config = $config; |
69
|
|
|
$this->request = $request ?: Request::createFromGlobals(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get AppId. |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
* @throws Exception When app id is not present. |
77
|
|
|
*/ |
78
|
|
|
public function getAppId() |
79
|
|
|
{ |
80
|
|
|
if ($this->appId) { |
81
|
|
|
return $this->appId; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if (isset($this->config['open_platform'])) { |
85
|
|
|
$this->appId = $this->config['open_platform']['app_id']; |
86
|
|
|
} else { |
87
|
|
|
$this->appId = $this->config['app_id']; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (empty($this->appId)) { |
91
|
|
|
throw new Exception('App Id is not present.'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $this->appId; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Set AppId. |
99
|
|
|
* |
100
|
|
|
* @param string $appId |
101
|
|
|
* |
102
|
|
|
* @return $this |
103
|
|
|
*/ |
104
|
|
|
public function setAppId($appId) |
105
|
|
|
{ |
106
|
|
|
$this->appId = $appId; |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
} |
112
|
|
|
|