erge branch 'v3' of github.com:yansongda/pay into v3

This commit is contained in:
yansongda 2020-01-11 23:00:56 +08:00
commit 595867f5a1
4 changed files with 41 additions and 45 deletions

View File

@ -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;
}
/**

View File

@ -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 {
};
};
}

View File

@ -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;
};

View File

@ -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']);
}
}