ts / writer
Extensible data output library.
This package has no released version yet, and little information is available.
README
Extensible data output library.
Installation
Use Composer to install: composer require ts/writer:~2.1
Direct usage of an implementation
If you know exactly what you want to output, feel free to instantiate a writer implementation directly:
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();
Using the FileWriterContainer
Instead of instantiating writer implementations directly you can use the FileWriterContainer to create the 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();
Using the event system
You can intercept or influence most parts of the mailer's lifecycle by utilizing your favorite event dispatcher. By default Symfony's EventDispatcher Component is used.
To utilize the full power of the EventDispatcher, you need to explicitly pass an instance when creating either the FileWriterContainer or writer implementation:
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);
You're now able to listen in to the following events, which are described inside the TS\Writer\WriterEvents
class:
- BEFORE_WRITE: Dispatched before the writer tries to write.
- INIT: Dispatched when the writer is instantiated.
- WRITE: Dispatched when a line write occurs.
- WRITE_ALL: Dispatched when a writer's writeAll() method is called.
- WRITE_COMPLETE: Dispatched when the writer has finished writing.
Available implementations
Type Class name 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\YamlSince creating excel sheets can be quite complex I've opted out of trying to achieve an abstraction. Feel free to use the great PHPExcel library for that purpose.