castor/identifier
Composer 安装命令:
pie install castor/identifier
包简介
High-performance 128-bit identifier extension for PHP (UUIDs and ULIDs)
README 文档
README
A high-performance PHP extension for working with 128-bit identifiers including UUIDs and ULIDs.
Features
- 128-bit Base Class:
Identifier\Bit128for all 128-bit identifiers - Complete UUID Support: All UUID versions (1, 3, 4, 5, 6, 7) with proper RFC compliance
- ULID Support: Universally Unique Lexicographically Sortable Identifiers with monotonic ordering
- Thread Safety: Full thread safety for ULID monotonic generation using TSRM (Thread Safe Resource Manager)
- State System: Deterministic generation for testing with
State\Fixed - Exceptional Performance: Native C implementation delivering 9.9M+ ULID ops/sec, 2.8M+ UUID ops/sec
- Type Safety: Proper PHP class hierarchy with inheritance
Installation
Requirements
- PHP 8.3 or higher
- Zig 0.15.2+ (Download from ziglang.org) - The amazing build system that makes this all possible
If you use devenv, the repository includes a ready-to-use environment with PHP, PHP development headers (php-config), Composer, Zig, and ZLS:
devenv shell
Inside the devenv shell, you can use the provided helper scripts:
build # zig build test # zig build + PHPUnit dev # zig build dev bench # zig build + composer install + composer bench
Build and Install
git clone https://github.com/your-org/php-ext-identifier.git cd php-ext-identifier zig build dev # Build + test in one command
For production installation:
zig build install-system # Install to system PHP (requires sudo)
Enable Extension
Add to your php.ini:
extension=identifier
Quick Start
use Identifier\Uuid\Version4; use Identifier\Uuid\Version7; use Identifier\Ulid; // Generate random UUID v4 $uuid = Version4::generate(); echo $uuid->toString(); // e.g., "550e8400-e29b-41d4-a716-446655440000" // Generate timestamp-based UUID v7 $uuid7 = Version7::generate(); echo $uuid7->toString(); // Generate ULID $ulid = Ulid::generate(); echo $ulid->toString(); // e.g., "01ARZ3NDEKTSV4RRFFQ69G5FAV"
Testing with Fixed State
use Identifier\State\Fixed; use Identifier\Uuid\Version4; // Create deterministic state for testing $ctx = Fixed::create(1640995200000, 12345); // Generate deterministic UUIDs $uuid1 = Version4::generate($ctx); $uuid7 = Version7::generate($ctx); $ulid = Ulid::generate($ctx); // Both calls with same state will produce same results
Thread Safety
This extension is fully thread-safe for ULID monotonic generation in multi-threaded PHP environments (ZTS builds). The implementation uses PHP's TSRM (Thread Safe Resource Manager) to ensure proper thread isolation.
How Thread Safety Works
- Thread Isolation: Each thread maintains its own monotonic state (last timestamp and randomness)
- Zero Contention: No locks or synchronization required - each thread operates independently
- Monotonic Ordering: ULIDs generated within a single thread are guaranteed to be monotonically increasing
- Performance: Thread safety comes with zero performance overhead
Compatibility
- ✅ Apache mod_php (both threaded and non-threaded)
- ✅ Apache mod_worker (requires ZTS PHP build)
- ✅ Windows IIS (requires ZTS PHP build)
- ✅ PHP-FPM (process-based, inherently thread-safe)
- ✅ CLI (single-threaded by default)
Build Considerations
The same extension binary works for both ZTS (thread-safe) and non-ZTS PHP builds:
# Check if your PHP build supports threading php -m | grep -i zts php-config --configure-options | grep -i zts
For web servers that use threading (like Apache mod_worker or IIS), ensure you're using a ZTS PHP build to get full thread safety benefits.
API Documentation
Check the stub file for a detailed API documentation.
Testing
Tests are written with PHPUnit under test/Test and are run against the locally built extension.
zig build test # Build the extension and run PHPUnit zig build dev # Build + test composer test # Run PHPUnit after the extension has been built
With devenv:
devenv shell test # Build and run PHPUnit inside the managed environment devenv test # Validate the environment and run the test suite
Benchmarks
Benchmarks use PHPBench and compare this extension against symfony/uid and ramsey/uuid.
zig build composer install composer bench
With devenv:
devenv shell bench
Contributing
- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Ensure all tests pass (
zig build test) - Submit a pull request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- Zig Programming Language - This project is built using Zig's incredible embedded C compiler and build system. Zig's seamless C interop, cross-compilation capabilities, and modern build tooling make it the perfect choice for PHP extension development. Special thanks to the Zig team for creating such an outstanding development experience.
- devenv - Provides a reproducible development environment for PHP extension work, including PHP development tooling, Composer, Zig, and ZLS.
- RFC 4122 - UUID specification
- RFC 9562 - Updated UUID specification
- ULID Specification - ULID format specification
castor/identifier 适用场景与选型建议
castor/identifier 是一款 基于 C 开发的 Composer 扩展包,目前已累计 5 次下载、GitHub Stars 达 6, 最近一次更新时间为 2025 年 11 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「uuid」 「identifier」 「guid」 「rfc4122」 「ulid」 「php-extension」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 castor/identifier 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 castor/identifier 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 castor/identifier 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Paramconverter, Normalizer and Form Type for Ramsey Uuid
A PHP library for generating and working with identifiers, including UUIDs, ULIDs, and Snowflakes
DrUUID RFC 4122 library for PHP
Trait to generate uuid when creating models
Provides Doctrine types for mediagone/small-uid package.
Simply create Eloquent Models & database tables with UUID/GUID primary keys.
统计信息
- 总下载量: 5
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 31
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-11-16