bnbwebexpertise/laravel-sequence
Composer 安装命令:
composer require bnbwebexpertise/laravel-sequence
包简介
Laravel package to handle model sequence generation (no gap)
README 文档
README
This package provides helpers to work with no-gap and ordered integer sequences in Laravel models.
It has been built to help the generation of "administrative sequences" where there should be no missing value between records (invoices, etc).
WARNING This is beta version (POC). This is not yet production ready, so you should use at your own risk.
Installation
Add the service provider to your configuration :
'providers' => [ // ... Bnb\Laravel\Sequence\SequenceServiceProvider::class, // ... ],
Configuration
You can customize this package behavior by publishing the configuration file :
php artisan vendor:publish --provider='Bnb\Laravel\Sequence\SequenceServiceProvider'
You can customize values without publishing by specifying those keys in your .env file :
SEQUENCE_START=123 # defaults to 1
SEQUENCE_QUEUE_CONNECTION=database # defaults to default
SEQUENCE_QUEUE_NAME=sequence # defaults to default
SEQUENCE_AUTO_DISPATCH=false # defaults to true
To avoid concurrency issues when generating sequence number, the queue worker number should be set to one. It is recommended to use a dedicated queue (and worker).
Adding a sequence to a model
Sequence columns should be generated with the following configuration in your migration :
$table->unsignedInteger('sequence_name')->nullable()->unique();
To work with sequence you must enhance your model class with the Bnb\Laravel\Sequence\HasSequence trait.
The sequences array property of your model must contain the list of the sequences names :
public $sequences = ['my_sequence'];
Some sequence properties can be overridden by specifying a method in the model class (where MySequence is the name your sequence in PascalCase) :
- sequence start value (per-sequence) with the
getMySequenceStartValue() : int - sequence format (per-sequence) with the
formatMySequenceSequence($next, $last) : int - sequence generation authorization (per-sequence) with the
canGenerateMySequenceSequence() : bool - sequence gap filling mode (per-sequence)
isMySequenceGapFilling() : bool - sequence generation queue connection (for the model class)
getSequenceConnection() : string - sequence generation queue name (for the model class)
getSequenceQueue() : string - sequence auto-dispatch activation (for the model class)
isSequenceAutoDispatch() : bool
Example :
use Bnb\Laravel\Sequence\HasSequence;
use Illuminate\Database\Eloquent\Model;
/**
* MyModel model uses a sequence named
*/
class MyModel extends Model
{
use HasSequence;
const SEQ_INVOICE_NUMBER_START = 0;
public $timestamps = false;
protected $fillable = ['active'];
protected $sequences = ['invoice_number'];
/**
* Assume the sequence can only be generated if active is true
*/
protected function canGenerateReadOnlySequence()
{
return $this->active;
}
/**
* The sequence default value must match the format
*/
protected function getInvoiceNumberStartValue()
{
return sprintf('%1$04d%2$06d', date('Y'), static::SEQ_INVOICE_NUMBER_START);
}
/**
* Format the sequence number with current Year that resets its counter to 0 on year change
*/
protected function formatInvoiceNumberSequence($next, $last)
{
$newYear = date('Y');
$newCounter = substr($next, 4);
if ($last) {
$lastYear = substr($last, 0, 4);
if ($lastYear < $newYear) {
$newCounter = static::SEQ_INVOICE_NUMBER_START;
}
}
return sprintf('%1$04d%2$06d', $newYear, $newCounter);
}
/**
* If `true`, the first number available is used, ie. the lowest number available, greater or equal to start number, including gaps caused by deletion (soft-delete included)
* If `false` (default value), the next number will always be the last number used + 1 or the first number of the sequence when nothing pre-exists
*/
protected function isSequenceGapFilling()
{
return false;
}
}
Sequence generation event
When a sequence number is generated a model event is thrown for running custom tasks.
You can listen to the sequence event using the sequenceGenerated($sequenceName, $callback) method (in a service provider boot method for example) :
MyModel::sequenceGenerated('invoice_number', function ($model) {
Mail::to($model->recipient_email)->send(new InvoiceGenerated($model));
});
Schedule generation
You can schedule inside your console kernel the Bnb\Laravel\Sequence\Console\Commands\UpdateSequence at the required frequency to generate the missing sequence numbers asynchronously.
protected function schedule(Schedule $schedule)
{
$schedule->command('sequence:update')
->hourly();
}
This may not be required when using auto dispatch mode but can be used as a security fallback.
bnbwebexpertise/laravel-sequence 适用场景与选型建议
bnbwebexpertise/laravel-sequence 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11.06k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2017 年 03 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 bnbwebexpertise/laravel-sequence 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bnbwebexpertise/laravel-sequence 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 11.06k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-03-02