定制 lendable/clock 二次开发

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

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

lendable/clock

Composer 安装命令:

composer require lendable/clock

包简介

Clock Abstraction

README 文档

README

Latest Stable Version License

The Lendable Clock library provides an object-oriented interface for accessing the system time in PHP. While PHP offers direct instantiation of \DateTime, and \DateTimeImmutable to obtain the current system time, this library introduces the concept of a Clock to offer greater control and flexibility over time-related operations.

Why Use a Clock?

You might wonder why you need a clock when you can simply instantiate \DateTime objects whenever you need them. Here's why a Clock abstraction is beneficial:

  • Control Over Time: By depending on a Clock rather than instantiating time objects directly, you gain the ability to reason about and control time within your application.

  • Testing Flexibility: Using a Clock allows you to swap underlying implementations, making it easier to test time-dependent code. You can stub time with fixed values, simulate time passing, and observe interactions with the Clock for more robust testing.

  • Dependency Management: Clear dependencies on the Clock class help in managing components that rely on accessing the current system time.

  • PSR-20 Compatibility: The library aligns with PSR-20, offering interoperability with other libraries and frameworks.

Installation

You can install the Lendable Clock library via Composer.

composer require lendable/clock

Clock Types

The library provides several types of Clocks to suit different use cases:

SystemClock

  • Target: Runtime
  • Description: Delegates to PHP for the current system time, using a fixed timezone at construction.

FixedClock

  • Target: Unit/Functional Tests
  • Description: Always provides a specific timestamp provided at construction, facilitating deterministic testing.
$clock = new FixedClock(new \DateTimeImmutable('2024-03-01 14:19:41'));

echo $clock->now()->format('Y-m-d H:i:s'), "\n";
sleep(5);
echo $clock->now()->format('Y-m-d H:i:s'), "\n";
2024-03-01 14:19:41
2024-03-01 14:19:41

TickingMockClock

  • Target: Unit/Functional Tests
  • Description: Mocks time starting from a given timestamp and simulates time progressing from that point. Useful for testing time-dependent functionality.
$clock = TickingMockClock::tickingFromCurrentTime(new \DateTimeImmutable('2024-03-01 14:19:41'));

echo $clock->now()->format('Y-m-d H:i:s.u'), "\n";
sleep(5);
echo $clock->now()->format('Y-m-d H:i:s.u'), "\n";
2024-03-01 14:19:41.000006
2024-03-01 14:19:46.005175

PersistedFixedClock

  • Target: Functional Tests (e.g., Behat vs. Symfony Kernel)
  • Description: Similar to FixedClock, but can persist and load the given timestamp from disk. Ideal for scenarios where you need to reload your context during testing.

Use PersistedFixedClock::initializeWith(...) to set up the timestamp and PersistedFixedClock::fromPersisted(...) to load from the persisted value on disk.

By leveraging these Clock types, you can enhance the reliability, testability, and maintainability of your time-dependent PHP applications.

$clock = PersistedFixedClock::initializeWith(
    __DIR__,
    new FixedFileNameGenerator('time.json'),
    new \DateTimeImmutable('2024-03-01 14:19:41'),
);

echo $clock->now()->format('Y-m-d H:i:s.u'), "\n";

sleep(5);

$clock = PersistedFixedClock::fromPersisted(__DIR__, new FixedFileNameGenerator('time.json'));

echo $clock->now()->format('Y-m-d H:i:s.u'), "\n";
2024-03-01 14:19:41.000000
2024-03-01 14:19:41.000000

lendable/clock 适用场景与选型建议

lendable/clock 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 281.07k 次下载、GitHub Stars 达 15, 最近一次更新时间为 2021 年 06 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 lendable/clock 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 281.07k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 17
  • 点击次数: 22
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 15
  • Watchers: 33
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-06-24