定制 dragon-code/laravel-http-macros 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

dragon-code/laravel-http-macros

最新稳定版本:1.4.0

Composer 安装命令:

composer require dragon-code/laravel-http-macros

包简介

Extending the functionality of the Laravel HTTP client

README 文档

README

laravel http macros

Stable Version Total Downloads Github Workflow Status License

Installation

To get the latest version of HTTP Macros, simply require the project using Composer:

composer require dragon-code/laravel-http-macros

Configuration

If desired, you can publish the configuration file using the console command:

php artisan vendor:publish --provider="DragonCode\\LaravelHttpMacros\\ServiceProvider"

If your application already has a config/http.php file, then you can simply add a new macros key from the configuration file to it.

Here you can specify a list of your classes for registering macros. Macro classes must inherit from the abstract class DragonCode\LaravelHttpMacros\Macros\Macro.

You can also redefine macro names using an associative array. For example:

// Config
return [
    'macros' => [
        'request' => [
            WithLoggerMacro::class,
        ],
        'response' => [
            ToDataMacro::class,
        ],
    ],
];

// Macro
Http::withLogger('some')->get();
Http::withLogger('some')->get()->toData(...);
Http::get()->toData(...);
// Config
return [
    'macros' => [
        'request' => [
            'qwerty' => WithLoggerMacro::class,
        ],
        'response' => [
            'qwerty' => ToDataMacro::class,
        ],
    ],
];

// Macro
Http::qwerty('some')->get();
Http::qwerty('some')->get()->qwerty(...);
Http::qwerty('some')->get()->toData(...); // method not found

Http::get()->qwerty(...);
Http::get()->toData(...); // method not found

Usage

Available Methods

Request

Response

Method Listing

withLogger()

Adds the ability to log HTTP requests and responses.

use Illuminate\Support\Facades\Http;

Http::withLogger('some_channel')->get();

This method will log HTTP requests and responses.

It is also possible to use your own handler, message formatting and path to the log file. To do this, you need to specify the desired channel name from the log file and define the necessary parameters in it.

For example:

// config/logging.php
return [
    // ...
    
    'channels' => [
        'some' => [
            'driver' => 'single',
            'level' => env('LOG_LEVEL', 'debug'),
            'path' => storage_path('logs/some.log'),
            'handler' => \App\Logging\SomeHandlerStack::class,
            'formatter' => \App\Logging\MessageFormatter::class,
        ],
    ],
];

// Usage
return Http::withLogger('some')->...

toData()

The class instance will be returned.

use Illuminate\Support\Facades\Http;

// Returns a SomeData object
return Http::get()->toData(SomeData::class);

// Will return a SomeData object generated from the JSON path
return Http::get()->toData(SomeData::class, 'data.item');

// Returns the result of the callback execution
return Http::get()->toData(
    fn (array $data) => new SomeData(
        $data['data']['item']['id'],
        $data['data']['item']['title']
    )
);

// Returns the result of the callback execution from a custom JSON path
return Http::get()->toData(
    fn (array $data) => new SomeData($data['id'], $data['title']),
    'data.item'
);

Note

If a from method exists in a class, then it will be called to construct the object.

Compatible with Spatie Laravel Data.

class SomeData
{
    public function __construct(
        public int $id,
        public string $title
    ) {}
    
    public static function from(array $data): static
    {
        return new static(...$data);
    }
}

return Http::get()->toData(SomeData::class);

toDataCollection()

The Illuminate\Support\Collection object or an object inherited from it will be returned.

use Illuminate\Support\Facades\Http;

// Returns a collection of SomeData objects
return Http::get()->toDataCollection(SomeData::class);

// Returns a collection of SomeData objects formed from the JSON path
return Http::get()->toDataCollection(SomeData::class, 'data.item');

// Returns the result of the callback execution
return Http::get()->toDataCollection(
    fn (array $data) => collect([
        new SomeData(
            $data['data']['item']['id'],
            $data['data']['item']['title']
        ),
    ])
);

// Returns the result of the callback execution from a custom JSON path
return Http::get()->toDataCollection(
    fn (array $data) => collect([
        new SomeData(...$data),
    ]),
    'data.item'
);

Note

If a collect method exists in a class, then it will be called to construct the collection.

Compatible with Spatie Laravel Data.

use Illuminate\Support\Collection;

class SomeData
{
    public function __construct(
        public int $id,
        public string $title
    ) {}
    
    public static function collect(array $items): Collection
    {
        return collect($items)->map(
            fn (array $item) => new static(...$item)
        );
    }
}

return Http::get()->toDataCollection(SomeData::class);

Generate IDE Helper files

You can generate helper files for the IDE using the console command:

php artisan http:macros-helper

This will help your IDE suggest methods.

IDE Helper

License

This package is licensed under the MIT License.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-08-22