rexlabs/laravel-smokescreen 问题修复 & 功能扩展

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

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

rexlabs/laravel-smokescreen

Composer 安装命令:

composer require rexlabs/laravel-smokescreen

包简介

README 文档

README

License: MIT Build Status Code Coverage Packagist

Overview

Laravel Smokescreen is a package for transforming your Laravel models, and other entities.

  • Transform API responses
  • Transform Job and Event payloads
  • Minimal boiler-plate and bootstrap
  • Supports complex relationships for embedded data
  • Supports eager loading of relationships
  • Allows transforming different types of resources
  • Can handle serializing to customisable formats

This package tightly integrates the rexlabs/smokescreen (Vanilla PHP) package with the Laravel framework, to provide the convenience and minimal boilerplate when working with Laravel applications.

Usage

<?php
class MyController extends Controller
{
    public function index()
    {
        return Smokescreen::transform(Post::paginate());
    }
    
     public function show(Post $post)
     {
        return Smokescreen::transform($post);
     }
}
  • laravel-smokescreen is bootstrapped into Laravel's app container, so you can also type-hint it to be injected into your controller's constructor or methods.
  • Using the facade (as above) is recommended within controller methods, use type-hinting in service classes (if needed)
  • You can also use it directly from the container via app('smokescreen') as shown above.
  • Since we implement the Responsable interface, you can simply return smokescreen from any controller method.

Requirements

  • PHP >= 7.0
  • Laravel >= 5.5

Installation

This package is currently hosted on RexSoftware's private packagist repository. First ensure you have configured your composer.json to use this repository.

Install package

composer require rexlabs/laravel-smokescreen

This package will be auto-discovered, and no additional configuration is necessary.

Configuration

To publish the configuration file to your app/config folder, run the following command:

php artisan vendor:publish --provider='Rexlabs\Laravel\Smokescreen\Providers\ServiceProvider --tag=config'

This will create config/smokescreen.php:

<?php
return [
    // Set the default namespace for resolving transformers when
    // they are not explicitly provided.
    'transformer_namespace' => 'App\Transformers',
    
    // Override the default serializer to be used.
    // If not specified - the Smokescreen DefaultSerializer will be used.
    'default_serializer' => null,

    // Set the default request parameter key which is parsed for
    // the list of includes.
    'include_key' => 'include',
];

API

transform(): Set resource to be transformed

$smokescreen->transform(mixed $resource, mixed $transformer = null);

<?php
$smokescreen->transform(Post::find(1));
$smokescreen->transform(Post::all());
$smokescreen->transform(Post::paginate());
$smokescreen->transform(Post::find(1), new SomeOtherTransformer);
  • Smokescreen will automatically determine the type of resource being transformed.
  • It will also infer the Transformer class to use if not provided.

item(): Set single item resource to be transformed

$smokescreen->item(mixed $item, mixed $transformer = null);

<?php
$smokescreen->item(Post::find(1));
$smokescreen->item(Post::find(1), new SomeOtherTransformer);
  • Similar to transform() but only accepts a item.

collection(): Set collection resource to be transformed

$smokescreen->collection(mixed $collection, mixed $transformer = null);

<?php
$smokescreen->collection(Post::all());
$smokescreen->collection(Post::paginate());
$smokescreen->collection(Post::paginate(), new SomeOtherTransformer);
  • Similar to transform() but only accepts a collection.

transformWith(): Set the transformer to use on the previously set resource

$smokescreen->transformWith(TransformerInterface|callable $transformer);

<?php
$smokescreen->transform(Post::find(1))
    ->transformWith(new SomeOtherTransformer);
  • It's an alternative to passing the transformer directly to resource methods.

serializeWith(): Override the serializer to be used

<?php
$smokescreen->serializeWith(new MyCustomSerializer);
  • You only need to set this if you plan to use a different serialize than the default.
  • We provide DefaultSerializer as the default, it returns collections nested under a "data" node, and an item resource without any nesting.
  • Your custom serializer should implement the SerializerInterface interface.

loadRelationsVia(): Override the default Laravel relations loader

$smokescreen->loadRelationsVia(RelationsLoaderInterface $loader);

<?php
$smokescreen->loadRelationsVia(new MyRelationsLoader);
  • You only need to set this if you plan to use a different loader than the default,
  • We provide RelationsLoader as the default which eager-loads relationships for collection resources.
  • Your custom loader should implement the RelationsLoaderInterface interface and provide a load() method.

