定制 ndtan/id-generator-lite 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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

PHP License Type

Table of Contents

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 ndtan/id-generator-lite 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 39
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-09-21