初始化本地仓库
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
/**
|
||||
*+------------------
|
||||
* madong
|
||||
*+------------------
|
||||
* Copyright (c) https://gitee.com/motion-code All rights reserved.
|
||||
*+------------------
|
||||
* Author: Mr. April (405784684@qq.com)
|
||||
*+------------------
|
||||
* Official Website: https://madong.tech
|
||||
*/
|
||||
|
||||
namespace core\uuid;
|
||||
|
||||
use madong\helper\Snowflake as SnowflakeGenerator;
|
||||
use core\exception\handler\AdminException;
|
||||
use RuntimeException;
|
||||
use Webman\App;
|
||||
|
||||
/**
|
||||
* 雪花ID生成
|
||||
*
|
||||
* @author Mr.April
|
||||
* @since 1.0
|
||||
* @method static generate()
|
||||
*/
|
||||
final class Snowflake
|
||||
{
|
||||
/**
|
||||
* @var \madong\helper\Snowflake|null 唯一的 Snowflake 实例
|
||||
*/
|
||||
private static ?SnowflakeGenerator $instance = null;
|
||||
|
||||
/**
|
||||
* 私有构造方法 —— 禁止外部 new
|
||||
*/
|
||||
private function __construct()
|
||||
{
|
||||
// 禁止外部实例化
|
||||
}
|
||||
|
||||
/**
|
||||
* 私有克隆方法 —— 禁止 clone
|
||||
*/
|
||||
private function __clone()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* 私有反序列化方法 —— 禁止反序列化恢复对象
|
||||
*/
|
||||
public function __wakeup()
|
||||
{
|
||||
throw new RuntimeException("Cannot unserialize singleton.");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取唯一的 Snowflake 实例(懒加载)
|
||||
*
|
||||
* @return \madong\helper\Snowflake
|
||||
*/
|
||||
private static function instance(): SnowflakeGenerator
|
||||
{
|
||||
if (self::$instance === null) {
|
||||
$nodeId = 1;
|
||||
$workerId = App::worker() ? (App::worker()->id ?: 0) : 0;
|
||||
self::$instance = new SnowflakeGenerator(
|
||||
$nodeId,
|
||||
$workerId,
|
||||
array_merge([
|
||||
'node_id_bits' => 3,
|
||||
'worker_id_bits' => 7,
|
||||
'sequence_bits' => 12,
|
||||
], config('core.uuid.snowflake', []))
|
||||
|
||||
);
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* 静态代理调用:将所有静态方法调用转发到真正的 Snowflake 实例上
|
||||
*
|
||||
* @param string $method 方法名,比如 "generate"
|
||||
* @param array $args 参数列表
|
||||
*
|
||||
* @return mixed
|
||||
* @throws \core\exception\handler\AdminException
|
||||
*/
|
||||
public static function __callStatic(string $method, array $args)
|
||||
{
|
||||
$instance = self::instance();
|
||||
|
||||
if (!method_exists($instance, $method)) {
|
||||
throw new AdminException(sprintf(
|
||||
'Call to undefined method %s::%s()',
|
||||
static::class,
|
||||
$method
|
||||
));
|
||||
}
|
||||
|
||||
return $instance->{$method}(...$args);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
/**
|
||||
*+------------------
|
||||
* madong
|
||||
*+------------------
|
||||
* Copyright (c) https://gitee.com/motion-code All rights reserved.
|
||||
*+------------------
|
||||
* Author: Mr. April (405784684@qq.com)
|
||||
*+------------------
|
||||
* Official Website: http://www.madong.tech
|
||||
*/
|
||||
|
||||
namespace core\uuid;
|
||||
|
||||
/**
|
||||
* uuid
|
||||
*
|
||||
* @author Mr.April
|
||||
* @since 1.0
|
||||
*/
|
||||
class UUIDGenerator
|
||||
{
|
||||
/**
|
||||
* 生成UUID
|
||||
*
|
||||
* @param string $format
|
||||
* @param int $length
|
||||
*
|
||||
* @return string|null
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function generate(string $format = 'uuid', int $length = 36): ?string
|
||||
{
|
||||
if ($format === 'uuid') {
|
||||
return self::generateUUID();
|
||||
} elseif ($format === 'custom') {
|
||||
return self::generateCustomUUID($length);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成标准格式UUID
|
||||
*
|
||||
* @return string
|
||||
* @throws \Exception
|
||||
*/
|
||||
private static function generateUUID(): string
|
||||
{
|
||||
// 生成标准UUID
|
||||
return sprintf(
|
||||
'%s-%s-%s-%s-%s',
|
||||
bin2hex(random_bytes(4)),
|
||||
bin2hex(random_bytes(2)),
|
||||
bin2hex(random_bytes(2)),
|
||||
bin2hex(random_bytes(2)),
|
||||
bin2hex(random_bytes(6))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义UUID
|
||||
*
|
||||
* @param $length
|
||||
*
|
||||
* @return string|null
|
||||
* @throws \Exception
|
||||
*/
|
||||
private static function generateCustomUUID($length): ?string
|
||||
{
|
||||
// 生成自定义长度的UUID
|
||||
if ($length < 1) {
|
||||
return null; // 长度必须大于0
|
||||
}
|
||||
return strtoupper(substr(bin2hex(random_bytes($length / 2)), 0, $length));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
/**
|
||||
*+------------------
|
||||
* madong
|
||||
*+------------------
|
||||
* Copyright (c) https://gitee.com/motion-code All rights reserved.
|
||||
*+------------------
|
||||
* Author: Mr. April (405784684@qq.com)
|
||||
*+------------------
|
||||
* Official Website: https://madong.tech
|
||||
*/
|
||||
|
||||
return [
|
||||
'enable' => true,
|
||||
];
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
/**
|
||||
*+------------------
|
||||
* madong
|
||||
*+------------------
|
||||
* Copyright (c) https://gitee.com/motion-code All rights reserved.
|
||||
*+------------------
|
||||
* Author: Mr. April (405784684@qq.com)
|
||||
*+------------------
|
||||
* Official Website: https://madong.tech
|
||||
*/
|
||||
|
||||
return [
|
||||
'enable' => true,
|
||||
'node_id' => 1,
|
||||
'worker_id' => null,//使用线程id
|
||||
'node_id_bits' => 3,
|
||||
'worker_id_bits' => 7,
|
||||
'sequence_bits' => 12,
|
||||
];
|
||||
Reference in New Issue
Block a user