Skip to content
Merged

2.0 #10

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/mine-core/src/Aspect/SaveAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ public function process(ProceedingJoinPoint $proceedingJoinPoint)
}
}
// 生成雪花ID 或者 UUID
if ($instance instanceof MineModel &&
! $instance->incrementing &&
empty($instance->{$instance->getKeyName()})
if ($instance instanceof MineModel
&& ! $instance->incrementing
&& empty($instance->{$instance->getKeyName()})
) {
$instance->setPrimaryKeyValue($instance->getPrimaryKeyType() === 'int' ? snowflake_id() : uuid());
}
Expand Down
152 changes: 76 additions & 76 deletions src/mine-core/src/Snowflake/Configuration.php
Original file line number Diff line number Diff line change
@@ -1,76 +1,76 @@
<?php
declare(strict_types=1);
/**
* This file is part of MineAdmin.
*
* @link https://www.mineadmin.com
* @document https://doc.mineadmin.com
* @contact root@imoi.cn
* @license https://github.com/mineadmin/MineAdmin/blob/master/LICENSE
*/
namespace Mine\Snowflake;
use Hyperf\Snowflake\ConfigurationInterface;
class Configuration implements ConfigurationInterface
{
protected int $millisecondBits = 41;
protected int $dataCenterIdBits = 2;
protected int $workerIdBits = 4;
protected int $sequenceBits = 5;
public function maxWorkerId(): int
{
return -1 ^ (-1 << $this->workerIdBits);
}
public function maxDataCenterId(): int
{
return -1 ^ (-1 << $this->dataCenterIdBits);
}
public function maxSequence(): int
{
return -1 ^ (-1 << $this->sequenceBits);
}
public function getTimestampLeftShift(): int
{
return $this->sequenceBits + $this->workerIdBits + $this->dataCenterIdBits;
}
public function getDataCenterIdShift(): int
{
return $this->sequenceBits + $this->workerIdBits;
}
public function getWorkerIdShift(): int
{
return $this->sequenceBits;
}
public function getTimestampBits(): int
{
return $this->millisecondBits;
}
public function getDataCenterIdBits(): int
{
return $this->dataCenterIdBits;
}
public function getWorkerIdBits(): int
{
return $this->workerIdBits;
}
public function getSequenceBits(): int
{
return $this->sequenceBits;
}
}
<?php

declare(strict_types=1);
/**
* This file is part of MineAdmin.
*
* @link https://www.mineadmin.com
* @document https://doc.mineadmin.com
* @contact root@imoi.cn
* @license https://github.com/mineadmin/MineAdmin/blob/master/LICENSE
*/

namespace Mine\Snowflake;

use Hyperf\Snowflake\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
protected int $millisecondBits = 41;

protected int $dataCenterIdBits = 2;

protected int $workerIdBits = 4;

protected int $sequenceBits = 5;

public function maxWorkerId(): int
{
return -1 ^ (-1 << $this->workerIdBits);
}

public function maxDataCenterId(): int
{
return -1 ^ (-1 << $this->dataCenterIdBits);
}

public function maxSequence(): int
{
return -1 ^ (-1 << $this->sequenceBits);
}

public function getTimestampLeftShift(): int
{
return $this->sequenceBits + $this->workerIdBits + $this->dataCenterIdBits;
}

public function getDataCenterIdShift(): int
{
return $this->sequenceBits + $this->workerIdBits;
}

public function getWorkerIdShift(): int
{
return $this->sequenceBits;
}

public function getTimestampBits(): int
{
return $this->millisecondBits;
}

public function getDataCenterIdBits(): int
{
return $this->dataCenterIdBits;
}

public function getWorkerIdBits(): int
{
return $this->workerIdBits;
}

public function getSequenceBits(): int
{
return $this->sequenceBits;
}
}
183 changes: 92 additions & 91 deletions src/mine-core/src/Snowflake/SnowflakeIdGenerator.php
Original file line number Diff line number Diff line change
@@ -1,91 +1,92 @@
<?php

declare(strict_types=1);
/**
* This file is part of MineAdmin.
*
* @link https://www.mineadmin.com
* @document https://doc.mineadmin.com
* @contact root@imoi.cn
* @license https://github.com/mineadmin/MineAdmin/blob/master/LICENSE
*/
/**
* __
* ____ __ __ ____/ /
* /_ / / / / / / __ /
* / /_ / /_/ / / /_/ /
* /___/ \__, / \__,_/
* /____/ 众熠达
*/
namespace Mine\Snowflake;

use Hyperf\Context\ApplicationContext;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Snowflake\ConfigurationInterface;
use Hyperf\Snowflake\IdGeneratorInterface;
use Hyperf\Snowflake\Meta;
use Hyperf\Snowflake\MetaGenerator\RedisMilliSecondMetaGenerator;
use Hyperf\Snowflake\MetaGeneratorInterface;

use function Hyperf\Support\make;

