mirror of
https://gitee.com/ctexthuang/hyperf-micro-svc.git
synced 2026-02-08 10:20:16 +08:00
first commit
This commit is contained in:
51
app/Lib/Jwt/JwtFactory.php
Normal file
51
app/Lib/Jwt/JwtFactory.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Lib\Jwt;
|
||||
|
||||
use App\Interface\JwtInterface;
|
||||
use Hyperf\Collection\Arr;
|
||||
use Hyperf\Contract\ConfigInterface;
|
||||
use function Hyperf\Support\make;
|
||||
|
||||
final class JwtFactory
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ConfigInterface $config,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return JwtInterface
|
||||
*/
|
||||
public function get(string $name = 'default'): JwtInterface
|
||||
{
|
||||
return make(Jwt::class, [
|
||||
'config' => $this->getConfig($name),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取场景配置
|
||||
* @param string $scene
|
||||
* @return array
|
||||
*/
|
||||
public function getConfig(string $scene): array
|
||||
{
|
||||
if ($scene === 'default') {
|
||||
return $this->config->get($this->getConfigKey());
|
||||
}
|
||||
return Arr::merge(
|
||||
$this->config->get($this->getConfigKey()),
|
||||
$this->config->get($this->getConfigKey($scene), [])
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return string
|
||||
*/
|
||||
private function getConfigKey(string $name = 'default'): string
|
||||
{
|
||||
return 'jwt.' . $name;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user