This commit is contained in:
yansongda 2020-02-03 09:30:05 +08:00
parent d2ca9d2428
commit 43fda349d6
12 changed files with 169 additions and 237 deletions

View File

@ -2,8 +2,12 @@
namespace Yansongda\Pay\Contract;
use Pimple\ServiceProviderInterface as BaseServiceProviderInterface;
interface ServiceProviderInterface extends BaseServiceProviderInterface
interface ServiceProviderInterface
{
/**
* register the service.
*
* @author yansongda <me@yansongda.cn>
*/
public function register(): void;
}

View File

@ -0,0 +1,20 @@
<?php
namespace Yansongda\Pay\Exception;
use Throwable;
class ContainerDependencyException extends ContainerException
{
/**
* Bootstrap.
*
* @param string $message
* @param array $extra
* @param int $code
*/
public function __construct($message = 'Dependency Resolve Error', $extra = [], $code = self::CONTAINER_DEPENDENCY_ERROR, Throwable $previous = null)
{
parent::__construct($message, $extra, $code, $previous);
}
}

View File

@ -0,0 +1,21 @@
<?php
namespace Yansongda\Pay\Exception;
use Psr\Container\ContainerExceptionInterface;
use Throwable;
class ContainerException extends Exception implements ContainerExceptionInterface
{
/**
* Bootstrap.
*
* @param string $message
* @param int $code
* @param array $extra
*/
public function __construct($message = '', $extra = [], $code = self::CONTAINER_ERROR, Throwable $previous = null)
{
parent::__construct($message, $extra, $code, $previous);
}
}

View File

@ -0,0 +1,21 @@
<?php
namespace Yansongda\Pay\Exception;
use Psr\Container\NotFoundExceptionInterface;
use Throwable;
class ContainerNotFoundException extends ContainerException implements NotFoundExceptionInterface
{
/**
* Bootstrap.
*
* @param string $message
* @param array $extra
* @param int $code
*/
public function __construct($message = 'Container Not Found', $extra = [], $code = self::NOT_FOUND_CONTAINER, Throwable $previous = null)
{
parent::__construct($message, $extra, $code, $previous);
}
}

View File

