comsolit/clock-bundle
Composer 安装命令:
composer require comsolit/clock-bundle
包简介
Clock Service to represent a request-constant, system-wide instance in time.
关键字:
README 文档
README
Clock Bundle for Symfony
Unit-Testing anything related to time is hard if the code under test directly accesses the system time. Therefor this bundle provides a clock service wrapping a DateTime instance that represents "now".
Another nice property is, that "now" remains the same during one request.
Usage Examples
The clock also contains some helper functions that help to create easily understandable code related to time.
Test, whether a user may try to login again
After some failures to login the user from a given IP address may not login for some minutes:
$clock->hasElapsed($blockingPeriod, $lastFailedLogin->getDateTime())
Without the helper methods this would look like:
((int)$lastFailedLogin->getDateTime()->format('U')) + $blockingPeriod >= $clock->getTimestamp()
Test, whether a token is still valid
$clock->isExpired($tokenExpirationTime)
Save a file with time as part of the filename
$fileName = $clock->getFileName() . '_datadump'; // e.g. $fileName === '2015-03-25_23-12-59_datadump'
Helper methods in class Clock
class Clock { // SNIPP some stuff /** * Time representation usable as (part of) a file name, e.g.: 2014-05-23_13-45-23 * @return string */ public function getFileName() { return $this->now->format('Y-m-d_H-i-s'); } /** * @return \DateTime */ public function getDateTime() { return $this->now; } /** * @return int */ public function getSeconds() { return (int)$this->now->format('U'); } /** * @return int */ public function getSecondsSince(\DateTimeInterface $past) { return $this->getSeconds() - (int)$past->format('U'); } /** * @return int */ public function getSecondsUntil(\DateTimeInterface $future) { return - $this->getSecondsSince($future); } /** * @return bool * @param int $seconds */ public function hasElapsed($seconds, \DateTimeInterface $since) { return $this->getSecondsSince($since) > $seconds; } /** * @return bool */ public function isExpired(\DateTimeInterface $expiryDate) { return (int)$this->now->format('U') > (int)$expiryDate->format('U'); } /** * Create new DateTime object with the timezone of this clock * * @param String $time */ public function createDateTime($time) { return (new \DateTime($time, $this->now->getTimezone()))->setTimezone($this->now->getTimezone()); } /** * Returns an instance of DateTimeImmutable for the given implementation of DateTimeInterface. * * This method should rather exist in the DateTimeImmutable class but Derick Rethans doesn't * think so. * * @param \DateTimeInterface $datetime * @return \DateTimeImmutable */ public static function createDateTimeImmutable(\DateTimeInterface $datetime) { if ($datetime instanceof \DateTimeImmutable) { return $datetime; } return \DateTimeImmutable::createFromMutable($datetime); } }
comsolit/clock-bundle 适用场景与选型建议
comsolit/clock-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 463 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 02 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「clock bundle」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 comsolit/clock-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 comsolit/clock-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 comsolit/clock-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Time provider, providing consistent current date, time, datetime and timezone across the request.
2lenet/EasyAdminPlusBundle
The bundle for easy using json-rpc api on your project
The smallest PSR-20 implementation (PHP 7.0 and later)
PSR-20 Implementation
A Laravel Nova card.
统计信息
- 总下载量: 463
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-02-23