class SnowflakeIdGenerator implements IdGeneratorInterface
{
protected ConfigurationInterface $config;

protected MetaGeneratorInterface $metaGenerator;

public function __construct()
{
$configuration = new Configuration();
$config = ApplicationContext::getContainer()->get(ConfigInterface::class);
$beginSecond = $config->get('snowflake.begin_second', MetaGeneratorInterface::DEFAULT_BEGIN_SECOND);
$this->metaGenerator = make(RedisMilliSecondMetaGenerator::class, [
$configuration,
$beginSecond,
$config,
]);

$this->config = $this->metaGenerator->getConfiguration();
}

public function generate(?Meta $meta = null): int
{
$meta = $this->meta($meta);

$interval = $meta->getTimeInterval() << $this->config->getTimestampLeftShift();
$dataCenterId = $meta->getDataCenterId() << $this->config->getDataCenterIdShift();
$workerId = $meta->getWorkerId() << $this->config->getWorkerIdShift();

return $interval | $dataCenterId | $workerId | $meta->getSequence();
}

public function degenerate(int $id): Meta
{
$interval = $id >> $this->config->getTimestampLeftShift();
$dataCenterId = $id >> $this->config->getDataCenterIdShift();
$workerId = $id >> $this->config->getWorkerIdShift();

return new Meta(
$interval << $this->config->getDataCenterIdBits() ^ $dataCenterId,
$dataCenterId << $this->config->getWorkerIdBits() ^ $workerId,
$workerId << $this->config->getSequenceBits() ^ $id,
$interval + $this->metaGenerator->getBeginTimestamp(),
$this->metaGenerator->getBeginTimestamp()
);
}

public function getMetaGenerator(): MetaGeneratorInterface
{
return $this->metaGenerator;
}

protected function meta(?Meta $meta = null): Meta
{
if (is_null($meta)) {
return $this->metaGenerator->generate();
}

return $meta;
}
}
<?php

declare(strict_types=1);
/**
* This file is part of MineAdmin.
*
* @link https://www.mineadmin.com
* @document https://doc.mineadmin.com
* @contact root@imoi.cn
* @license https://github.com/mineadmin/MineAdmin/blob/master/LICENSE
*/
/**
* __
* ____ __ __ ____/ /
* /_ / / / / / / __ /
* / /_ / /_/ / / /_/ /
* /___/ \__, / \__,_/
* /____/ 众熠达.
*/

namespace Mine\Snowflake;

use Hyperf\Context\ApplicationContext;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Snowflake\ConfigurationInterface;
use Hyperf\Snowflake\IdGeneratorInterface;
use Hyperf\Snowflake\Meta;
use Hyperf\Snowflake\MetaGenerator\RedisMilliSecondMetaGenerator;
use Hyperf\Snowflake\MetaGeneratorInterface;

use function Hyperf\Support\make;

class SnowflakeIdGenerator implements IdGeneratorInterface
{
protected ConfigurationInterface $config;

protected MetaGeneratorInterface $metaGenerator;

public function __construct()
{
$configuration = new Configuration();
$config = ApplicationContext::getContainer()->get(ConfigInterface::class);
$beginSecond = $config->get('snowflake.begin_second', MetaGeneratorInterface::DEFAULT_BEGIN_SECOND);
$this->metaGenerator = make(RedisMilliSecondMetaGenerator::class, [
$configuration,
$beginSecond,
$config,
]);

$this->config = $this->metaGenerator->getConfiguration();
}

public function generate(?Meta $meta = null): int
{
$meta = $this->meta($meta);

$interval = $meta->getTimeInterval() << $this->config->getTimestampLeftShift();
$dataCenterId = $meta->getDataCenterId() << $this->config->getDataCenterIdShift();
$workerId = $meta->getWorkerId() << $this->config->getWorkerIdShift();

return $interval | $dataCenterId | $workerId | $meta->getSequence();
}

public function degenerate(int $id): Meta
{
$interval = $id >> $this->config->getTimestampLeftShift();
$dataCenterId = $id >> $this->config->getDataCenterIdShift();
$workerId = $id >> $this->config->getWorkerIdShift();

return new Meta(
$interval << $this->config->getDataCenterIdBits() ^ $dataCenterId,
$dataCenterId << $this->config->getWorkerIdBits() ^ $workerId,
$workerId << $this->config->getSequenceBits() ^ $id,
$interval + $this->metaGenerator->getBeginTimestamp(),
$this->metaGenerator->getBeginTimestamp()
);
}

public function getMetaGenerator(): MetaGeneratorInterface
{
return $this->metaGenerator;
}

protected function meta(?Meta $meta = null): Meta
{
if (is_null($meta)) {
return $this->metaGenerator->generate();
}

return $meta;
}
}
13 changes: 6 additions & 7 deletions src/mine-helpers/src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Logger\LoggerFactory;
use Hyperf\Redis\Redis;
use Hyperf\Snowflake\IdGeneratorInterface;
use Mine\Helper\AppVerify;
use Mine\Helper\LoginUser;
use Mine\MineCollection;
use Mine\Snowflake\SnowflakeIdGenerator;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface;
use Ramsey\Uuid\Uuid;

if (! function_exists('container')) {
/**
Expand Down Expand Up @@ -167,23 +168,21 @@ function app_verify(string $scene = 'api'): AppVerify

if (! function_exists('snowflake_id')) {
/**
* 生成雪花ID
* @return String
* 生成雪花ID.
*/
function snowflake_id(): string
{
return container()->get(\Mine\Snowflake\SnowflakeIdGenerator::class)->generate();
return container()->get(SnowflakeIdGenerator::class)->generate();
}
}

if (! function_exists('uuid')) {
/**
* 生成UUID
* @return String
* 生成UUID.
*/
function uuid(): string
{
return \Ramsey\Uuid\Uuid::uuid4()->toString();
return Uuid::uuid4()->toString();
}
}

Expand Down