resolveTransformerVia(): Override the default transformer resolver

$smokescreen->loadTransformersVia(TransformerResolverInterface $loader);

<?php
$smokescreen->loadTransformersVia(new MyTransformerResolver);
  • The resolver is used when a transformer is not explicitly defined on a resource.
  • The default resolver in this package tries to find a matching transformer class within the path defined in smokescreen.transformer_namespace path, and instantiates it via the app container.
  • You only need to set this if you plan to use a different resolver than the default.
  • Your custom resolver should implement the TransformersLoaderInterface interface and provide a resolve(ResourceInterface) method.

response(): Access the generated response object

$response = $smokescreen->response(int $statusCode = 200, array $headers = [], int $options = 0);

<?php
$smokescreen->response()
    ->header('X-Custom-Header', 'boo')
    ->setStatusCode(405);
  • This method returns an \Illuminate\Http\JsonResponse object so it is not chainable.
  • All supported JsonResponse methods can be applied.
  • You can still return response() directly from your controller since it is a JsonResponse object.
  • You can alternatively use withResponse($callback) to apply changes, and still support chainability.
  • Note: the first call to response() caches the result so that the entire data set is not re-generated every time, this means passing any parameters on subsequent calls will be ignored. You can use clearResponse() or manipulate the JsonResponse object directly.

freshResponse(): Generate a fresh Response object

$response = $smokescreen->freshResponse(int $statusCode = 200, array $headers = [], int $options = 0);

  • Unlike response() this method returns a fresh non-cached JsonResponse object (by calling clearResponse() first).
  • This method returns an \Illuminate\Http\JsonResponse object so it is not chainable. See withResponse() for a chainable method.
  • All supported JsonResponse methods can be applied.

withResponse(): Apply changes to the generated response object

$smokescreen->withResponse(callable $apply);

<?php
$smokescreen->withResponse(function (JsonResponse $response) {
    $response->header('X-Crypto-Alert', 'all your coins are worthless!');
});
  • Provide a callback that accepts a JsonResponse object and manipulates the response
  • This method is chainable unlike response()

clearResponse(): Clear any cached response

$smokescreen->clearResponse();

<?php
$smokescreen->response();       // Data is generated, response object is built and cached
$smokescreen->response(500);    // NOPE - Cached, wont be changed!
$smokescreen->clearResponse();
$smokescreen->response(500);    // Response is re-generated
  • Reset's any cached response object

Transformers

Example Transformer

<?php
class PostTransformer extends AbstractTransformer
{
    protected $includes = [
        'user' => 'default|relation:user|method:includeTheDamnUser',
        'comments' => 'relation',
    ];

    public function transform(Post $post): array
    {
        return [
            'id' => $post->id,
            'user' => $this->when($post->user_id, [
                'id' => $post->user_id,
            ]),
            'title' => $post->title,
            'summary' => $post->summary,
            'body' => $post->body,
            'created_at' => utc_datetime($post->created_at),
            'updated_at' => utc_datetime($post->updated_at),
        ];
    }

    public function includeTheDamnUser(Post $post)
    {
        return $this->item($post->user); // Infer Transformer
    }

    public function includeComments(Post $post)
    {
        return $this->collection($post->comments, new CommentTransformer);
    }
}
  • You declare your available includes via the $includes array
  • Each include accepts 0 or more of the following directives:
    • default: This include is always enabled regardless of the requested includes
    • relation: Indicates that a relation should be eager-loaded. If the relation name is different specify it as relation:othername
    • method: By default the include key is mapped to include{IncludeKey} you can provide the method to be used instead
  • Your transform() method should return an array.
  • Define your include methods in the format include{IncludeKey}(Model) - they should return either a collection() or an item()
  • when() is a simple helper method which accepts a condition and returns either the given value when true, or null (by default) when false. In the above example the "user" node will be null if there is no user_id set on the $post object.

Contributing

Pull-requests are welcome. Please ensure code is PSR compliant. Github Repository

About

  • Author: Jodie Dunlop
  • License: MIT
  • Copyright (c) 2018 Rex Software Pty Ltd

rexlabs/laravel-smokescreen 适用场景与选型建议

rexlabs/laravel-smokescreen 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 418 次下载、GitHub Stars 达 4, 最近一次更新时间为 2018 年 03 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 rexlabs/laravel-smokescreen 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 4
  • Watchers: 4
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-03-08