ndtan/id-generator-lite
Composer 安装命令:
composer require ndtan/id-generator-lite
包简介
NDT ID Generator — Lite: single-file, dependency-free PHP ID generator (UUID v4/v7, ULID, NanoID, Mongo ObjectId, ShortUUID, KSUID, Snowflake).
关键字:
README 文档
README
Single‑file, dependency‑free PHP library for generating UUID v4/v7, ULID, NanoID, Mongo ObjectId, ShortUUID (Base58), KSUID (Base62), and Snowflake.
Namespace:ndtan· PHP 8.1+ · PSR‑4 autoload
Table of Contents
- Features
- Installation
- Quick Start
- API Reference
- Snowflake Configuration (optional)
- Which ID should I use?
- Notes & Guarantees
- Testing
- Changelog
- License
Features
- ✅ Single file core:
src/ID.php - ✅ No external dependencies (pure PHP)
- ✅ UUID v4 (random), UUID v7 (time‑ordered; RFC 9562)
- ✅ ULID (Crockford Base32) + monotonic variant
- ✅ NanoID (URL‑safe; customizable length/alphabet)
- ✅ Mongo ObjectId (24 hex chars)
- ✅ ShortUUID (UUID → Base58)
- ✅ KSUID (27 chars, Base62, time‑ordered)
- ✅ Snowflake (64‑bit; string return for 32‑bit safety)
- ✅ Works with any framework (Composer autoload) and plain PHP
Installation
composer require ndtan/id-generator-lite
Requires PHP 8.1+ and the
random_bytes()function available by default.
Quick Start
<?php require __DIR__.'/vendor/autoload.php'; use ndtan\ID; $u4 = ID::uuid4(); // 4xxx-... $u7 = ID::uuid7(); // 7xxx-... $ul = ID::ulid(); // 26-char ULID $ulm = ID::ulidMonotonic(); // ULID monotonic (same ms) $na = ID::nanoid(); // default 21 chars, URL-safe $oid = ID::objectId(); // 24 hex (Mongo-like) $suid= ID::shortUuid(); // UUID -> Base58 (short) $ks = ID::ksuid(); // 27 Base62, time-ordered $sf = ID::snowflake(); // 64-bit Snowflake (as string)
API Reference
// UUID ID::uuid4(): string; ID::uuid7(): string; // ULID ID::ulid(): string; ID::ulidMonotonic(): string; // NanoID ID::nanoid(int $size = 21, ?string $alphabet = null): string; // Mongo ObjectId ID::objectId(): string; // 24 lowercase hex // ShortUUID (Base58) ID::shortUuid(?string $uuid = null): string; // if null, uses uuid4() // KSUID (Base62) ID::ksuid(): string; // 27 chars // Snowflake (64-bit layout) ID::configureSnowflake(array $opts): void; // optional, see below ID::snowflake(?array $cfg = null): string; // returns string
Snowflake Configuration (optional)
// global, call once (e.g., at bootstrap) ID::configureSnowflake([ 'epoch' => '2020-01-01T00:00:00Z', // or epoch milliseconds 'worker_id' => 1, // 0..31 'datacenter_id' => 1 // 0..31 ]); // or per-call: $sf = ID::snowflake(['worker_id' => 2]);
Layout (Twitter‑like): timestamp(41) | datacenter(5) | worker(5) | sequence(12)
IDs are returned as string to remain safe on 32‑bit platforms.
Which ID should I use?
| Use case | Recommended |
|---|---|
| Write‑heavy DB, good index locality | UUID v7, ULID, or KSUID |
| Human/URL‑friendly short slugs | NanoID or ShortUUID(uuid7()) |
| Interop with Mongo ecosystem | Mongo ObjectId |
| Need sortable UUID but keep UUID format | UUID v7 (or ULID) |
| Coordinated multi‑node generation | Snowflake (configure worker/datacenter) |
Rule of thumb: pick UUID v7 or ULID for most DB keys; NanoID for slugs; Snowflake when you control worker IDs across instances.
Notes & Guarantees
- Uses
random_bytes()for cryptographic randomness where applicable. - UUID v7, ULID, KSUID are time‑ordered, improving B‑Tree locality.
ulidMonotonic()bumps randomness if multiple IDs are generated within the same millisecond.snowflake()maintains an internal per‑ms sequence; returns string to avoid integer overflow.shortUuid()encodes the UUID bytes using Base58 (no ambiguous characters).
Testing
composer install
composer test
Includes PHPUnit tests & a GitHub Actions workflow.
Changelog
See CHANGELOG.md for release notes.
License
MIT © Tony Nguyen
ndtan/id-generator-lite 适用场景与选型建议
ndtan/id-generator-lite 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 09 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「uuid」 「lite」 「snowflake」 「ulid」 「ObjectId」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ndtan/id-generator-lite 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ndtan/id-generator-lite 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ndtan/id-generator-lite 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Provides quickstart to build Ecotone Lite Application with Event Sourcing
DrUUID RFC 4122 library for PHP
Micro Active Record library in PHP, support chain calls, events, and relations.
Trait to generate uuid when creating models
A Flight PHP framework skeleton app to get your new projects up and running ASAP
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 39
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-09-21