madewithlove/export 问题修复 & 功能扩展

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

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

madewithlove/export

Composer 安装命令:

composer require madewithlove/export

包简介

Interfaces and basic implementations for file exports from a web application

README 文档

README

Latest Version on Packagist Software License Build Status Code Coverage Quality Score

Usage

What it does in one code sample:

// A list of users to export.
$users = [
    [
        'username' => 'John Doe',
        'email' => 'john.doe@gmail.com',
    ],
    [
        'username' => 'Jane Doe',
        'email' => 'jane.doe@gmail.com',
    ],
];

// Create a new CSV exporter object.
$exporter = new Madewithlove\Export\Csv\Exporter();

// Create a new custom Transformer object (an anonymous class, only in PHP 7)
$transformer = new class implements Madewithlove\Export\Csv\Transformer, Madewithlove\Export\Csv\WithHeaders {
    public function getHeaders()
    {
        return ['username', 'email'];
    }

    public function transform(array $user)
    {
        return [
            $user['username'],
            $user['email'],
        ];
    }
};

$exporter->setItems($users);
$exporter->setTransformer($transformer);

// New controller being (an anonymous class, only in PHP 7)
$controller = new class {
    use Madewithlove\Export\Http\Psr7Response;

    /**
     * @param Madewithlove\Export\Exporter $exporter
     */
    public function index(Exporter $exporter)
    {
         return $this->fileDownload($exporter->getContent(), 'users.csv');
    }
};

$psrResponse = $controller->index($exporter);

CSV exporter

The included CSV exporter (Madewithlove\Export\Csv\Exporter) will create the file contents for a CSV export file. For that it uses the Writer class of the league/csv package, but that's just an implementation detail. It adheres to the Madewithlove\Export\Exporter interface (feel free to make an XML or any other exporter implementation) and returns the file content when you call the getContent() method on it. You can define which items it should export by passing an array or an Iterator (like a Generator) to the setItems($items) method. You can also optionally set a Transformer to apply a transformation on each row of the given items. This Transformer is used by the League\Csv\Writer class, and may implement the Madewithlove\Export\Csv\WithHeaders contract to let the writer know which headers the CSV file should have.

Transformers

A Transformer object has a method transform(array $row) : array which allows you to do transformations on each row. The interface Madewithlove\Export\Csv\WithHeaders defines a getHeaders() : array method that returns the headers to be used in the CSV file.

This package also includes some transformer implementations for general usage:

Callable transformer

This allows you to use any callable (function) without having to create a class that implements the Transformer interface. Create one by using the factory method, or use the setter method:

use Madewithlove\Export\Csv\Transformers\CallableTransformer;

$transformer = (new CallableTransformer())->setTransformer(function (array $row) {...});

$transformer = CallableTransformer::fromCallable(function (array $row) {...});

Null transformer, just headers

When you don't really need to do a transformation on the row, but you do want to insert headers in the CSV file, use the JustHeaders transformer class:

use Madewithlove\Export\Csv\Transformers\JustHeaders;

$transformer = (new JustHeaders())->setHeaders(['username', 'email']);

$transformer = JustHeaders::fromHeaders(['username', 'email']);

Headers decorator

When you have an existing Transformers object but you want it to add headers to the CSV file too, you don't need to extend it. Just wrap it with the WithHeadersDecorator like this:

use Madewithlove\Export\Csv\Transformers\JustHeaders;

$transformer = new WithHeadersDecorator($reusedTransformer, $headers);

$transformer = (new WithHeadersDecorator($reusedTransformer))->setHeaders($headers);

HTTP Response objects

Both Symfony and PSR-7 reponse objects are supported. Use the trait Madewithlove\Export\Http\SymfonyResponse or Madewithlove\Export\Http\Psr7Response in your controller to make a file download response object with the fileDownload($content, $filename) method. This requires you to install the symfony/http-foundation package or zendframework/zend-diactoros package respectively.

Install

In order to install it via composer you should run this command:

composer require madewithlove/export

Testing

$ vendor/bin/phpunit

Credits

All Contributors

License

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

madewithlove/export 适用场景与选型建议

madewithlove/export 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.91k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2016 年 02 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 madewithlove/export 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-02-22