mirror of
https://gitee.com/yansongda/pay.git
synced 2024-12-02 04:08:21 +08:00
update
This commit is contained in:
parent
f3273688e7
commit
9bee6312c2
@ -19,7 +19,7 @@
|
||||
"ext-simplexml":"*",
|
||||
"ext-libxml": "*",
|
||||
"ext-json": "*",
|
||||
"yansongda/supports": "^1.8",
|
||||
"yansongda/supports": "^2.0",
|
||||
"symfony/http-foundation": "^4.0",
|
||||
"symfony/event-dispatcher": "^4.0",
|
||||
"pimple/pimple": "^3.2"
|
||||
|
@ -6,4 +6,4 @@ use Pimple\ServiceProviderInterface;
|
||||
|
||||
interface ServiceInterface extends ServiceProviderInterface
|
||||
{
|
||||
}
|
||||
}
|
||||
|
39
src/Exception/Exception.php
Normal file
39
src/Exception/Exception.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace Yansongda\Pay\Exception;
|
||||
|
||||
use Throwable;
|
||||
|
||||
class Exception extends \Exception
|
||||
{
|
||||
const UNKNOWN_ERROR = 9999;
|
||||
|
||||
// Service
|
||||
const UNKNOWN_SERVICE = 1000;
|
||||
|
||||
const FROZEN_SERVICE = 1001;
|
||||
|
||||
const GATEWAY_SERVICE = 1002;
|
||||
|
||||
/**
|
||||
* raw.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $raw = [];
|
||||
|
||||
/**
|
||||
* Bootstrap.
|
||||
*
|
||||
* @param string $message
|
||||
* @param int $code
|
||||
* @param array|string $raw
|
||||
* @param \Throwable|null $previous
|
||||
*/
|
||||
public function __construct($message = '', $code = self::UNKNOWN_ERROR, $raw = [], Throwable $previous = null)
|
||||
{
|
||||
$this->raw = is_array($raw) ? $raw : [$raw];
|
||||
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
}
|
22
src/Exception/FrozenServiceException.php
Normal file
22
src/Exception/FrozenServiceException.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace Yansongda\Pay\Exception;
|
||||
|
||||
use Throwable;
|
||||
|
||||
class FrozenServiceException extends Exception
|
||||
{
|
||||
/**
|
||||
* Bootstrap.
|
||||
*
|
||||
* @param string $message
|
||||
* @param int $code
|
||||
* @param array $raw
|
||||
* @param \Throwable|null $previous
|
||||
*
|
||||
*/
|
||||
public function __construct($message = 'Frozen Service Exception!', $code = self::FROZEN_SERVICE, $raw = [], Throwable $previous = null)
|
||||
{
|
||||
parent::__construct($message, $code, $raw, $previous);
|
||||
}
|
||||
}
|
22
src/Exception/GatewayServiceException.php
Normal file
22
src/Exception/GatewayServiceException.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace Yansongda\Pay\Exception;
|
||||
|
||||
use Throwable;
|
||||
|
||||
class GatewayServiceException extends Exception
|
||||
{
|
||||
/**
|
||||
* Bootstrap.
|
||||
*
|
||||
* @param string $message
|
||||
* @param int $code
|
||||
* @param array $raw
|
||||
* @param \Throwable|null $previous
|
||||
*
|
||||
*/
|
||||
public function __construct($message = 'Gateway Service Exception!', $code = self::GATEWAY_SERVICE, $raw = [], Throwable $previous = null)
|
||||
{
|
||||
parent::__construct($message, $code, $raw, $previous);
|
||||
}
|
||||
}
|
22
src/Exception/UnknownServiceException.php
Normal file
22
src/Exception/UnknownServiceException.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace Yansongda\Pay\Exception;
|
||||
|
||||
use Throwable;
|
||||
|
||||
class UnknownServiceException extends Exception
|
||||
{
|
||||
/**
|
||||
* Bootstrap.
|
||||
*
|
||||
* @param string $message
|
||||
* @param int $code
|
||||
* @param array $raw
|
||||
* @param \Throwable|null $previous
|
||||
*
|
||||
*/
|
||||
public function __construct($message = 'Unknown Service Exception!', $code = self::UNKNOWN_SERVICE, $raw = [], Throwable $previous = null)
|
||||
{
|
||||
parent::__construct($message, $code, $raw, $previous);
|
||||
}
|
||||
}
|
195
src/Pay.php
195
src/Pay.php
@ -3,19 +3,18 @@
|
||||
namespace Yansongda\Pay;
|
||||
|
||||
use Pimple\Container;
|
||||
use Pimple\Exception\FrozenServiceException;
|
||||
use Pimple\Exception\UnknownIdentifierException;
|
||||
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\Str;
|
||||
|
||||
class Pay extends Container
|
||||
{
|
||||
/**
|
||||
* baseConfig.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $baseConfig;
|
||||
|
||||
/**
|
||||
* config.
|
||||
*
|
||||
@ -23,6 +22,20 @@ class Pay extends Container
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* service.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $service;
|
||||
|
||||
/**
|
||||
* baseConfig.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $baseConfig;
|
||||
|
||||
/**
|
||||
* baseService.
|
||||
*
|
||||
@ -34,13 +47,6 @@ class Pay extends Container
|
||||
EventService::class,
|
||||
];
|
||||
|
||||
/**
|
||||
* service.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $service;
|
||||
|
||||
/**
|
||||
* Bootstrap.
|
||||
*
|
||||
@ -51,16 +57,104 @@ class Pay extends Container
|
||||
*/
|
||||
public function __construct(array $config, array $value = [])
|
||||
{
|
||||
$this->baseConfig = $config;
|
||||
$this->config = $config;
|
||||
|
||||
parent::__construct($value);
|
||||
|
||||
$this->registerService();
|
||||
}
|
||||
|
||||
/**
|
||||
* __get.
|
||||
*
|
||||
* @author yansongda <me@yansongda.cn>
|
||||
*
|
||||
* @param $key
|
||||
*
|
||||
* @throws \Yansongda\Pay\Exception\UnknownServiceException
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function __get($key)
|
||||
{
|
||||
return $this->get($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* __set.
|
||||
*
|
||||
* @author yansongda <me@yansongda.cn>
|
||||
*
|
||||
* @param $key
|
||||
* @param $value
|
||||
*
|
||||
* @throws \Yansongda\Pay\Exception\FrozenServiceException
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __set($key, $value)
|
||||
{
|
||||
$this->set($key, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* __callStatic.
|
||||
*
|
||||
* @author yansongda <me@yansongda.cn>
|
||||
*
|
||||
* @param $method
|
||||
* @param $params
|
||||
*
|
||||
* @return \Yansongda\Pay\Contract\ServiceInterface
|
||||
*/
|
||||
public static function __callStatic($method, $params)
|
||||
{
|
||||
$app = new static(...$params);
|
||||
|
||||
$app->create();
|
||||
return $app->create($method);
|
||||
}
|
||||
|
||||
/**
|
||||
* get.
|
||||
*
|
||||
* @author yansongda <me@yansongda.cn>
|
||||
*
|
||||
* @param $key
|
||||
*
|
||||
* @throws \Yansongda\Pay\Exception\UnknownServiceException
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($key)
|
||||
{
|
||||
try {
|
||||
$result = $this->offsetGet($key);
|
||||
} catch (UnknownIdentifierException $e) {
|
||||
throw new UnknownServiceException();
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* set.
|
||||
*
|
||||
* @author yansongda <me@yansongda.cn>
|
||||
*
|
||||
* @param $key
|
||||
* @param $value
|
||||
*
|
||||
* @throws \Yansongda\Pay\Exception\FrozenServiceException
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function set($key, $value)
|
||||
{
|
||||
try {
|
||||
$this->offsetSet($key, $value);
|
||||
} catch (FrozenServiceException $e) {
|
||||
throw new Exception\FrozenServiceException();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -75,8 +169,73 @@ class Pay extends Container
|
||||
return array_merge($this->baseConfig, $this->config);
|
||||
}
|
||||
|
||||
protected function create()
|
||||
/**
|
||||
* create.
|
||||
*
|
||||
* @author yansongda <me@yansongda.cn>
|
||||
*
|
||||
* @param $method
|
||||
*
|
||||
* @throws \Yansongda\Pay\Exception\GatewayServiceException
|
||||
*
|
||||
* @return ServiceInterface
|
||||
*/
|
||||
protected function create($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");
|
||||
}
|
||||
|
||||
return $this[$method];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* make.
|
||||
*
|
||||
* @author yansongda <me@yansongda.cn>
|
||||
*
|
||||
* @param $service
|
||||
*
|
||||
* @throws \Yansongda\Pay\Exception\GatewayServiceException
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function make($service)
|
||||
{
|
||||
$gatewayService = new $service($this);
|
||||
|
||||
if ($gatewayService instanceof ServiceInterface) {
|
||||
$this->registerService($gatewayService);
|
||||
}
|
||||
|
||||
throw new GatewayServiceException("Gateway [{$service}] Must Be An Instance Of ServiceInterface");
|
||||
}
|
||||
|
||||
/**
|
||||
* registerService.
|
||||
*
|
||||
* @author yansongda <me@yansongda.cn>
|
||||
*
|
||||
* @param \Yansongda\Pay\Contract\ServiceInterface|null $service
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function registerService(?ServiceInterface $service = null)
|
||||
{
|
||||
if (!is_null($service)) {
|
||||
parent::register($service);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (array_merge($this->baseService, $this->service) as $service) {
|
||||
parent::register(new $service());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ namespace Yansongda\Pay\Service;
|
||||
|
||||
use Pimple\Container;
|
||||
use Yansongda\Pay\Contract\ServiceInterface;
|
||||
use Yansongda\Supports\Collection;
|
||||
|
||||
class ConfigService implements ServiceInterface
|
||||
{
|
||||
@ -17,6 +18,9 @@ class ConfigService implements ServiceInterface
|
||||
*/
|
||||
public function register(Container $pimple)
|
||||
{
|
||||
|
||||
$pimple['config'] = function ($container) {
|
||||
/* @var \Yansongda\Pay\Pay $container */
|
||||
return new Collection($container->getConfig());
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,21 @@
|
||||
|
||||
namespace Yansongda\Pay\Service;
|
||||
|
||||
class EventService
|
||||
{
|
||||
use Pimple\Container;
|
||||
use Yansongda\Pay\Contract\ServiceInterface;
|
||||
|
||||
}
|
||||
class EventService implements ServiceInterface
|
||||
{
|
||||
/**
|
||||
* Registers services on the given container.
|
||||
*
|
||||
* This method should only be used to configure services and parameters.
|
||||
* It should not get services.
|
||||
*
|
||||
* @param Container $pimple A container instance
|
||||
*/
|
||||
public function register (Container $pimple)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Yansongda\Pay\Service\Gateway\Alipay;
|
||||
|
||||
class AlipayService
|
||||
{
|
||||
|
||||
}
|
7
src/Service/Gateway/AlipayService.php
Normal file
7
src/Service/Gateway/AlipayService.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Yansongda\Pay\Service\Gateway;
|
||||
|
||||
class AlipayService
|
||||
{
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Yansongda\Pay\Service\Gateway\Wechat;
|
||||
|
||||
class WechatService
|
||||
{
|
||||
|
||||
}
|
7
src/Service/Gateway/WechatService.php
Normal file
7
src/Service/Gateway/WechatService.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Yansongda\Pay\Service\Gateway;
|
||||
|
||||
class WechatService
|
||||
{
|
||||
}
|
@ -4,6 +4,7 @@ namespace Yansongda\Pay\Service;
|
||||
|
||||
use Pimple\Container;
|
||||
use Yansongda\Pay\Contract\ServiceInterface;
|
||||
use Yansongda\Supports\Logger;
|
||||
|
||||
class LoggerService implements ServiceInterface
|
||||
{
|
||||
@ -17,6 +18,22 @@ class LoggerService implements ServiceInterface
|
||||
*/
|
||||
public function register(Container $pimple)
|
||||
{
|
||||
$pimple['logger'] = $pimple['log'] = function ($container) {
|
||||
/* @var \Yansongda\Pay\Pay $container */
|
||||
$logger = new Logger();
|
||||
|
||||
$config = ['identify' => 'yansongda.pay'];
|
||||
|
||||
if (isset($container['config']['log'])) {
|
||||
$config = array_merge(
|
||||
$config,
|
||||
$container['config']['log']
|
||||
);
|
||||
}
|
||||
|
||||
$logger->setConfig($config);
|
||||
|
||||
return $logger->getLogger();
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
8
tests/TestCase.php
Normal file
8
tests/TestCase.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Yansongda\Pay\Tests;
|
||||
|
||||
class TestCase extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user