This commit is contained in:
yansongda 2019-09-08 17:39:09 +08:00
parent f3273688e7
commit 9bee6312c2
15 changed files with 347 additions and 42 deletions

View File

@ -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"

View File

@ -6,4 +6,4 @@ use Pimple\ServiceProviderInterface;
interface ServiceInterface extends ServiceProviderInterface
{
}
}

View 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);
}
}

View 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);
}
}

View 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);
}
}

View 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);
}
}

View File

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

View File

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

View File

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

View File

@ -1,8 +0,0 @@
<?php
namespace Yansongda\Pay\Service\Gateway\Alipay;
class AlipayService
{
}

View File

@ -0,0 +1,7 @@
<?php
namespace Yansongda\Pay\Service\Gateway;
class AlipayService
{
}

View File

@ -1,8 +0,0 @@
<?php
namespace Yansongda\Pay\Service\Gateway\Wechat;
class WechatService
{
}

View File

@ -0,0 +1,7 @@
<?php
namespace Yansongda\Pay\Service\Gateway;
class WechatService
{
}

View File

@ -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
View File

@ -0,0 +1,8 @@
<?php
namespace Yansongda\Pay\Tests;
class TestCase extends \PHPUnit\Framework\TestCase
{
}