icanhazstring/symfony-time-machine 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

icanhazstring/symfony-time-machine

最新稳定版本:0.1.1

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);
    }
}

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-10-14

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固