tomschlick/request-migrations

HTTP 请求迁移

v0.5.0 2019-05-09 19:03 UTC

This package is auto-updated.

Last update: 2024-08-29 04:20:04 UTC


README

Latest Version on Packagist Build Status Total Downloads StyleCI

此包基于Stripe使用的API版本化方案。用户传递版本头,您会自动将请求和响应数据迁移到与您代码当前版本匹配。

安装

您可以通过composer安装此包

通过Composer安装

composer require tomschlick/request-migrations

服务提供者 & Facade

此包支持Laravel 5.5自动加载,因此服务提供者和facade将自动加载。

如果您使用的是Laravel的早期版本或已禁用自动加载,您需要将服务提供者和facade添加到config/app.php中。

'providers' => [
    \TomSchlick\RequestMigrations\RequestMigrationsServiceProvider.php,
]
'aliases' => [
    'RequestMigrations' => \TomSchlick\RequestMigrations\Facades\RequestMigrations::class,
]

中间件

将中间件添加到您的Http Kernel app/Http/Kernel.php

protected $middleware = [
	\TomSchlick\RequestMigrations\RequestMigrationsMiddleware::class,
];

配置

运行以下Artisan命令以将包配置发布到config/request-migrations.php

php artisan vendor:publish --provider="TomSchlick\RequestMigrations\RequestMigrationsServiceProvider"

使用方法

创建迁移

您可以使用Artisan CLI生成新的请求迁移。

php artisan make:request-migration ExampleMigration

该命令将生成请求迁移并将其发布到App/Http/Migrations/*

它将生成迁移,您可以像这样修改它

class GroupNameMigration extends RequestMigration
{
    /**
     * Migrate the request for the application to "read".
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return \Illuminate\Http\Request
     */
    public function migrateRequest(Request $request) : Request
    {
        return $request;
    }

    /**
     * Migrate the response to display to the client.
     *
     * @param \Symfony\Component\HttpFoundation\Response $response
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function migrateResponse(Response $response) : Response
    {
        $content = json_decode($response->getContent(), true);

        $content['firstname'] = array_get($content, 'name.firstname');
        $content['lastname'] = array_get($content, 'name.lastname');
        unset($content['name']);

        return $response->setContent(json_encode($content));
    }

    /**
     * Define which named paths should this migration modify.
     *
     * @return array
     */
    public function paths() : array
    {
        return [
            'users/show',
        ];
    }
}

覆盖版本

use TomSchlick\RequestMigrations\Facades\RequestMigrations;

// set both response & request versions
RequestMigrations::setVersion('2017-01-01')

// set the request version
RequestMigrations::setRequestVersion('2017-01-01')

// set the response version
RequestMigrations::setResponseVersion('2017-01-01')

如果您将版本锁定到特定用户,这将很有用。

RequestMigrations::setVersion(auth()->user()->api_version);

变更日志

请参阅CHANGELOG以获取更多最近更改的信息。

测试

composer test

贡献

请参阅CONTRIBUTING以获取详细信息。

安全

如果您发现任何安全问题,请通过tom@schlick.email发送电子邮件,而不是使用问题跟踪器。

许可

MIT许可(MIT)。请参阅许可文件以获取更多信息。