castor/identifier 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

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\Bit128 for 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

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

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for new functionality
  4. Ensure all tests pass (zig build test)
  5. 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 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 6
  • Watchers: 0
  • Forks: 0
  • 开发语言: C

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-16