• Stars
    star
    185
  • Rank 207,621 (Top 5 %)
  • Language
    PHP
  • License
    MIT License
  • Created about 7 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

HTTP Request Migrations for API Versioning like Stripe

HTTP Request Migrations

Latest Version on Packagist Build Status Total Downloads StyleCI

This package is based on the API versioning scheme used at Stripe. Users pass a version header and you automatically migrate the request & response data to match the current version of your code.

Installation

You can install the package via composer:

Installation via Composer

composer require tomschlick/request-migrations

Service Provider & Facade

This package supports Laravel 5.5 autoloading so the service provider and facade will be loaded automatically.

If you are using an earlier version of Laravel or have autoloading disabled you need to add the service provider and facade to config/app.php.

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

Middleware

Add the middleware to your Http Kernel app/Http/Kernel.php.

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

Configuration

Run the following Artisan command to publish the package configuration to config/request-migrations.php.

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

Usage

Creating a Migration

You can generate a new request migration using the Artisan CLI.

php artisan make:request-migration ExampleMigration

The command will generate a request migration and publish it to App/Http/Migrations/*.

It will generate a migration, you can modify it like this:

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',
        ];
    }
}

Override the Versions

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')

This can be useful if you are pinning the version to a user.

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

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

composer test

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

License

The MIT License (MIT). Please see License File for more information.

More Repositories

1

laravel-http2-server-push

A middleware package for Laravel to enable server push for your script, style, and image assets.
PHP
167
star
2

memcached-library

A CodeIgniter Library to Interface with the Memcached cache system
PHP
130
star
3

townhouse

[WORK IN PROGRESS] A Multitenancy package for Laravel that keeps each tenant in a separate database.
PHP
112
star
4

laravel-micro-manager

Oversee jobs you have put into the queue. Track status, completion % and other job information in real time.
PHP
72
star
5

laravel-linkable

URL binding for Laravel models
PHP
25
star
6

fuel-s3

FuelPHP package for the Amazon S3 PHP Class
PHP
23
star
7

fuel-postmark

A driver enabling the postmark email delivery service to be used in conjunction with Fuel
PHP
21
star
8

codeigniter-mongo-logs

Changes the default CodeIgniter Log Library to write to a MongoDB collection instead of flat log files.
PHP
20
star
9

fuel-jobqueue

A job queue & processor for Fuelphp
PHP
12
star
10

fuel-wrench

A simple maintenance package for FuelPHP allowing you to take a site online or offline.
PHP
11
star
11

codeigniter-forrst

A wrapper for the Forrst API using CodeIgniter / PHP
PHP
11
star
12

homebrew-envoy

A homebrew formula to install Laravel's Envoy
Ruby
9
star
13

mozenda-api

Mozenda API Codeigniter Library
PHP
8
star
14

fuel-mongo-crud

A MongoDB CRUD for FuelPHP
6
star
15

fuel-notifications

A notifications system for FuelPHP
PHP
6
star
16

fuel-deploy

Deploy your fuel app in a single command.
PHP
4
star
17

fuel-menu

A set of classes to create nested menus made up of ul/li elements such as the ones in twitter's bootstrap library
PHP
3
star
18

idrac-fan-control

A docker container to control iDRAC fan speeds over IPMI
PHP
3
star
19

dns-over-https

-- WORK IN PROGRESS -- DoH (DNS over HTTPS) Client for PHP / Laravel with support for many providers
PHP
2
star
20

code-style-config

Shared code style configs for my own projects.
1
star