承接 dragon-code/laravel-http-macros 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

dragon-code/laravel-http-macros

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.

dragon-code/laravel-http-macros 适用场景与选型建议

dragon-code/laravel-http-macros 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 218 次下载、GitHub Stars 达 11, 最近一次更新时间为 2024 年 08 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「http」 「laravel」 「macros」 「helldar」 「dragon-code」 「andrey helldar」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 dragon-code/laravel-http-macros 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 dragon-code/laravel-http-macros 我们能提供哪些服务?
定制开发 / 二次开发

基于 dragon-code/laravel-http-macros 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

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