1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kaylyu\Alipay\Kernel; |
4
|
|
|
|
5
|
|
|
use Kaylyu\Alipay\Kernel\Providers\ConfigServiceProvider; |
6
|
|
|
use Kaylyu\Alipay\Kernel\Providers\HttpClientServiceProvider; |
7
|
|
|
use Kaylyu\Alipay\Kernel\Providers\LogServiceProvider; |
8
|
|
|
use Kaylyu\Alipay\Kernel\Providers\RequestServiceProvider; |
9
|
|
|
use Pimple\Container; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class ServiceContainer. |
13
|
|
|
* |
14
|
|
|
* @property \Kaylyu\Alipay\Kernel\Config $config |
15
|
|
|
* @property \Symfony\Component\HttpFoundation\Request $request |
16
|
|
|
* @property \GuzzleHttp\Client $http_client |
17
|
|
|
* @property \Monolog\Logger $logger |
18
|
|
|
*/ |
19
|
|
|
class ServiceContainer extends Container |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $id; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $providers = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $defaultConfig = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
protected $userConfig = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Constructor. |
43
|
|
|
* |
44
|
|
|
* @param array $config |
45
|
|
|
* @param array $prepends |
46
|
|
|
* @param string|null $id |
47
|
|
|
*/ |
48
|
|
|
public function __construct(array $config = [], array $prepends = [], string $id = null) |
49
|
|
|
{ |
50
|
|
|
$this->registerProviders($this->getProviders()); |
51
|
|
|
|
52
|
|
|
parent::__construct($prepends); |
53
|
|
|
|
54
|
|
|
$this->userConfig = $config; |
55
|
|
|
|
56
|
|
|
$this->id = $id; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
public function getId() |
63
|
|
|
{ |
64
|
|
|
return $this->id ?? $this->id = md5(json_encode($this->userConfig)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* 当面付 |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
|
|
public function getF2fpay() : array |
72
|
|
|
{ |
73
|
|
|
return $this->config->get('f2fpay'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
public function getConfig() |
80
|
|
|
{ |
81
|
|
|
$base = [ |
82
|
|
|
// http://docs.guzzlephp.org/en/stable/request-options.html |
83
|
|
|
'http' => [ |
84
|
|
|
'timeout' => 5.0, |
85
|
|
|
'base_uri' => '', |
86
|
|
|
], |
87
|
|
|
]; |
88
|
|
|
|
89
|
|
|
return array_replace_recursive($base, $this->defaultConfig, $this->userConfig); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Return all providers. |
94
|
|
|
* |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
|
|
public function getProviders() |
98
|
|
|
{ |
99
|
|
|
return array_merge([ |
100
|
|
|
ConfigServiceProvider::class, |
101
|
|
|
LogServiceProvider::class, |
102
|
|
|
RequestServiceProvider::class, |
103
|
|
|
HttpClientServiceProvider::class, |
104
|
|
|
], $this->providers); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $id |
109
|
|
|
* @param mixed $value |
110
|
|
|
*/ |
111
|
|
|
public function rebind($id, $value) |
112
|
|
|
{ |
113
|
|
|
$this->offsetUnset($id); |
114
|
|
|
$this->offsetSet($id, $value); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Magic get access. |
119
|
|
|
* |
120
|
|
|
* @param string $id |
121
|
|
|
* |
122
|
|
|
* @return mixed |
123
|
|
|
*/ |
124
|
|
|
public function __get($id) |
125
|
|
|
{ |
126
|
|
|
return $this->offsetGet($id); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Magic set access. |
131
|
|
|
* |
132
|
|
|
* @param string $id |
133
|
|
|
* @param mixed $value |
134
|
|
|
*/ |
135
|
|
|
public function __set($id, $value) |
136
|
|
|
{ |
137
|
|
|
$this->offsetSet($id, $value); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param array $providers |
142
|
|
|
*/ |
143
|
|
|
public function registerProviders(array $providers) |
144
|
|
|
{ |
145
|
|
|
foreach ($providers as $provider) { |
146
|
|
|
parent::register(new $provider()); |
|
|
|
|
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.