sidigi/laravel-remote-models 问题修复 & 功能扩展

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

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

sidigi/laravel-remote-models

Composer 安装命令:

composer require sidigi/laravel-remote-models

包简介

Use remote requests in laravel eloquent models way

README 文档

README

Latest Version on Packagist GitHub Tests Action Status Total Downloads

This is where your description should go. Try and limit it to a paragraph or two. Consider adding a small example.

Installation

You can install the package via composer:

composer require sidigi/laravel-remote-models

Usage

Config

    'defaults' => [
        'response_key'        => 'data',
        'pagination_strategy' => 'page_based',
    ],

    'providers' => [
        'aws-lambda' => [
            'class' => Sidigi\LaravelRemoteModels\Providers\AwsLambdaProvider::class,
        ],
        'http' => [
            'class' => Sidigi\LaravelRemoteModels\Providers\HttpProvider::class,
        ],
    ],

    'pagination_strategies' => [
        'page_based' => [
            'class'               => Sidigi\LaravelRemoteModels\Pagination\PaginationBaseStrategy::class,
            'response_number_key' => 'meta.pages_count',
            'defaults'            => [
                'number' => 1,
                'size'   => 100,
            ],
        ],
    ],

    'clients' => [
        'comment-client' => [
            'client' =>  App\RemoteClients\CommentClient::class,
            'base_uri' => 'https://jsonplaceholder.typicode.com',
   |        'provider' => 'http',
            'pagination_strategy' => 'page_based',
            'paths' => [
                'index_comments' => 'comments',
                'index_comments_filter_by_post' => '/comments?postId={id}',
                'todo_detail' => 'todos/{id}',
            ],
        ],
        'aws-comment-client' => [
            'client' =>  App\RemoteClients\AwsCommentClient::class,
            'base_uri' => 'https://jsonplaceholder.typicode.com',
   |        'provider' => 'aws-lambda',
            'function_name' => 'user-service-api',
            'pagination_strategy' => 'page_based',
            'paths' => [
                'index_comments' => 'comments',
                'index_comments_filter_by_post' => '/comments?postId={id}',
                'todo_detail' => 'todos/{id}',
            ],
        ],
    ],

    'models' => [
        App\RemoteModels\Comment::class => 'comment-client',
        //or
        App\RemoteModels\Comment::class => App\RemoteClients\CommentClient::class,
        //or
        App\RemoteModels\Comment::class => [
            'aws' => 'aws-comment-client',
            'http' => 'comment-client',
         ]
    ],

Clients

use Sidigi\LaravelRemoteModels\Client;

class CommentClient extends Client
{
}

$comments = Comment::getRemoteClient()->get();
$comments = Comment::getRemoteClient()->get('/comments');
$comments = Comment::getRemoteClient()->get('/comments/{id}', ['id' => 1]);
$comments = Comment::getRemoteClient()->get('/comments/{id}', ['id' => 1, 'active' => true]);
$comments = Comment::getRemoteClient()->withPath('/comments/{id}', ['id' => 1])->get();
$comments = Comment::getRemoteClient()->withQuery(['active' => true])->get();
$comments = Comment::getRemoteClient()->get(['active' => true]);
use Sidigi\LaravelRemoteModels\Client;

class CommentClient extends Client
{
    public function getPaths() : array
    {
        return [
            'index_comments' => 'comments',
            'detail_comment' => 'comment/{id}',
        ]
    }
}

$comments = CommentClient::withPath('index_comments')->get();
$comments = CommentClient::indexComments()->get();
$comments = CommentClient::withPath('detail_comment', ['id' => 1])->get();
$comments = CommentClient::detailComment(['id' => 1])->get();
$comments = CommentClient::detailComment(['id' => 1])->withQuery(['active' => true])->get();
use Sidigi\LaravelRemoteModels\Client

class CommentClient extends Client
{
}

$comments = CommentClient::withPath('/comments/{id}', ['id' => 1])
                ->withQuery(['active' => true])
                ->filter(['id' => [1, 2, 3])
                ->include('posts.user')
                ->orderBy('created_at')
                ->paginate(['size' => 1, 'number' => 2])
                ->get();

Models

use Sidigi\LaravelRemoteModels\Client;

class CommentClient extends Client
{
}

class Comment extends Model
{
    use HasRemotes;

    protected $guarded = [];

    public function getRemoteClient(): string
    {
        return resolve(CommentClient::class);
    }
}

$comment = Comment::getRemoteClient()
    ->indexComments()
    ->get() //response with models
    ->mapModel(Comment::class, fn ($item) => ['id' => $item['id']])
    ->first();

//App\RemoteModels\Comment

Client classes are extended Illuminate\Http\Client\PendingReuqest. You can use all http client methods

$comment = Comment::getRemoteClient()
    ->indexComments()
    ->withHeaders(['X-Foo' => 'X-Baz']) //withToken, withAuth, etc.
    ->get() //response with models
    ->mapModel(Comment::class)
    ->first();
//App\RemoteModels\Comment
$builder = Comment::getRemoteClient()->indexComments();

foreach ($builder->perPage() as $response) {
    $comments = $response->mapModel(Note::class);
}
$builder = Post::getRemoteClient()->index();

foreach ($builder->perPage() as $response) {
    $comments = $response->mapModel(
        Comment::class,
        fn ($item) => ['id' => $item['id']],
        'data.*.comments'
    );
}
$builder = Post::getRemoteClient()->index();

foreach ($builder->perPage() as $response) {
    $commentIds = $response->get('data.*.comments.*.id');
}

Detail information about laravel http client here

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email sidigicoder@gmail.com or use the issue tracker.

Credits

License

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

统计信息

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

GitHub 信息

  • Stars: 5
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-08-14

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固