initContainer(); $this->registerServices($config); } /** * __callStatic. * * @throws \Yansongda\Pay\Exception\ContainerDependencyException * @throws \Yansongda\Pay\Exception\ContainerException * @throws \Yansongda\Pay\Exception\ServiceNotFoundException * * @return mixed */ public static function __callStatic(string $service, array $config) { if (!empty($config)) { self::config(...$config); } return self::get($service); } /** * 初始化容器、配置等信息. * * @throws \Yansongda\Pay\Exception\ContainerDependencyException * @throws \Yansongda\Pay\Exception\ContainerException * @throws \Yansongda\Pay\Exception\ServiceNotFoundException */ public static function config(array $config = []): Pay { if (self::hasContainer() && !($config['_force'] ?? false)) { return self::get(Pay::class); } return new self($config); } /** * 定义. * * @param mixed $value * * @throws \Yansongda\Pay\Exception\ContainerException */ public static function set(string $name, $value): void { Pay::getContainer()->set($name, $value); } /** * @throws \Yansongda\Pay\Exception\ContainerDependencyException * @throws \Yansongda\Pay\Exception\ContainerException * @throws \Yansongda\Pay\Exception\ServiceNotFoundException * * @return mixed */ public static function make(string $service, array $parameters = []) { try { return Pay::getContainer()->make(...func_get_args()); } catch (NotFoundException $e) { throw new ServiceNotFoundException($e->getMessage()); } catch (DependencyException $e) { throw new ContainerDependencyException($e->getMessage()); } catch (Throwable $e) { throw new ContainerException($e->getMessage()); } } /** * 获取服务. * * @throws \Yansongda\Pay\Exception\ContainerDependencyException * @throws \Yansongda\Pay\Exception\ServiceNotFoundException * @throws \Yansongda\Pay\Exception\ContainerException * * @return mixed */ public static function get(string $service) { try { return Pay::getContainer()->get($service); } catch (NotFoundException $e) { throw new ServiceNotFoundException($e->getMessage()); } catch (DependencyException $e) { throw new ContainerDependencyException($e->getMessage()); } catch (Throwable $e) { throw new ContainerException($e->getMessage()); } } /** * @throws \Yansongda\Pay\Exception\ContainerException */ public static function has(string $service): bool { return Pay::getContainer()->has($service); } /** * getContainer. * * @throws \Yansongda\Pay\Exception\ContainerNotFoundException */ public static function getContainer(): Container { if (self::hasContainer()) { return self::$container; } throw new ContainerNotFoundException('You should init/config PAY first', Exception\Exception::CONTAINER_NOT_FOUND); } /** * has Container. */ public static function hasContainer(): bool { return isset(self::$container) && self::$container instanceof Container; } /** * clear. */ public static function clear(): void { self::$container = null; } /** * 注册服务. * * @throws \Yansongda\Pay\Exception\ContainerDependencyException * @throws \Yansongda\Pay\Exception\ContainerException * @throws \Yansongda\Pay\Exception\ServiceNotFoundException */ public static function registerService(string $service, array $config): void { $var = self::get($service); if ($var instanceof ServiceProviderInterface) { $var->register(self::get(Pay::class), $config); } } /** * initContainer. * * @throws \Yansongda\Pay\Exception\ContainerException */ private function initContainer(): void { $builder = new ContainerBuilder(); $builder->useAnnotations(false); try { $container = $builder->build(); $container->set(ContainerInterface::class, $container); $container->set(\Psr\Container\ContainerInterface::class, $container); $container->set(Pay::class, $this); self::$container = $container; } catch (Throwable $e) { throw new ContainerException($e->getMessage()); } } /** * register services. * * @throws \Yansongda\Pay\Exception\ContainerDependencyException * @throws \Yansongda\Pay\Exception\ContainerException * @throws \Yansongda\Pay\Exception\ServiceNotFoundException */ private function registerServices(array $config): void { foreach (array_merge($this->coreService, $this->service) as $service) { self::registerService($service, $config); } } }