@ -8,32 +8,35 @@ class Exception extends \Exception
{
const UNKNOWN_ERROR = 9999;
// Service
const SERVICE_EXCEPTION = 1000;
/**
* about container di.
*/
const CONTAINER_ERROR = 1000;
const UNKNOWN_SERVICE = 1001;
const NOT_FOUND_CONTAINER = 1001;
const FROZEN_SERVICE = 1002;
const CONTAINER_DEPENDENCY_ERROR = 1002;
const GATEWAY_SERVICE = 1003;
/**
* about service.
*/
const SERVICE_EXCEPTION = 2000;
/**
* raw.
*
* @var array
*/
public $raw = [];
public $extra = [];
/**
* Bootstrap.
*
* @param string $message
* @param int $code
* @param array|string $raw
* @param array|string $extra
*/
public function __construct($message = '', $code = self::UNKNOWN_ERROR, $raw = [], Throwable $previous = null)
public function __construct($message = 'Unknown Error', $extra = [], $code = self::UNKNOWN_ERROR, Throwable $previous = null)
{
$this->raw = is_array($raw) ? $raw : [$raw];
$this->extra = is_array($extra) ? $extra : [$extra];
parent::__construct($message, $code, $previous);
}

View File

@ -1,20 +0,0 @@
<?php
namespace Yansongda\Pay\Exception;
use Throwable;
class FrozenServiceException extends ServiceException
{
/**
* Bootstrap.
*
* @param string $message
* @param int $code
* @param array $raw
*/
public function __construct($message = 'Frozen Service Exception!', $code = self::FROZEN_SERVICE, $raw = [], Throwable $previous = null)
{
parent::__construct($message, $code, $raw, $previous);
}
}

View File

@ -10,11 +10,15 @@ class ServiceException extends Exception
* Bootstrap.
*
* @param string $message
* @param array $extra
* @param int $code
* @param array $raw
*/
public function __construct($message = 'Service Exception', $code = self::SERVICE_EXCEPTION, $raw = [], Throwable $previous = null)
{
parent::__construct($message, $code, $raw, $previous);
public function __construct(
$message = 'Service Error',
$extra = [],
$code = self::SERVICE_EXCEPTION,
Throwable $previous = null
) {
parent::__construct($message, $extra, $code, $previous);
}
}

View File

@ -1,20 +0,0 @@
<?php
namespace Yansongda\Pay\Exception;
use Throwable;
class ServiceProviderException extends ServiceException
{
/**
* Bootstrap.
*
* @param string $message
* @param int $code
* @param array $raw
*/
public function __construct($message = 'Gateway Service Exception!', $code = self::GATEWAY_SERVICE, $raw = [], Throwable $previous = null)
{
parent::__construct($message, $code, $raw, $previous);
}
}

View File

@ -1,20 +0,0 @@
<?php
namespace Yansongda\Pay\Exception;
use Throwable;
class UnknownServiceException extends ServiceException
{
/**
* Bootstrap.
*
* @param string $message
* @param int $code
* @param array $raw
*/
public function __construct($message = 'Unknown Service Exception!', $code = self::UNKNOWN_SERVICE, $raw = [], Throwable $previous = null)
{
parent::__construct($message, $code, $raw, $previous);
}
}

View File

@ -4,43 +4,29 @@ namespace Yansongda\Pay;
use DI\Container;
use DI\ContainerBuilder;
use Symfony\Component\EventDispatcher\EventDispatcher;
use DI\DependencyException;
use DI\NotFoundException;
use Exception;
use Yansongda\Pay\Contract\ServiceInterface;
use Yansongda\Pay\Contract\ServiceProviderInterface;
use Yansongda\Pay\Exception\ContainerDependencyException;
use Yansongda\Pay\Exception\ContainerException;
use Yansongda\Pay\Exception\ContainerNotFoundException;
use Yansongda\Pay\Exception\ServiceException;
use Yansongda\Pay\Exception\ServiceProviderException;
use Yansongda\Pay\Exception\UnknownServiceException;
use Yansongda\Pay\Service\AlipayServiceProvider;
use Yansongda\Pay\Service\ConfigServiceProvider;
use Yansongda\Pay\Service\EventServiceProvider;
use Yansongda\Pay\Service\LoggerServiceProvider;
use Yansongda\Pay\Service\WechatServiceProvider;
use Yansongda\Supports\Config;
use Yansongda\Supports\Logger;
use Yansongda\Supports\Str;
/**
* @author yansongda <me@yansongda.cn>
*
* @property \Yansongda\Supports\Logger logger
* @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
{
/**
* config.
*
* @var array
*/
protected $userConfig = [];
protected $middleware = [];
/**
@ -64,50 +50,22 @@ class Pay
EventServiceProvider::class,
];
private $container;
/**
* @var \DI\Container
*/
private static $container;
/**
* Bootstrap.
*
* @author yansongda <me@yansongda.cn>
*
* @param array $c customer config
*
* @throws \Exception
* @throws \Yansongda\Pay\Exception\ContainerException
*/
public function __construct(array $c, array $value = [])
public function __construct(array $config)
{
$this->userConfig = $c;
$this->container = $this->getContainer();
$this->registerService();
}
/**
* __set.
*
* @author yansongda <me@yansongda.cn>
*
* @param mixed $value
*
* @throws \Yansongda\Pay\Exception\FrozenServiceException
*/
public function __set(string $key, $value): void
{
$this->set($key, $value);
}
/**
* __get.
*
* @author yansongda <me@yansongda.cn>
*
* @throws \Yansongda\Pay\Exception\ServiceException
* @throws \Yansongda\Pay\Exception\UnknownServiceException
*/
public function __get(string $key): ServiceInterface
{
return $this->get($key);
$this->initContainer();
$this->registerService($config);
}
/**
@ -115,20 +73,18 @@ class Pay
*
* @author yansongda <me@yansongda.cn>
*
* @param $method
* @param $params
*
* @throws \Yansongda\Pay\Exception\ContainerDependencyException
* @throws \Yansongda\Pay\Exception\ContainerException
* @throws \Yansongda\Pay\Exception\ContainerNotFoundException
* @throws \Yansongda\Pay\Exception\ServiceException
* @throws \Yansongda\Pay\Exception\ServiceProviderException
* @throws \Yansongda\Pay\Exception\UnknownServiceException
*
* @return \Yansongda\Pay\Contract\ServiceInterface
*/
public static function __callStatic($method, $params): ServiceInterface
public static function __callStatic(string $service, array $config): ServiceInterface
{
$app = new static(...$params);
$pay = new self($config);
$app->create($method);
return $app->get($method);
return $pay->get($service);
}
/**
@ -136,15 +92,19 @@ class Pay
*
* @author yansongda <me@yansongda.cn>
*
* @throws \Yansongda\Pay\Exception\ContainerDependencyException
* @throws \Yansongda\Pay\Exception\ContainerException
* @throws \Yansongda\Pay\Exception\ContainerNotFoundException
* @throws \Yansongda\Pay\Exception\ServiceException
* @throws \Yansongda\Pay\Exception\UnknownServiceException
*/
public function get(string $key): ServiceInterface
{
try {
$result = $this->offsetGet($key);
} catch (UnknownIdentifierException $e) {
throw new UnknownServiceException();
$result = self::getContainer()->get($key);
} catch (NotFoundException $e) {
throw new ContainerNotFoundException($e->getMessage());
} catch (DependencyException $e) {
throw new ContainerDependencyException($e->getMessage());
}
if ($result instanceof ServiceInterface) {
@ -161,83 +121,11 @@ class Pay
*
* @param mixed $value
*
* @throws \Yansongda\Pay\Exception\FrozenServiceException
* @throws \Yansongda\Pay\Exception\ContainerException
*/
public function set(string $key, $value): void
{
try {
$this->offsetSet($key, $value);
} catch (FrozenServiceException $e) {
throw new Exception\FrozenServiceException();
}
}
/**
* getConfig.
*
* @author yansongda <me@yansongda.cn>
*/
public function getUserConfig(): array
{
return $this->userConfig;
}
/**
* create.
*
* @author yansongda <me@yansongda.cn>
*
* @throws \Yansongda\Pay\Exception\ServiceProviderException
*/
protected function create(string $method): void
{
if (isset($this[$method])) {
return;
}
$service = __NAMESPACE__.'\\Service\\'.Str::studly($method).'Service';
if (class_exists($service)) {
self::make($service);
}
throw new ServiceProviderException("ServiceProvider [{$method}] Not Exists");
}
/**
* registerService.
*
* @author yansongda <me@yansongda.cn>
*/
private function registerService(?ServiceProviderInterface $service = null): void
{
if (!is_null($service)) {
parent::register($service);
return;
}
foreach (array_merge($this->baseService, $this->service) as $service) {
parent::register(new $service());
}
}
/**
* make.
*
* @author yansongda <me@yansongda.cn>
*
* @throws \Yansongda\Pay\Exception\ServiceProviderException
*/
private function make(string $service): void
{
$gatewayService = new $service($this);
if ($gatewayService instanceof ServiceProviderInterface) {
$this->registerService($gatewayService);
}
throw new ServiceProviderException("[{$service}] Must Be An Instance Of ServiceProviderInterface");
self::getContainer()->set($key, $value);
}
/**
@ -245,16 +133,54 @@ class Pay
*
* @author yansongda <me@yansongda.cn>
*
* @throws \Exception
*
* @return \DI\Container
* @throws \Yansongda\Pay\Exception\ContainerException
*/
private function getContainer(): Container
public static function getContainer(): Container
{
if (self::$container instanceof Container) {
return self::$container;
}
$builder = new ContainerBuilder();
$builder->useAnnotations(true);
$builder->useAnnotations(false);
return $builder->build();
try {
self::$container = $builder->build();
return self::$container;
} catch (Exception $e) {
throw new ContainerException($e->getMessage());
}
}
/**
* setContainer.
*
* @author yansongda <me@yansongda.cn>
*
* @throws \Yansongda\Pay\Exception\ContainerException
*
* @return void
*/
private function initContainer()
{
self::getContainer();
}
/**
* registerService.
*
* @author yansongda <me@yansongda.cn>
*/
private function registerService(array $config): void
{
foreach (array_merge($this->baseService, $this->service) as $service) {
$var = new $service();
if ($var instanceof ServiceProviderInterface) {
$var->register();
}
}
}
}

View File

@ -2,7 +2,6 @@
namespace Yansongda\Pay\Service;
use Pimple\Container;
use Yansongda\Pay\Contract\ServiceInterface;
use Yansongda\Pay\Contract\ServiceProviderInterface;
use Yansongda\Supports\Config;

View File

@ -2,7 +2,6 @@
namespace Yansongda\Pay\Service;
use Pimple\Container;
use Yansongda\Pay\Contract\ServiceInterface;
use Yansongda\Pay\Contract\ServiceProviderInterface;
use Yansongda\Pay\Pay;
@ -11,14 +10,9 @@ use Yansongda\Supports\Logger;
class LoggerServiceProvider implements ServiceProviderInterface
{
/**
* 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
* {@inheritdoc}
*/
public function register(Container $pimple)
public function register(): void
{
$pimple['logger'] = $pimple['log'] = function ($container) {
/* @var \Yansongda\Pay\Pay $container */