mirror of
https://gitee.com/yansongda/pay.git
synced 2024-11-29 18:58:38 +08:00
update
This commit is contained in:
parent
162d7cab6f
commit
615ab0b847
13
.github/workflows/Linter.yml
vendored
Normal file
13
.github/workflows/Linter.yml
vendored
Normal file
@ -0,0 +1,13 @@
|
||||
name: Linter
|
||||
on: [push, pull_request]
|
||||
|
||||
jobs:
|
||||
php_cs_fixer:
|
||||
name: PHP-CS-Fxier
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@master
|
||||
- name: Install Dependencies
|
||||
run: composer install --no-progress
|
||||
- name: Run PHP-CS-Fxier
|
||||
run: ./vendor/bin/php-cs-fixer fix --dry-run --diff 1>&2
|
21
.github/workflows/Tester.yml
vendored
Normal file
21
.github/workflows/Tester.yml
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
name: Tester
|
||||
on: [push, pull_request]
|
||||
|
||||
jobs:
|
||||
PHPUnit:
|
||||
name: php-${{ matrix.php }}
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
php:
|
||||
- 7.1
|
||||
- 7.2
|
||||
container:
|
||||
image: yansongda/php-fpm:${{ matrix.php }}
|
||||
steps:
|
||||
- uses: actions/checkout@master
|
||||
- name: Install Dependencies
|
||||
run: composer update --prefer-dist --no-interaction --no-suggest
|
||||
- name: Run PHPUnit
|
||||
run: ./vendor/bin/phpunit
|
1
.php_cs
1
.php_cs
@ -1,6 +1,7 @@
|
||||
<?php
|
||||
|
||||
$finder = PhpCsFixer\Finder::create()
|
||||
->in(__DIR__)
|
||||
->exclude('tests')
|
||||
->exclude('vendor');
|
||||
|
||||
|
10
README.md
10
README.md
@ -0,0 +1,10 @@
|
||||
<h1 align="center">Pay</h1>
|
||||
|
||||
<p align="center">
|
||||
[![Linter Status](https://github.com/yansongda/pay/workflows/Linter/badge.svg)](https://github.com/yansongda/pay/actions)
|
||||
[![Tester Status](https://github.com/yansongda/pay/workflows/Tester/badge.svg)](https://github.com/yansongda/pay/actions)
|
||||
[![Latest Stable Version](https://poser.pugx.org/yansongda/pay/v/stable)](https://packagist.org/packages/yansongda/pay)
|
||||
[![Total Downloads](https://poser.pugx.org/yansongda/pay/downloads)](https://packagist.org/packages/yansongda/pay)
|
||||
[![Latest Unstable Version](https://poser.pugx.org/yansongda/pay/v/unstable)](https://packagist.org/packages/yansongda/pay)
|
||||
[![License](https://poser.pugx.org/yansongda/pay/license)](https://packagist.org/packages/yansongda/pay)
|
||||
</p>
|
83
src/Pay.php
83
src/Pay.php
@ -5,12 +5,15 @@ namespace Yansongda\Pay;
|
||||
use Pimple\Container;
|
||||
use Pimple\Exception\FrozenServiceException;
|
||||
use Pimple\Exception\UnknownIdentifierException;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcher;
|
||||
use Yansongda\Pay\Contract\ServiceInterface;
|
||||
use Yansongda\Pay\Exception\GatewayServiceException;
|
||||
use Yansongda\Pay\Exception\UnknownServiceException;
|
||||
use Yansongda\Pay\Service\ConfigService;
|
||||
use Yansongda\Pay\Service\EventService;
|
||||
use Yansongda\Pay\Service\LoggerService;
|
||||
use Yansongda\Supports\Config;
|
||||
use Yansongda\Supports\Logger;
|
||||
use Yansongda\Supports\Str;
|
||||
|
||||
/**
|
||||
@ -20,6 +23,10 @@ use Yansongda\Supports\Str;
|
||||
* @property \Yansongda\Supports\Logger log
|
||||
* @property \Yansongda\Supports\Config config
|
||||
* @property \Symfony\Component\EventDispatcher\EventDispatcher event
|
||||
* @method static Config config($config)
|
||||
* @method static Logger logger($config)
|
||||
* @method static Logger log($config)
|
||||
* @method static EventDispatcher event($config)
|
||||
*/
|
||||
class Pay extends Container
|
||||
{
|
||||
@ -28,26 +35,32 @@ class Pay extends Container
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $config;
|
||||
protected $config = [];
|
||||
|
||||
/**
|
||||
* service.
|
||||
*
|
||||
* @var array
|
||||
* @var string[]
|
||||
*/
|
||||
protected $service;
|
||||
protected $service = [];
|
||||
|
||||
/**
|
||||
* baseConfig.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $baseConfig;
|
||||
private $baseConfig = [
|
||||
'http' => [
|
||||
'timeout' => 5.0,
|
||||
'connect_timeout' => 3.0,
|
||||
],
|
||||
'mode' => 'dev',
|
||||
];
|
||||
|
||||
/**
|
||||
* baseService.
|
||||
*
|
||||
* @var array
|
||||
* @var string[]
|
||||
*/
|
||||
private $baseService = [
|
||||
ConfigService::class,
|
||||
@ -111,9 +124,9 @@ class Pay extends Container
|
||||
* @param $method
|
||||
* @param $params
|
||||
*
|
||||
* @return \Yansongda\Pay\Contract\ServiceInterface
|
||||
* @return mixed
|
||||
*/
|
||||
public static function __callStatic($method, $params): ServiceInterface
|
||||
public static function __callStatic($method, $params)
|
||||
{
|
||||
$app = new static(...$params);
|
||||
|
||||
@ -182,41 +195,21 @@ class Pay extends Container
|
||||
*
|
||||
* @throws \Yansongda\Pay\Exception\GatewayServiceException
|
||||
*
|
||||
* @return ServiceInterface
|
||||
* @return mixed
|
||||
*/
|
||||
protected function create(string $method): ServiceInterface
|
||||
protected function create(string $method)
|
||||
{
|
||||
if (!isset($this[$method])) {
|
||||
$service = __NAMESPACE__.'\\Service\\Gateway\\'.Str::studly($method).'Service';
|
||||
|
||||
if (class_exists($service)) {
|
||||
self::make($service);
|
||||
}
|
||||
|
||||
throw new GatewayServiceException("Gateway [{$method}] Not Exists");
|
||||
if (isset($this[$method])) {
|
||||
return $this[$method];
|
||||
}
|
||||
|
||||
return $this[$method];
|
||||
}
|
||||
$service = __NAMESPACE__.'\\Service\\Gateway\\'.Str::studly($method).'Service';
|
||||
|
||||
/**
|
||||
* make.
|
||||
*
|
||||
* @author yansongda <me@yansongda.cn>
|
||||
*
|
||||
* @param string $service
|
||||
*
|
||||
* @throws \Yansongda\Pay\Exception\GatewayServiceException
|
||||
*/
|
||||
private function make(string $service): void
|
||||
{
|
||||
$gatewayService = new $service($this);
|
||||
|
||||
if ($gatewayService instanceof ServiceInterface) {
|
||||
$this->registerService($gatewayService);
|
||||
if (class_exists($service)) {
|
||||
self::make($service);
|
||||
}
|
||||
|
||||
throw new GatewayServiceException("Gateway [{$service}] Must Be An Instance Of ServiceInterface");
|
||||
throw new GatewayServiceException("Gateway [{$method}] Not Exists");
|
||||
}
|
||||
|
||||
/**
|
||||
@ -238,4 +231,24 @@ class Pay extends Container
|
||||
parent::register(new $service());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* make.
|
||||
*
|
||||
* @author yansongda <me@yansongda.cn>
|
||||
*
|
||||
* @param string $service
|
||||
*
|
||||
* @throws \Yansongda\Pay\Exception\GatewayServiceException
|
||||
*/
|
||||
private function make(string $service): void
|
||||
{
|
||||
$gatewayService = new $service($this);
|
||||
|
||||
if ($gatewayService instanceof ServiceInterface) {
|
||||
$this->registerService($gatewayService);
|
||||
}
|
||||
|
||||
throw new GatewayServiceException("Gateway [{$service}] Must Be An Instance Of ServiceInterface");
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ namespace Yansongda\Pay\Service;
|
||||
use Pimple\Container;
|
||||
use Yansongda\Pay\Contract\ServiceInterface;
|
||||
use Yansongda\Supports\Collection;
|
||||
use Yansongda\Supports\Config;
|
||||
|
||||
class ConfigService implements ServiceInterface
|
||||
{
|
||||
@ -20,7 +21,7 @@ class ConfigService implements ServiceInterface
|
||||
{
|
||||
$pimple['config'] = function ($container) {
|
||||
/* @var \Yansongda\Pay\Pay $container */
|
||||
return new Collection($container->getConfig());
|
||||
return new Config($container->getConfig());
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -33,7 +33,7 @@ class LoggerService implements ServiceInterface
|
||||
|
||||
$logger->setConfig($config);
|
||||
|
||||
return $logger->getLogger();
|
||||
return $logger;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
77
tests/PayTest.php
Normal file
77
tests/PayTest.php
Normal file
@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace Yansongda\Pay\Tests;
|
||||
|
||||
use Pimple\Container;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcher;
|
||||
use Yansongda\Pay\Exception\UnknownServiceException;
|
||||
use Yansongda\Pay\Pay;
|
||||
use Yansongda\Supports\Config;
|
||||
use Yansongda\Supports\Logger;
|
||||
|
||||
class PayTest extends TestCase
|
||||
{
|
||||
public function testBootstrap()
|
||||
{
|
||||
$pay = new Pay([]);
|
||||
|
||||
$this->assertInstanceOf(Pay::class, $pay);
|
||||
$this->assertInstanceOf(Container::class, $pay);
|
||||
}
|
||||
|
||||
public function testMagicGetAndGet()
|
||||
{
|
||||
$config = [];
|
||||
|
||||
$pay = new Pay($config);
|
||||
|
||||
$this->assertInstanceOf(Config::class, $pay->config);
|
||||
$this->assertEquals(new Config($config), $pay->config);
|
||||
$this->assertEquals(new Config($config), $pay->get('config'));
|
||||
$this->assertInstanceOf(Logger::class, $pay->logger);
|
||||
$this->assertInstanceOf(Logger::class, $pay->log);
|
||||
$this->assertInstanceOf(EventDispatcher::class, $pay->event);
|
||||
|
||||
$this->expectException(UnknownServiceException::class);
|
||||
$pay->get('foo');
|
||||
}
|
||||
|
||||
public function testMagicSetAndSet()
|
||||
{
|
||||
$pay = new Pay([]);
|
||||
|
||||
$pay->name = 'yansongda';
|
||||
$pay->set('age', '26');
|
||||
|
||||
$this->assertEquals('yansongda', $pay->name);
|
||||
$this->assertEquals('yansongda', $pay->get('name'));
|
||||
$this->assertEquals('26', $pay->age);
|
||||
$this->assertEquals('26', $pay->get('age'));
|
||||
}
|
||||
|
||||
public function testStaticCall()
|
||||
{
|
||||
$config = [];
|
||||
|
||||
$this->assertInstanceOf(Config::class, Pay::config($config));
|
||||
$this->assertInstanceOf(Logger::class, Pay::logger($config));
|
||||
$this->assertInstanceOf(Logger::class, Pay::log($config));
|
||||
$this->assertInstanceOf(EventDispatcher::class, Pay::event($config));
|
||||
}
|
||||
|
||||
public function testGetConfig()
|
||||
{
|
||||
$config = ['name' => 'yansongda'];
|
||||
|
||||
$pay = new Pay($config);
|
||||
|
||||
$this->assertArrayHasKey('name', $pay->getConfig());
|
||||
$this->assertEquals(array_merge([
|
||||
'http' => [
|
||||
'timeout' => 5.0,
|
||||
'connect_timeout' => 3.0,
|
||||
],
|
||||
'mode' => 'dev'
|
||||
], $config), $pay->getConfig());
|
||||
}
|
||||
}
|
@ -4,5 +4,11 @@ namespace Yansongda\Pay\Tests;
|
||||
|
||||
class TestCase extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
public function tearDown()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user