Pay.php
6.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php
declare(strict_types=1);
namespace Yansongda\Pay;
use Closure;
use Illuminate\Container\Container as LaravelContainer;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Throwable;
use Yansongda\Pay\Contract\DirectionInterface;
use Yansongda\Pay\Contract\PackerInterface;
use Yansongda\Pay\Contract\ServiceProviderInterface;
use Yansongda\Pay\Direction\CollectionDirection;
use Yansongda\Pay\Exception\ContainerException;
use Yansongda\Pay\Exception\ContainerNotFoundException;
use Yansongda\Pay\Exception\ServiceNotFoundException;
use Yansongda\Pay\Packer\JsonPacker;
use Yansongda\Pay\Provider\Alipay;
use Yansongda\Pay\Provider\Unipay;
use Yansongda\Pay\Provider\Wechat;
use Yansongda\Pay\Service\AlipayServiceProvider;
use Yansongda\Pay\Service\ConfigServiceProvider;
use Yansongda\Pay\Service\ContainerServiceProvider;
use Yansongda\Pay\Service\EventServiceProvider;
use Yansongda\Pay\Service\HttpServiceProvider;
use Yansongda\Pay\Service\LoggerServiceProvider;
use Yansongda\Pay\Service\UnipayServiceProvider;
use Yansongda\Pay\Service\WechatServiceProvider;
/**
* @method static Alipay alipay(array $config = [], $container = null)
* @method static Wechat wechat(array $config = [], $container = null)
* @method static Unipay unipay(array $config = [], $container = null)
*/
class Pay
{
/**
* 正常模式.
*/
public const MODE_NORMAL = 0;
/**
* 沙箱模式.
*/
public const MODE_SANDBOX = 1;
/**
* 服务商模式.
*/
public const MODE_SERVICE = 2;
/**
* @var string[]
*/
protected array $service = [
AlipayServiceProvider::class,
WechatServiceProvider::class,
UnipayServiceProvider::class,
];
/**
* @var string[]
*/
private array $coreService = [
ContainerServiceProvider::class,
ConfigServiceProvider::class,
LoggerServiceProvider::class,
EventServiceProvider::class,
HttpServiceProvider::class,
];
/**
* @var null|Closure|ContainerInterface
*/
private static $container;
/**
* @param null|Closure|ContainerInterface $container
*
* @throws ContainerException
*/
private function __construct(array $config, $container = null)
{
$this->registerServices($config, $container);
Pay::set(DirectionInterface::class, CollectionDirection::class);
Pay::set(PackerInterface::class, JsonPacker::class);
}
/**
* @return mixed
*
* @throws ContainerException
* @throws ServiceNotFoundException
*/
public static function __callStatic(string $service, array $config)
{
if (!empty($config)) {
self::config(...$config);
}
return self::get($service);
}
/**
* @param null|Closure|ContainerInterface $container
*
* @throws ContainerException
*/
public static function config(array $config = [], $container = null): bool
{
if (self::hasContainer() && !($config['_force'] ?? false)) {
return false;
}
new self($config, $container);
return true;
}
/**
* @codeCoverageIgnore
*
* @param mixed $value
*
* @throws ContainerException
*/
public static function set(string $name, $value): void
{
try {
$container = Pay::getContainer();
if ($container instanceof LaravelContainer) {
$container->singleton($name, $value instanceof Closure ? $value : static fn () => $value);
return;
}
if (method_exists($container, 'set')) {
$container->set(...func_get_args());
return;
}
} catch (ContainerNotFoundException $e) {
throw $e;
} catch (Throwable $e) {
throw new ContainerException($e->getMessage());
}
throw new ContainerException('Current container does NOT support `set` method');
}
/**
* @codeCoverageIgnore
*
* @return mixed
*
* @throws ContainerException
*/
public static function make(string $service, array $parameters = [])
{
try {
$container = Pay::getContainer();
if (method_exists($container, 'make')) {
return $container->make(...func_get_args());
}
} catch (ContainerNotFoundException $e) {
throw $e;
} catch (Throwable $e) {
throw new ContainerException($e->getMessage());
}
$parameters = array_values($parameters);
return new $service(...$parameters);
}
/**
* @return mixed
*
* @throws ServiceNotFoundException
* @throws ContainerException
*/
public static function get(string $service)
{
try {
return Pay::getContainer()->get($service);
} catch (NotFoundExceptionInterface $e) {
throw new ServiceNotFoundException($e->getMessage());
} catch (ContainerNotFoundException $e) {
throw $e;
} catch (Throwable $e) {
throw new ContainerException($e->getMessage());
}
}
/**
* @throws ContainerNotFoundException
*/
public static function has(string $service): bool
{
return Pay::getContainer()->has($service);
}
/**
* @param null|Closure|ContainerInterface $container
*/
public static function setContainer($container): void
{
self::$container = $container;
}
/**
* @throws ContainerNotFoundException
*/
public static function getContainer(): ContainerInterface
{
if (self::$container instanceof ContainerInterface) {
return self::$container;
}
if (self::$container instanceof Closure) {
return (self::$container)();
}
throw new ContainerNotFoundException('`getContainer()` failed! Maybe you should `setContainer()` first', Exception\Exception::CONTAINER_NOT_FOUND);
}
public static function hasContainer(): bool
{
return self::$container instanceof ContainerInterface || self::$container instanceof Closure;
}
public static function clear(): void
{
self::$container = null;
}
/**
* @param mixed $data
*
* @throws ContainerException
*/
public static function registerService(string $service, $data): void
{
$var = new $service();
if ($var instanceof ServiceProviderInterface) {
$var->register($data);
}
}
/**
* @param null|Closure|ContainerInterface $container
*
* @throws ContainerException
*/
private function registerServices(array $config, $container = null): void
{
foreach (array_merge($this->coreService, $this->service) as $service) {
self::registerService($service, ContainerServiceProvider::class == $service ? $container : $config);
}
}
}