文件
yjyf/server/addons/epay/library/v3/Yansongda/Pay/Service/ContainerServiceProvider.php
YuanWen 78d45eea74 chore: 新增epay和cos插件相关类库及配置文件
- 新增epay v2、v3、v3new版本的支付类库及证书文件
- 新增cos云存储类库文件
- 调整文件上传保存格式为按分类存储
- 修复会员、规则、类目等语言翻译键名
- 新增小程序图标等静态资源文件
2026-11-04 09:33:57 +08:00

108 行
2.9 KiB
PHP

<?php
declare(strict_types=1);
namespace Yansongda\Pay\Service;
use Closure;
use Hyperf\Pimple\ContainerFactory as DefaultContainer;
use Hyperf\Utils\ApplicationContext as HyperfContainer;
use Illuminate\Container\Container as LaravelContainer;
use Psr\Container\ContainerInterface;
use Yansongda\Pay\Contract\ServiceProviderInterface;
use Yansongda\Pay\Exception\ContainerException;
use Yansongda\Pay\Exception\ContainerNotFoundException;
use Yansongda\Pay\Exception\Exception;
use Yansongda\Pay\Pay;
/**
* @codeCoverageIgnore
*/
class ContainerServiceProvider implements ServiceProviderInterface
{
private array $detectApplication = [
'laravel' => LaravelContainer::class,
'hyperf' => HyperfContainer::class,
];
/**
* @param mixed $data
*
* @throws ContainerException
*/
public function register($data = null): void
{
if ($data instanceof ContainerInterface || $data instanceof Closure) {
Pay::setContainer($data);
return;
}
if (Pay::hasContainer()) {
return;
}
foreach ($this->detectApplication as $framework => $application) {
$method = $framework.'Application';
if (class_exists($application) && method_exists($this, $method) && $this->{$method}()) {
return;
}
}
$this->defaultApplication();
}
/**
* @throws ContainerException
* @throws ContainerNotFoundException
*/
protected function laravelApplication(): bool
{
Pay::setContainer(static fn () => LaravelContainer::getInstance());
Pay::set(\Yansongda\Pay\Contract\ContainerInterface::class, LaravelContainer::getInstance());
if (!Pay::has(ContainerInterface::class)) {
Pay::set(ContainerInterface::class, LaravelContainer::getInstance());
}
return true;
}
/**
* @throws ContainerException
* @throws ContainerNotFoundException
*/
protected function hyperfApplication(): bool
{
if (!HyperfContainer::hasContainer()) {
return false;
}
Pay::setContainer(static fn () => HyperfContainer::getContainer());
Pay::set(\Yansongda\Pay\Contract\ContainerInterface::class, HyperfContainer::getContainer());
if (!Pay::has(ContainerInterface::class)) {
Pay::set(ContainerInterface::class, HyperfContainer::getContainer());
}
return true;
}
/**
* @throws ContainerNotFoundException
*/
protected function defaultApplication(): void
{
if (!class_exists(DefaultContainer::class)) {
throw new ContainerNotFoundException('Init failed! Maybe you should install `hyperf/pimple` first', Exception::CONTAINER_NOT_FOUND);
}
$container = (new DefaultContainer())();
Pay::setContainer($container);
}
}