icanhazstring/symfony-time-machine
Composer 安装命令:
composer require icanhazstring/symfony-time-machine
包简介
Symfony bundle that adds the capability to change the datetime of an application
关键字:
README 文档
README
Symfony bundle that adds the capability to change the datetime of an application
Motivation
Writing tests for your application is somewhat nice and easy. But what about
testing your application that heavily is based upon DateTime objects?
There is symfony/clock which already provides a ClockInterface which you can
use to inject a NativeClock or MockClock into your services.
But that only works for tests. What if you want to test end-to-end?
You could change the system time of you server, true, but there is an easier way using
this symfony-time-machine.
Installation
composer require icanhazstring/symfony-time-machine
If the autoregister doesn't work, add the bundle into your config/bundles.php.
<?php return [ Icanhazstring\SymfonyTimeMachine\TimeMachineBundle => ['all' => true], ];
How it works
This bundle relies on the presence of ClockInterface as a service in your application.
Therefor you need to have something like this in your services.yaml:
services: Symfony\Component\Clock\ClockInterface: class: Symfony\Component\Clock\NativeClock
This bundle will set this service to public: true as this is needed to change the
service on request.
Now everytime you boot the TimeKernel, it will replace the ClockInterface with a MockClock
with a parsed datetime string from either a request query/cookie time-machine or environment
variable TIME_MACHINE.
Usage
Web usage
For web usage you have to alter your public/index.php a little.
<?php use App\Kernel; use Icanhazstring\SymfonyTimeMachine\TimeKernel; require_once dirname(__DIR__).'/vendor/autoload_runtime.php'; return function (array $context) { $kernel = new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']); (new TimeKernel($kernel))->fromContext($context)->boot(); return $kerneL; };
Console usage
For console usage alter the bin/console.
return function (array $context) { $kernel = new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']); (new TimeKernel($kernel))->fromContext($context)->boot(); return new Application($kernel); };
Register additional TimeMachineHandler
Sometimes you need to alter some other services in your application along with the time-machine.
For this you can implement services implementing the TimeMachineHandler interface.
These will be called right after the ClockInterface was changed in the container.
The TimeKernel will use the HandlerRegistry to call TimeMachineHandler::handle() on all
available services.
For example, you could add the time-machine query parameter to all openapi paths
in your application, as shown here using api-platform:
final class PathModifier implements OpenApiPathModifier, TimeMachineHandler { public function __construct(private readonly OpenApiFactory $openApiFactory) { } public function handles(string $path, PathItem $pathItem): bool { return true; } public function modify(PathItem $pathItem): PathItem { /** @var array<string, null|Operation> $operations */ $operations = [ 'get' => $pathItem->getGet(), 'post' => $pathItem->getPost(), ]; foreach ($operations as $method => $operation) { if ($operation === null) { continue; } $queryParameters = $operation->getParameters(); $queryParameters[] = new Parameter( name: 'time-machine', in: 'query', description: 'Sets the applications datetime to a specific value.', required: false, schema: ['type' => 'string', 'default' => 'now'] ); $wither = 'with'.ucfirst($method); /** @var PathItem $pathItem */ $pathItem = $pathItem->{$wither}($operation->withParameters($queryParameters)); } return $pathItem; } public function handle(): void { $this->openApiFactory->addModifier($this); } }
icanhazstring/symfony-time-machine 适用场景与选型建议
icanhazstring/symfony-time-machine 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.53k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2022 年 10 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「time-machine」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 icanhazstring/symfony-time-machine 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 icanhazstring/symfony-time-machine 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 icanhazstring/symfony-time-machine 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The bundle for easy using json-rpc api on your project
Bundle Symfony DaplosBundle
Biblioteca PHP pura para pagamentos SISP/Vinti4 de Cabo Verde.
Open-source place library (OpenStreetMap point/polygon places) + Symfony UX Map integration: clustered, thumbnail-on-zoom item markers
Official BugBoard SDK for PHP — report errors as cards on your project board, with first-class Laravel and Symfony support.
A maintained, security-focused continuation of KCFinder for modern production environments
统计信息
- 总下载量: 1.53k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-10-14