diff --git a/src/Pay.php b/src/Pay.php index 1703a4d..ec3bb24 100644 --- a/src/Pay.php +++ b/src/Pay.php @@ -40,7 +40,9 @@ class Pay extends Container * * @var array */ - protected $c = []; + protected $userConfig = []; + + protected $middleware = []; /** * service. @@ -52,27 +54,6 @@ class Pay extends Container WechatServiceProvider::class, ]; - /** - * baseConfig. - * - * @var array - */ - private $baseConfig = [ - 'log' => [ - 'enable' => true, - 'file' => null, - 'identify' => 'yansongda.pay', - 'level' => 'debug', - 'type' => 'daily', - 'max_files' => 30, - ], - 'http' => [ - 'timeout' => 5.0, - 'connect_timeout' => 3.0, - ], - 'mode' => 'normal', - ]; - /** * baseService. * @@ -94,7 +75,7 @@ class Pay extends Container */ public function __construct(array $c, array $value = []) { - $this->c = $c; + $this->userConfig = $c; parent::__construct($value); @@ -209,9 +190,9 @@ class Pay extends Container * * @return array */ - public function getConfig(): array + public function getUserConfig(): array { - return array_replace_recursive($this->baseConfig, $this->c); + return $this->userConfig; } /** diff --git a/src/Service/ConfigServiceProvider.php b/src/Service/ConfigServiceProvider.php index a9373b7..baea04c 100644 --- a/src/Service/ConfigServiceProvider.php +++ b/src/Service/ConfigServiceProvider.php @@ -9,6 +9,26 @@ use Yansongda\Supports\Config; class ConfigServiceProvider implements ServiceProviderInterface { + /** + * baseConfig. + * + * @var array + */ + private $baseConfig = [ + 'log' => [ + 'enable' => true, + 'file' => null, + 'level' => 'debug', + 'type' => 'daily', + 'max_files' => 30, + ], + 'http' => [ + 'timeout' => 5.0, + 'connect_timeout' => 3.0, + ], + 'mode' => 'normal', + ]; + /** * Registers services on the given container. * @@ -21,7 +41,10 @@ class ConfigServiceProvider implements ServiceProviderInterface { $pimple['config'] = function ($container) { /* @var \Yansongda\Pay\Pay $container */ - return new class($container->getConfig()) extends Config implements ServiceInterface { + $config = array_replace_recursive($this->baseConfig, $container->getUserConfig()); + $config['log']['identify'] = 'yansongda.pay'; + + return new class($config) extends Config implements ServiceInterface { }; }; } diff --git a/src/Service/LoggerServiceProvider.php b/src/Service/LoggerServiceProvider.php index 9c51a09..c02ae11 100644 --- a/src/Service/LoggerServiceProvider.php +++ b/src/Service/LoggerServiceProvider.php @@ -50,16 +50,7 @@ class LoggerServiceProvider implements ServiceProviderInterface } }; - $config = ['identify' => 'yansongda.pay']; - - if (isset($container['config']['log'])) { - $config = array_merge( - $container['config']['log'], - $config - ); - } - - $logger->setConfig($config); + $logger->setConfig($container['config']['log']); return $logger; }; diff --git a/tests/PayTest.php b/tests/PayTest.php index b609e4d..261d835 100644 --- a/tests/PayTest.php +++ b/tests/PayTest.php @@ -20,7 +20,7 @@ class PayTest extends TestCase 'identify' => 'yansongda.pay', 'level' => 'debug', 'type' => 'daily', - 'max_files' => 30 + 'max_files' => 30, ], 'http' => [ 'timeout' => 5.0, @@ -113,16 +113,17 @@ class PayTest extends TestCase { $config = ['name' => 'yansongda']; $pay = new Pay($config); - $this->assertEquals(array_replace_recursive($this->baseConfig, $config), $pay->getConfig()); - $this->assertArrayHasKey('name', $pay->getConfig()); + $this->assertEquals($config, $pay->getUserConfig()); + $this->assertEquals(array_replace_recursive($this->baseConfig, $config), $pay->config->all()); + $this->assertArrayHasKey('name', $pay->config->all()); $config1 = ['http' => ['timeout' => '3']]; $pay1 = new Pay($config1); - $this->assertEquals(array_replace_recursive($this->baseConfig, $config1), $pay1->getConfig()); - $this->assertArrayHasKey('http', $pay1->getConfig()); - $this->assertArrayNotHasKey('name', $pay1->getConfig()); - $this->assertEquals('3', $pay1->getConfig()['http']['timeout']); - $this->assertArrayHasKey('connect_timeout', $pay1->getConfig()['http']); - $this->assertEquals('3', $pay1->getConfig()['http']['connect_timeout']); + $this->assertEquals(array_replace_recursive($this->baseConfig, $config1), $pay1->config->all()); + $this->assertArrayHasKey('http', $pay1->config->all()); + $this->assertArrayNotHasKey('name', $pay1->config->all()); + $this->assertEquals('3', $pay1->config->all()['http']['timeout']); + $this->assertArrayHasKey('connect_timeout', $pay1->config->all()['http']); + $this->assertEquals('3', $pay1->config->all()['http']['connect_timeout']); } } \ No newline at end of file