定制 php-architecture-kit/clock 二次开发

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

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

php-architecture-kit/clock

最新稳定版本:1.0.1

Composer 安装命令:

composer require php-architecture-kit/clock

包简介

Another Clock implementation but if you allow the PHP Architecture library to be used on your domains, then you may prefer `new SystemClock` from that library rather than any other.

README 文档

README

PSR-20 Clock implementations for PHP applications. Provides testable time abstractions for domain-driven design and clean architecture.

Features

  • PSR-20 compliant - Implements Psr\Clock\ClockInterface
  • Testable - FrozenClock for deterministic unit tests
  • Timezone-aware - LocalizedClock for specific timezones
  • Zero dependencies - Only requires psr/clock
  • PHP 7.4+ - Compatible with legacy and modern PHP

Installation

composer require php-architecture-kit/clock

Quick Start

use PhpArchitecture\Clock\SystemClock;
use PhpArchitecture\Clock\FrozenClock;
use PhpArchitecture\Clock\LocalizedClock;

// Production: Use system clock
$clock = new SystemClock();
$now = $clock->now(); // DateTimeImmutable

// Testing: Use frozen clock
$clock = FrozenClock::at(new \DateTimeImmutable('2024-06-15 12:00:00'));
$now = $clock->now(); // Always returns '2024-06-15 12:00:00'

// Timezone-specific: Use localized clock
$clock = new LocalizedClock(new \DateTimeZone('Europe/Warsaw'));
$now = $clock->now(); // DateTimeImmutable in Warsaw timezone

// UTC shortcut
$clock = LocalizedClock::utc();

Clock Implementations

SystemClock

Returns the current system time. Use in production code.

$clock = new SystemClock();
$now = $clock->now(); // Current time

FrozenClock

Returns a fixed time. Ideal for unit testing.

// Freeze at specific time
$clock = FrozenClock::at(new \DateTimeImmutable('2024-01-01 00:00:00'));

// Freeze at current time
$clock = FrozenClock::fromNow();

// Time never changes
$clock->now(); // Always the same
usleep(10000);
$clock->now(); // Still the same

LocalizedClock

Returns current time in a specific timezone.

// Any timezone
$clock = new LocalizedClock(new \DateTimeZone('America/New_York'));

// UTC shortcut
$clock = LocalizedClock::utc();

$now = $clock->now();
echo $now->getTimezone()->getName(); // 'America/New_York' or 'UTC'

Usage in Domain Services

Inject ClockInterface instead of calling new \DateTimeImmutable() directly:

use Psr\Clock\ClockInterface;

class OrderService
{
    public function __construct(
        private ClockInterface $clock
    ) {}

    public function createOrder(array $items): Order
    {
        return new Order(
            items: $items,
            createdAt: $this->clock->now()
        );
    }
}

Production Configuration

// Symfony
services:
    Psr\Clock\ClockInterface:
        class: PhpArchitecture\Clock\SystemClock

// Laravel
$this->app->bind(ClockInterface::class, SystemClock::class);

Testing

class OrderServiceTest extends TestCase
{
    public function testOrderCreatedWithCorrectTimestamp(): void
    {
        $fixedTime = new \DateTimeImmutable('2024-06-15 12:00:00');
        $clock = FrozenClock::at($fixedTime);
        
        $service = new OrderService($clock);
        $order = $service->createOrder(['item1', 'item2']);
        
        $this->assertEquals($fixedTime, $order->getCreatedAt());
    }
}

Comparison

Clock Use Case Time Changes
SystemClock Production Yes
FrozenClock Unit tests No
LocalizedClock Timezone-specific apps Yes

API Reference

SystemClock

Method Description
now(): DateTimeImmutable Returns current system time

FrozenClock

Method Description
__construct(DateTimeImmutable $frozenAt) Create with fixed time
at(DateTimeImmutable $frozenAt): self Factory: create with fixed time
fromNow(): self Factory: freeze current time
now(): DateTimeImmutable Returns the frozen time

LocalizedClock

Method Description
__construct(DateTimeZone $timeZone) Create with timezone
utc(): self Factory: create UTC clock
now(): DateTimeImmutable Returns current time in timezone

Testing

Package is tested with PHPUnit in the php-architecture-kit/workspace project.

License

MIT

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-02-12

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固