定制 pimbay-php/sequence-contracts 二次开发

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

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

pimbay-php/sequence-contracts

最新稳定版本:v1.0.0

Composer 安装命令:

composer require pimbay-php/sequence-contracts

包简介

Contracts, DTOs and exceptions for the PimBay Sequence Stack. Shared foundation for number and random sequence implementations.

README 文档

README

Latest Version on Packagist PHP Version License Code Coverage

The pimbay-php/sequence-contracts package provides the foundational interfaces, Data Transfer Objects (DTOs), and exceptions for the entire PimBay Sequence Stack. It defines the core contracts for atomic sequence generation and ensures deterministic testing through PSR-20 clock abstraction.

This package is designed for maximum modularity, acting as the bedrock for all other components in the Sequence Stack.

Key Features

  • Atomic Consistency: Focused on preventing duplicates in high-concurrency environments.
  • PSR-20 Clock: Uses psr/clock as the only dependency — compatible with any PSR-20 clock implementation (Symfony, Laravel, or custom).
  • Testing Friendly: Includes an InMemoryNumberSequence for rapid unit testing.
  • Global Standard: Built for PHP 8.3+ with strict typing and PSR-4 compliance.
  • PHPStan Ready: Fully type-hinted for maximum static analysis compatibility (Level Max).

Core Features

  • NumberSequenceInterface (PimBay\SequenceContracts\Number\NumberSequenceInterface): Defines the contract for generating and retrieving number sequences, ensuring atomic integrity.
    • nextNumber(string $group, string $name, int $initialValue = 1, array $metadata = []): int: Performs an atomic increment and returns the new value.
    • getCurrent(string $group, string $name): SequenceResult: Retrieves the last generated value and its metadata without incrementing. Throws SequenceNotFoundException if the sequence does not exist.
  • SequenceResult DTO: A final readonly DTO that encapsulates the state of a sequence, including its last value, metadata, creation, and update timestamps.
  • Psr\Clock\ClockInterface (PSR-20): Used directly as the clock abstraction — no custom wrapper. Any PSR-20 compliant clock implementation can be injected, including MockClock provided by this package.
  • InMemoryNumberSequence: A simple, array-based implementation of NumberSequenceInterface included for easy testing and mocking in consumer projects.
  • MockClock: A test support class that implements ClockInterface, allowing developers to "freeze" or advance time in tests.

Installation

You can install the package via Composer:

composer require pimbay-php/sequence-contracts

Usage

NumberSequenceInterface

This interface defines how to interact with any number sequence implementation:

<?php

declare(strict_types=1);

namespace App;

use PimBay\SequenceContracts\Number\NumberSequenceInterface;
use PimBay\SequenceContracts\Dto\SequenceResult;
use PimBay\SequenceContracts\Exception\SequenceNotFoundException;

class MyService
{
    public function __construct(private readonly NumberSequenceInterface $numberSequence)
    {
    }

    public function generateOrderNumber(string $platform): string
    {
        $group = 'order';
        $name = date('Y');
        $initialValue = 1000;
        $metadata = ['platform' => $platform];

        $nextNumber = $this->numberSequence->nextNumber($group, $name, $initialValue, $metadata);

        return "ORD-{$nextNumber}";
    }

    public function getCurrentInvoiceNumber(): ?SequenceResult
    {
        try {
            return $this->numberSequence->getCurrent('invoice', date('Y'));
        } catch (SequenceNotFoundException $e) {
            return null; // Invoice sequence not yet created
        }
    }
}

MockClock — testing with controlled time

MockClock implements Psr\Clock\ClockInterface and allows freezing or advancing time in tests:

<?php

declare(strict_types=1);

namespace App\Tests;

use PHPUnit\Framework\TestCase;
use PimBay\SequenceContracts\InMemory\InMemoryNumberSequence;
use PimBay\SequenceContracts\Test\Support\MockClock;

final class MyNumberSequenceTest extends TestCase
{
    public function testSequenceCreationTimestamp(): void
    {
        $mockClock = new MockClock(new \DateTimeImmutable('2026-04-25 10:00:00'));
        $sequence = new InMemoryNumberSequence($mockClock);

        $result = $sequence->nextNumber('test', 'first');

        self::assertSame(1, $result);
        self::assertEquals($mockClock->now(), $sequence->getCurrent('test', 'first')->createdAt);

        // Advance time and verify updatedAt changes
        $mockClock->advance('+1 hour');
        $sequence->nextNumber('test', 'first');

        self::assertNotEquals(
            $sequence->getCurrent('test', 'first')->createdAt,
            $sequence->getCurrent('test', 'first')->updatedAt,
        );
    }
}

Test Matrix

This package is rigorously tested across multiple PHP versions to ensure compatibility and reliability.

PHP Version
8.3
8.4
8.5

Contribution

Contributions are welcome. Please ensure that your code adheres to the existing coding standards and all tests pass. Open an issue or submit a pull request for any new features or bug fixes.

License

Public domain — Unlicense

Created by Jan Sarmir · No conditions · No copyright

pimbay-php/sequence-contracts 适用场景与选型建议

pimbay-php/sequence-contracts 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 05 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「interface」 「sequence」 「dto」 「contracts」 「number-sequence」 「random-sequence」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 pimbay-php/sequence-contracts 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 pimbay-php/sequence-contracts 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unlicense
  • 更新时间: 2026-05-11