This commit is contained in:
yansongda 2020-03-30 23:11:22 +08:00
parent ab2332aede
commit 9874801441
7 changed files with 61 additions and 0 deletions

View File

@ -7,6 +7,7 @@ use DI\ContainerBuilder;
use DI\DependencyException;
use DI\NotFoundException;
use Exception;
use Yansongda\Pay\Contract\ConfigInterface;
use Yansongda\Pay\Contract\ContainerInterface;
use Yansongda\Pay\Contract\ServiceProviderInterface;
use Yansongda\Pay\Exception\ContainerDependencyException;
@ -18,6 +19,7 @@ use Yansongda\Pay\Service\ConfigServiceProvider;
use Yansongda\Pay\Service\EventServiceProvider;
use Yansongda\Pay\Service\LoggerServiceProvider;
use Yansongda\Pay\Service\WechatServiceProvider;
use Yansongda\Supports\Config;
class Pay
{
@ -67,7 +69,10 @@ class Pay
*
* @author yansongda <me@yansongda.cn>
*
* @throws \Yansongda\Pay\Exception\ContainerDependencyException
* @throws \Yansongda\Pay\Exception\ContainerException
* @throws \Yansongda\Pay\Exception\ContainerNotFoundException
* @throws \Yansongda\Pay\Exception\ServiceNotFoundException
*/
private function __construct(array $config)
{
@ -120,7 +125,12 @@ class Pay
*
* @author yansongda <me@yansongda.cn>
*
* @param $value
*
* @throws \Yansongda\Pay\Exception\ContainerDependencyException
* @throws \Yansongda\Pay\Exception\ContainerException
* @throws \Yansongda\Pay\Exception\ContainerNotFoundException
* @throws \Yansongda\Pay\Exception\ServiceNotFoundException
*/
public static function set(string $key, $value): void
{
@ -132,7 +142,10 @@ class Pay
*
* @author yansongda <me@yansongda.cn>
*
* @throws \Yansongda\Pay\Exception\ContainerDependencyException
* @throws \Yansongda\Pay\Exception\ContainerException
* @throws \Yansongda\Pay\Exception\ContainerNotFoundException
* @throws \Yansongda\Pay\Exception\ServiceNotFoundException
*/
public static function has(string $key): bool
{
@ -144,11 +157,20 @@ class Pay
*
* @author yansongda <me@yansongda.cn>
*
* @throws \Yansongda\Pay\Exception\ContainerDependencyException
* @throws \Yansongda\Pay\Exception\ContainerException
* @throws \Yansongda\Pay\Exception\ContainerNotFoundException
* @throws \Yansongda\Pay\Exception\ServiceNotFoundException
*/
public static function getContainer(?array $initConfig = null): Container
{
if (self::$container instanceof Container) {
if (isset($initConfig['cli']) && true === $initConfig['cli']) {
/* @var Config $config */
$config = self::get(ConfigInterface::class);
self::set(ConfigInterface::class, new Config(array_replace_recursive($config->all(), $initConfig)));
}
return self::$container;
}
@ -176,7 +198,10 @@ class Pay
*
* @author yansongda <me@yansongda.cn>
*
* @throws \Yansongda\Pay\Exception\ContainerDependencyException
* @throws \Yansongda\Pay\Exception\ContainerException
* @throws \Yansongda\Pay\Exception\ContainerNotFoundException
* @throws \Yansongda\Pay\Exception\ServiceNotFoundException
*/
private function initContainer(): void
{

View File

@ -18,7 +18,10 @@ class AlipayServiceProvider implements ServiceProviderInterface
/**
* {@inheritdoc}
*
* @throws \Yansongda\Pay\Exception\ContainerDependencyException
* @throws \Yansongda\Pay\Exception\ContainerException
* @throws \Yansongda\Pay\Exception\ContainerNotFoundException
* @throws \Yansongda\Pay\Exception\ServiceNotFoundException
*/
public function register(Pay $pay): void
{

View File

@ -25,6 +25,9 @@ class ConfigServiceProvider implements ServiceProviderInterface
'timeout' => 5.0,
'connect_timeout' => 3.0,
],
// 是否处于命令行模式
'cli' => false,
// 当前支付体系
'mode' => Pay::MODE_NORMAL,
];
@ -44,7 +47,10 @@ class ConfigServiceProvider implements ServiceProviderInterface
/**
* {@inheritdoc}
*
* @throws \Yansongda\Pay\Exception\ContainerDependencyException
* @throws \Yansongda\Pay\Exception\ContainerException
* @throws \Yansongda\Pay\Exception\ContainerNotFoundException
* @throws \Yansongda\Pay\Exception\ServiceNotFoundException
*/
public function register(Pay $pay): void
{

View File

@ -20,7 +20,10 @@ class EventServiceProvider implements ServiceProviderInterface
/**
* {@inheritdoc}
*
* @throws \Yansongda\Pay\Exception\ContainerDependencyException
* @throws \Yansongda\Pay\Exception\ContainerException
* @throws \Yansongda\Pay\Exception\ContainerNotFoundException
* @throws \Yansongda\Pay\Exception\ServiceNotFoundException
*/
public function register(Pay $pay): void
{

View File

@ -21,7 +21,10 @@ class LoggerServiceProvider implements ServiceProviderInterface
/**
* {@inheritdoc}
*
* @throws \Yansongda\Pay\Exception\ContainerDependencyException
* @throws \Yansongda\Pay\Exception\ContainerException
* @throws \Yansongda\Pay\Exception\ContainerNotFoundException
* @throws \Yansongda\Pay\Exception\ServiceNotFoundException
*/
public function register(Pay $pay): void
{

View File

@ -19,7 +19,10 @@ class WechatServiceProvider implements ServiceProviderInterface
/**
* {@inheritdoc}
*
* @throws \Yansongda\Pay\Exception\ContainerDependencyException
* @throws \Yansongda\Pay\Exception\ContainerException
* @throws \Yansongda\Pay\Exception\ContainerNotFoundException
* @throws \Yansongda\Pay\Exception\ServiceNotFoundException
*/
public function register(Pay $pay): void
{

View File

@ -98,6 +98,24 @@ class PayTest extends TestCase
$this->assertEquals($container1, $container2);
}
public function testCliMode()
{
$config = [
'name' => 'yansongda',
'cli' => true,
];
Pay::getContainer($config);
$this->assertEquals($config['name'], Pay::get(ConfigInterface::class)->get('name'));
// 修改 config 的情况
$config2 = [
'name' => 'yansongda2',
'cli' => true
];
Pay::getContainer($config2);
$this->assertEquals($config2['name'], Pay::get(ConfigInterface::class)->get('name'));
}
public function testGetContainer()
{
$this->expectExceptionMessage('You Must Init The Container First');