appkr/timemachine

Laravel/Lumen 应用级别的测试时间设置器

v0.3.0 2017-07-04 06:22 UTC

This package is not auto-updated.

Last update: 2024-09-28 00:05:46 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

Timemachine

任意操作应用(Laravel/Lumen)服务器时间(给定分钟数)。

注意

仅限测试用途。不要将其用于公开可访问的服务。

  • 提供的API没有通过认证保护。
  • 在设置激活期间,它会影响应用程序中所有与时间相关的功能。例如,在数据库表中写入的created_at

1. 安装

将库拉到您的项目中。

composer install appkr/timemachine 

将服务提供者在提供者数组中。

<?php // config/app.php (Laravel)

return [
    'providers' => [
        Appkr\Timemachine\TimemachineServiceProvider::class,
    ],
];

// boostrap/app.php (Lumen)
$app->register(Appkr\Timemachine\TimemachineServiceProvider::class);

可选地发布配置。

php artisan vendor:publish --provider="Appkr\Timemachine\TimemachineServiceProvider"

2. 使用方法

有三个API。为了方便,提供了一个Postman 收集

2.1. GET /timemachine

当给出target_server_time参数时,返回从当前服务器时间的时间差。如果没有给出任何内容,则打印当前服务器时间。

GET /timemachine
Accept: application/json
Content-Type: application/json
HTTP/1.1 200 OK
Content-Type: application/json
Date: Thu, 01 Jun 2017 12:00:00 +0900

{
  "current_server_time": "2017-06-01T12:00:00+0900",
  "target_server_time": "2017-06-01T12:05:00+0900",
  "add_days": 0,
  "add_minutes": 5,
  "sub_days": null,
  "sub_minutes": null
}

2.2. PUT /timemachine

根据给定的时间长度操作服务器时间。

PUT /timemachine
Accept: application/json
Content-Type: application/json

{
    "add_minutes": 5,
    "ttl": 5
}
HTTP/1.1 200 OK
Content-Type: application/json
Date: Thu, 01 Jun 2017 12:00:00 +0900

{
  "current_server_time": "2017-06-01T12:00:00+0900",
  "message": "Success. The settings will be effective from next request on for 5 minutes."
}

2.3. DELETE /timemachine

删除时间设置并恢复到机器时间。

DELETE /timemachine
Accept: application/json
Content-Type: application/json
HTTP/1.1 200 OK
Content-Type: application/json
Date: Thu, 01 Jun 2017 12:00:00 +0900

{
  "current_server_time": "2017-06-01T12:00:00+0900",
  "message": "Success. Settings removed."
}

3. 赞助商

这个库是公司项目的副产品。感谢MeshKorea

MeshKorea

开源版本是用JetBrains赞助的IntelliJ IDEA创建的。JetBrains.

JetBrains

4. 关于HTTP日期响应头的说明

在Nginx中,可以从应用程序端设置Date头。因此,以下是可能的操作

<?php

header('Date: ' . Carbon\Carbon::now()->addMinutes(5)->toRfc2822String());
echo $httpResponseBody;

在Apache中,这是不可行的。https://laracasts.com/discuss/channels/servers/how-can-i-override-http-date-response-header-under-apache24