diff --git a/src/Pay.php b/src/Pay.php index 382cda8..3195afa 100644 --- a/src/Pay.php +++ b/src/Pay.php @@ -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 * + * @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 * + * @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 * + * @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 * + * @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 * + * @throws \Yansongda\Pay\Exception\ContainerDependencyException * @throws \Yansongda\Pay\Exception\ContainerException + * @throws \Yansongda\Pay\Exception\ContainerNotFoundException + * @throws \Yansongda\Pay\Exception\ServiceNotFoundException */ private function initContainer(): void { diff --git a/src/Service/AlipayServiceProvider.php b/src/Service/AlipayServiceProvider.php index 2c6de29..24d131c 100644 --- a/src/Service/AlipayServiceProvider.php +++ b/src/Service/AlipayServiceProvider.php @@ -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 { diff --git a/src/Service/ConfigServiceProvider.php b/src/Service/ConfigServiceProvider.php index 9060c59..8f3042c 100644 --- a/src/Service/ConfigServiceProvider.php +++ b/src/Service/ConfigServiceProvider.php @@ -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 { diff --git a/src/Service/EventServiceProvider.php b/src/Service/EventServiceProvider.php index 808e2f1..479ba64 100644 --- a/src/Service/EventServiceProvider.php +++ b/src/Service/EventServiceProvider.php @@ -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 { diff --git a/src/Service/LoggerServiceProvider.php b/src/Service/LoggerServiceProvider.php index d9a2634..3aa96e3 100644 --- a/src/Service/LoggerServiceProvider.php +++ b/src/Service/LoggerServiceProvider.php @@ -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 { diff --git a/src/Service/WechatServiceProvider.php b/src/Service/WechatServiceProvider.php index 4d8d9e7..2da6be9 100644 --- a/src/Service/WechatServiceProvider.php +++ b/src/Service/WechatServiceProvider.php @@ -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 { diff --git a/tests/PayTest.php b/tests/PayTest.php index 21af31f..34a9884 100644 --- a/tests/PayTest.php +++ b/tests/PayTest.php @@ -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');