Skip to content

Commit

Permalink
初始提交
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric committed Aug 9, 2022
1 parent b06e969 commit e5c941a
Show file tree
Hide file tree
Showing 19 changed files with 461 additions and 325 deletions.
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
.idea
/vendor/
/.git/
.DS_Store
/vendor
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 kra8

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
121 changes: 109 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,117 @@
# laravel-snowflake
# Laravel Snowflake

### 安装
这个 Laravel 包生成 64 位标识符,就像 Twitter 中的雪花一样。

composer require ufucms/laravel-snowflake:"dev-master"
# Laravel 安装
```
composer require "ufucms/laravel-snowflake"
php artisan vendor:publish --provider="Ufucms\Snowflake\Providers\LaravelServiceProvider"
```

### 生成配置
# Lumen 安装
- Install via composer
```
composer require "ufucms/laravel-snowflake"
```

php artisan vendor:publish --provider="Ufucms\LaravelSnowflake\Providers\SnowflakeServiceProvider"
- Bootstrap file changes
将以下代码段添加到 providers 部分下的 bootstrap/app.php 文件中,如下所示:
``` php
// Add this line
$app->register(Ufucms\Snowflake\Providers\LumenServiceProvider::class);
```

# Usage
Get instance
``` php
use Ufucms\Snowflake\Snowflake;

### 引入
$snowflake = new Snowflake();
```
or
``` php
$snowflake = $this->app->make('Ufucms\Snowflake\Snowflake');
```
or
``` php
$snowflake = app('Ufucms\Snowflake\Snowflake');
```

use Ufucms\LaravelSnowflake\Snowflake;

$sn = new Snowflake();
$id = $sn->nextId();


Generate snowflake identifier
```
$id = $snowflake->nextId();
```
or
```
$id = $snowflake->id();
```
or
```
$id = Snowflake::nextId();
```
# Usage with Eloquent
`Ufucms\Snowflake\HasSnowflakePrimary` 特征添加到您的 Eloquent 模型中。
此特征使主键类型为`snowflake`。 Trait 会自动将 $incrementing 属性设置为 false。

``` php
<?php
namespace App;

use Ufucms\Snowflake\HasSnowflakePrimary;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
use HasSnowflakePrimary, Notifiable;
}
```

Column type `id` is supported.

``` php
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
```

# JavaScript support

由于 JavaScript 无法处理 64 位整数,因此还有 HasShortPrimary,它为 JavaScript 可以处理的 53 位整数创建 ID。

``` php
<?php
namespace App;

use Ufucms\Snowflake\HasShortPrimary;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
use HasShortPrimary, Notifiable;
}
```

# Configuration
If `config/snowflake.php` not exist, run below:
```
php artisan vendor:publish
```

