dmt-software/import-reader
Composer 安装命令:
composer require dmt-software/import-reader
包简介
Read parts of a file/stream into a series of objects or arrays
README 文档
README
The reader is designed to go through a file to return chunks of its contents as objects without a high memory usage.
Installation
composer require dmt-software/import-reader
Usage
Create a reader
The reader can be created manually or the reader builder can be used to create a (default) reader or a pre-configured one.
use DMT\Import\Reader\Decorators\DecoratorInterface; use DMT\Import\Reader\Decorators\Handler\GenericHandlerDecorator; use DMT\Import\Reader\Handlers\JsonReaderHandler; use DMT\Import\Reader\Reader; use DMT\Import\Reader\Handlers\Sanitizers\SanitizerInterface; use pcrov\JsonReader\JsonReader; $internalReader = new JsonReader(); $internalReader->open('/path/to/some.json'); /** @var DecoratorInterface[] $decorators */ /** @var SanitizerInterface[] $sanitizers */ $reader = new Reader( new JsonReaderHandler($internalReader, ...$sanitizers), new GenericHandlerDecorator(), ...$decorators );
Visit the reader handler documentation for more information on the handlers internal readers and sanitizers.
Adding decorators
Once a default reader is created, it is possible to add extra decorators to apply on each object that is returned by the read method.
use DMT\Import\Reader\Decorators\ToObjectDecorator; use DMT\Import\Reader\Reader; /** @var Reader $reader */ $reader->addDecorator(new ToObjectDecorator(Customer::class, ['id' => 'id', 'name' => 'fullName', ])); foreach ($reader->read() as $customer) { // import customer; }
More on decorators see the documentation.
Adding filters
Besides controlling the output by using a decorator a part of the object stream the reader returns can be skipped or filtered.
use DMT\Import\Reader\Reader; /** start on item 4 */ $skip = 3; /** skip all objects that has no id */ $filter = function (object $current) { return isset($current->id); } foreach ($reader->read($skip, $filter) as $item) { // import item }
Visit the filters section for more information about filter callbacks.
Error Handling
UnreadableException
This is thrown when the given file can not be read. This can have several causes:
- file is unreadable
- can not set file pointer
- end of file reached whilst set file pointer
- file pointer is set to the wrong return type
use DMT\Import\Reader\Exceptions\UnreadableException; try { $readerBuilder->build($file, $options = []); } catch (UnreadableException $exception) { // file can not be processed }
ReaderReadException
This can happen when a single chunk can not be read from the file. It will stop execution of the reading process.
use DMT\Import\Reader\Exceptions\ReaderReadException; try { foreach ($reader->read() as $n => $object) { // import object } } catch (ReaderReadException $exception) { // execution stopped, after $n rows }
DecoratorException
This exception happens silently. It triggers a user warning and continues the reading process. Depending on you server configuration this warning is ignored or send to STDOUT or STDERR.
Other Exceptions or Errors
Any other kind of failures are (most likely) problems that are caused by configuration faults or when this software is implemented incorrect.
dmt-software/import-reader 适用场景与选型建议
dmt-software/import-reader 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 88 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 09 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「json」 「xml」 「file」 「iterator」 「csv」 「stream」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dmt-software/import-reader 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dmt-software/import-reader 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dmt-software/import-reader 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Kinikit - PHP Application development framework MVC component
The file manager intended for using Laravel with CKEditor / TinyMCE / Colorbox
ext-json wrapper with sane defaults
A package to cast json fields, each sub-keys is castable
Load DOM document safety
Laravel Media Popup to upload/view the files
统计信息
- 总下载量: 88
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 18
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-09-16