From 123ad13089674a3842c46abcc2a296548c76baee Mon Sep 17 00:00:00 2001 From: Death-Satan <2771717608@qq.com> Date: Fri, 26 Jan 2024 12:00:04 +0800 Subject: [PATCH] code style --- src/mine-core/src/Aspect/SaveAspect.php | 6 +- src/mine-core/src/Snowflake/Configuration.php | 152 +++++++-------- .../src/Snowflake/SnowflakeIdGenerator.php | 183 +++++++++--------- src/mine-helpers/src/functions.php | 13 +- 4 files changed, 177 insertions(+), 177 deletions(-) diff --git a/src/mine-core/src/Aspect/SaveAspect.php b/src/mine-core/src/Aspect/SaveAspect.php index c7df2cd4..5ec23686 100644 --- a/src/mine-core/src/Aspect/SaveAspect.php +++ b/src/mine-core/src/Aspect/SaveAspect.php @@ -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()); } diff --git a/src/mine-core/src/Snowflake/Configuration.php b/src/mine-core/src/Snowflake/Configuration.php index f7851d9b..b4a649fb 100644 --- a/src/mine-core/src/Snowflake/Configuration.php +++ b/src/mine-core/src/Snowflake/Configuration.php @@ -1,76 +1,76 @@ -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; - } -} \ No newline at end of file +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; + } +} diff --git a/src/mine-core/src/Snowflake/SnowflakeIdGenerator.php b/src/mine-core/src/Snowflake/SnowflakeIdGenerator.php index 1cc39cf7..37178771 100644 --- a/src/mine-core/src/Snowflake/SnowflakeIdGenerator.php +++ b/src/mine-core/src/Snowflake/SnowflakeIdGenerator.php @@ -1,91 +1,92 @@ -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; - } -} +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; + } +} diff --git a/src/mine-helpers/src/functions.php b/src/mine-helpers/src/functions.php index e3ae0a0b..007c62d4 100644 --- a/src/mine-helpers/src/functions.php +++ b/src/mine-helpers/src/functions.php @@ -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')) { /** @@ -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(); } }