ts/writer

该包已被废弃且不再维护。未建议替代包。

可扩展数据输出库。

该包尚未发布任何版本,可用的信息不多。


README

可扩展数据输出库。

安装

使用 Composer 安装: composer require ts/writer:~2.1

直接使用实现

如果您确切知道要输出的内容,可以直接实例化一个writer实现。

use TS\Writer\Implementation\Json;

// Creating the writer
$jsonWriter = new Json;

// Setting the data array
$jsonWriter->setData(array(/* ... */));

// Setting the file path to output to
$jsonWriter->setTargetFile(/* path where the .json file should be created */);

// Dumping the data
$jsonWriter->writeAll();

使用 FileWriterContainer

您可以使用 FileWriterContainer 来创建 writer,而不是直接实例化 writer 实现。

use TS\Writer\FileWriterContainer;

// Create the container
$container = new FileWriterContainer;

// Registration and setting a type
$container->registerWriter('TS\\Writer\\Implementation\\Json', ['json']);
// ...
// Registering further implementations...
// ...

// Creating the writer
$writer = $container->createForType('json');

// Setting the data array
$writer->setData(array(/* ... */));

// Setting the file path to output to
$writer->setTargetFile(/* path where the .json file should be created */);

// Dumping the data
$writer->writeAll();

使用事件系统

您可以通过使用您喜欢的调度器来拦截或影响邮件发送器生命周期的多数部分。默认使用 Symfony 的 EventDispatcher 组件

要利用 EventDispatcher 的全部功能,需要在创建 FileWriterContainer 或 writer 实现时显式传递一个实例。

use TS\Common\Event\EventDispatcher;
use TS\Writer\Implementation\Json;
use TS\Writer\FileWriterContainer;

// Instantiate or get the EventDispatcher instance from somewhere, like a service/DI container
$dispatcher = new EventDispatcher;

// Pass it to the reader implementation or container
$jsonWriter = new Json($dispatcher);
$container = new FileWriterContainer($dispatcher);

您现在可以监听以下事件,这些事件在 TS\Writer\WriterEvents 类中进行了描述

  • BEFORE_WRITE: 在 writer 尝试写入之前触发。
  • INIT: 当 writer 被实例化时触发。
  • WRITE: 当发生行写入时触发。
  • WRITE_ALL: 当调用 writer 的 writeAll() 方法时触发。
  • WRITE_COMPLETE: 当 writer 完成写入时触发。

可用的实现

类型 类名 Csv TS\Writer\Implementation\Csv Ini TS\Writer\Implementation\Ini Json TS\Writer\Implementation\Json Txt TS\Writer\Implementation\Txt Xml TS\Writer\Implementation\Xml Yaml TS\Writer\Implementation\Yaml

由于创建电子表格可能相当复杂,因此我选择不尝试实现抽象。您可以使用优秀的 PHPExcel 库 来实现此目的。

贡献者