# Licence
[MIT licence](https://github.com/ufucms/laravel-snowflake/blob/master/LICENSE)
34 changes: 20 additions & 14 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
{
"name": "ufucms/laravel-snowflake",
"description": "分布式Id生成,基于雪花算法的laravel包",
"description": "分布式唯一ID,基于雪花算法。",
"keywords": [
"php",
"snowflake",
"Laravel",
"Lumen"
],
"authors": [
{
"name": "ufucms",
"email": "ufucms@ufucms.com"
}
],
"homepage": "https://github.com/ufucms/laravel-snowflake",
"license": "MIT",
"authors": [{
"name": "ufucms",
"email": "ufucms@ufucms.com"
}],
"minimum-stability": "dev",
"require": {
"php": ">=7.0.0"
"php": "^8.0|^8.1",
"illuminate/support": "^6.3|^7.0|^8.0|^9.0"
},
"autoload": {
"psr-4": {
"Ufucms\\LaravelSnowflake\\": "src/"
"Ufucms\\Snowflake\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"Ufucms\\LaravelSnowflake\\Providers\\SnowflakeServiceProvider"
],
"aliases": {
"Snowflake": "Ufucms\\LaravelSnowflake\\Facades\\Snowflake"
}
"Ufucms\\Snowflake\\Providers\\LaravelServiceProvider"
]
}
}
}
}
32 changes: 32 additions & 0 deletions config/snowflake.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
return [
/*
|--------------------------------------------------------------------------
| Snowflake Epoch
|--------------------------------------------------------------------------
|
| 在这里您可以配置雪花算法的开始时间。 设置时间
| 该应用程序已开始开发。 不要设定未来的时间。
| 如果服务已经运行,请不要更改时间。
|
| 参考设置: Y-m-d H:i:s
|
*/
'epoch' => env('SNOWFLAKE_EPOCH', '2022-08-08 08:08:08'),

/*
|--------------------------------------------------------------------------
| Snowflake Configuration
|--------------------------------------------------------------------------
|
| 在这里您可以配置雪花的日志设置。
| 如果您使用多个服务器,请分配唯一的
| 雪花算法的ID(1-31).
|
| 参考设置: 1-31
|
*/
'worker_id' => env('SNOWFLAKE_WORKER_ID', 1),

'datacenter_id' => env('SNOWFLAKE_DATACENTER_ID', 1),
];
13 changes: 0 additions & 13 deletions src/Facades/Snowflake.php

This file was deleted.

20 changes: 20 additions & 0 deletions src/HasShortflakePrimary.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php declare(strict_types=1);

namespace Ufucms\Snowflake;

use Ufucms\Snowflake\Snowflake;

trait HasShortflakePrimary
{
public static function bootHasShortflakePrimary()
{
static::saving(function ($model) {
if (is_null($model->getKey())) {
$model->setIncrementing(false);
$keyName = $model->getKeyName();
$id = app(Snowflake::class)->short();
$model->setAttribute($keyName, $id);
}
});
}
}
19 changes: 19 additions & 0 deletions src/HasSnowflakePrimary.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
namespace Ufucms\Snowflake;

use Ufucms\Snowflake\Snowflake;

trait HasSnowflakePrimary
{
public static function bootHasSnowflakePrimary()
{
static::saving(function ($model) {
if (is_null($model->getKey())) {
$model->setIncrementing(false);
$keyName = $model->getKeyName();
$id = app(Snowflake::class)->nextId();
$model->setAttribute($keyName, $id);
}
});
}
}
32 changes: 32 additions & 0 deletions src/Providers/AbstractServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Ufucms\Snowflake\Providers;

use Illuminate\Support\ServiceProvider;
use Ufucms\Snowflake\Snowflake;

abstract class AbstractServiceProvider extends ServiceProvider
{
/**
* Boot the service provider.
*
* @return void
*/
abstract public function boot();

/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->singleton(Snowflake::class, function () {
$epoch = config('snowflake.epoch', null);
$workerId = config('snowflake.worker_id', 1);
$datacenterId = config('snowflake.datacenter_id', 1);
$timestamp = $epoch === null ? null : $timestamp = strtotime($epoch);
return new Snowflake($timestamp, $workerId, $datacenterId);
});
}
}
20 changes: 20 additions & 0 deletions src/Providers/LaravelServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Ufucms\Snowflake\Providers;

use Ufucms\Snowflake\Snowflake;

class LaravelServiceProvider extends AbstractServiceProvider
{
/**
* Bootstrap any application services for laravel.
*
* @return void
*/
public function boot()
{
$this->publishes([
__DIR__ . '/../../config/snowflake.php' => config_path('snowflake.php'),
]);
}
}
18 changes: 18 additions & 0 deletions src/Providers/LumenServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Ufucms\Snowflake\Providers;

class LumenServiceProvider extends AbstractServiceProvider
{
/**
* Bootstrap any application services for lumen.
*
* @return void
*/
public function boot()
{
$path = realpath(__DIR__.'/../../config/snowflake.php');

$this->mergeConfigFrom($path, 'snowflake');
}
}
Loading

0 comments on commit e5c941a

Please sign in to comment.