From 43fda349d60148faaa8be43addf7cf8e5e6b939e Mon Sep 17 00:00:00 2001 From: yansongda Date: Mon, 3 Feb 2020 09:30:05 +0800 Subject: [PATCH] update --- src/Contract/ServiceProviderInterface.php | 10 +- .../ContainerDependencyException.php | 20 ++ src/Exception/ContainerException.php | 21 ++ src/Exception/ContainerNotFoundException.php | 21 ++ src/Exception/Exception.php | 25 +- src/Exception/FrozenServiceException.php | 20 -- src/Exception/ServiceException.php | 12 +- src/Exception/ServiceProviderException.php | 20 -- src/Exception/UnknownServiceException.php | 20 -- src/Pay.php | 226 ++++++------------ src/Service/ConfigServiceProvider.php | 1 - src/Service/LoggerServiceProvider.php | 10 +- 12 files changed, 169 insertions(+), 237 deletions(-) create mode 100644 src/Exception/ContainerDependencyException.php create mode 100644 src/Exception/ContainerException.php create mode 100644 src/Exception/ContainerNotFoundException.php delete mode 100644 src/Exception/FrozenServiceException.php delete mode 100644 src/Exception/ServiceProviderException.php delete mode 100644 src/Exception/UnknownServiceException.php diff --git a/src/Contract/ServiceProviderInterface.php b/src/Contract/ServiceProviderInterface.php index 2a05cdc..5673726 100644 --- a/src/Contract/ServiceProviderInterface.php +++ b/src/Contract/ServiceProviderInterface.php @@ -2,8 +2,12 @@ namespace Yansongda\Pay\Contract; -use Pimple\ServiceProviderInterface as BaseServiceProviderInterface; - -interface ServiceProviderInterface extends BaseServiceProviderInterface +interface ServiceProviderInterface { + /** + * register the service. + * + * @author yansongda + */ + public function register(): void; } diff --git a/src/Exception/ContainerDependencyException.php b/src/Exception/ContainerDependencyException.php new file mode 100644 index 0000000..e1366e4 --- /dev/null +++ b/src/Exception/ContainerDependencyException.php @@ -0,0 +1,20 @@ +raw = is_array($raw) ? $raw : [$raw]; + $this->extra = is_array($extra) ? $extra : [$extra]; parent::__construct($message, $code, $previous); } diff --git a/src/Exception/FrozenServiceException.php b/src/Exception/FrozenServiceException.php deleted file mode 100644 index 0143804..0000000 --- a/src/Exception/FrozenServiceException.php +++ /dev/null @@ -1,20 +0,0 @@ - - * - * @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 * - * @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 - * - * @param mixed $value - * - * @throws \Yansongda\Pay\Exception\FrozenServiceException - */ - public function __set(string $key, $value): void - { - $this->set($key, $value); - } - - /** - * __get. - * - * @author yansongda - * - * @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 * - * @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 * + * @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 - */ - public function getUserConfig(): array - { - return $this->userConfig; - } - - /** - * create. - * - * @author yansongda - * - * @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 - */ - 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 - * - * @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 * - * @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 + * + * @throws \Yansongda\Pay\Exception\ContainerException + * + * @return void + */ + private function initContainer() + { + self::getContainer(); + } + + /** + * registerService. + * + * @author yansongda + */ + private function registerService(array $config): void + { + foreach (array_merge($this->baseService, $this->service) as $service) { + $var = new $service(); + + if ($var instanceof ServiceProviderInterface) { + $var->register(); + } + } } } diff --git a/src/Service/ConfigServiceProvider.php b/src/Service/ConfigServiceProvider.php index baea04c..70332c1 100644 --- a/src/Service/ConfigServiceProvider.php +++ b/src/Service/ConfigServiceProvider.php @@ -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; diff --git a/src/Service/LoggerServiceProvider.php b/src/Service/LoggerServiceProvider.php index 0f74dd6..471dc0c 100644 --- a/src/Service/LoggerServiceProvider.php +++ b/src/Service/LoggerServiceProvider.php @@ -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 */