fruivita / line-reader
逐行读取大文件,而不造成Laravel应用内存溢出
Requires
- php: ^8.0
- illuminate/collections: ^9.11
- illuminate/pagination: ^9.11
- illuminate/support: ^9.11
Requires (Dev)
- nunomaduro/larastan: ^2.1
- orchestra/testbench: ^7.4
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.2
- phpstan/phpstan: ^1.6
- phpstan/phpstan-deprecation-rules: ^1.0
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-09-06 21:59:42 UTC
README
本包适用于 Laravel 应用,允许您逐行读取大文件的文件内容,而不会杀死服务器,即不需要一次性将所有内容加载到内存中,从而不会导致 内存溢出错误。
这里使用的策略是逐行读取文件内容,优化服务器资源的利用,最重要的是,以高效的方式。
也可以分页显示文件内容,同样不需要将整个文件加载到内存中,除了页面本身。
use FruiVita\LineReader\Facades\LineReader; $generator = LineReader::readLines($file_path); // or $length_aware_paginator = LineReader::readPaginatedLines($file_path, $per_page, $page);
目录
注释
⭐ 在内部,此包使用php的 SplFileObject 和 Generators 类来读取文件内容。在分页的具体情况下,使用 LimitIterator 类来限定要读取的内容的开始和结束。
❤️ 受 bcremer/LineReader 包的启发。
⬆️ 返回
先决条件
-
PHP 依赖
PHP ^8.0
composer check-platform-reqs
⬆️ 返回
安装
-
通过 composer 安装
composer require fruivita/line-reader
-
可选发布翻译
php artisan vendor:publish --provider='FruiVita\LineReader\LineReaderServiceProvider' --tag='lang'
可用的翻译字符串如下。根据需要更改它们。
{ "The file entered could not be read": "The file entered could not be read" }
此包已为 en 和 pt-br 提供了翻译。
⬆️ 返回
工作原理
-
逐行读取文件。
use FruiVita\LineReader\Facades\LineReader; public function example() { foreach (LineReader::readLines($file_path) as $key => $line) { // $key is 0 when reading the 1st line, 1 when reading the 2nd line, and so on. // $line is a string with the contents of the line. } }
LineReader公开以下方法逐行读取文件
✏️ readLines
use FruiVita\LineReader\Facades\LineReader; /** * @param string $file_path full path of the file to be read * * @throws \FruiVita\LineReader\Exceptions\FileNotReadableException * * @return \Generator */ LineReader::readLines(string $file_path);
🚨 异常
- readLines 如果没有文件读取权限或找不到文件,则抛出 \FruiVita\LineReader\Exceptions\FileNotReadableException
-
按页读取文件。
use FruiVita\LineReader\Facades\LineReader; public function example() { $per_page = 15; $page = 2; $length_aware_paginator = LineReader::readPaginatedLines(string $file_path, int $per_page, int $page); // The index of the items in the collection respects their position in the file using a zero-based index, // that is, in the example above the 1st item on page 2 will have index 15, since it is the 16th line of // the file and the last item on page 2 will have index 29, since it is the 30th line of the file. }
LineReader公开以下方法按页读取文件
✏️ readPaginatedLines
use FruiVita\LineReader\Facades\LineReader; /** * @param string $file_path full path of the file to be read * @param int $per_page * @param int $page * @param string $page_name * * @throws \FruiVita\LineReader\Exceptions\FileNotReadableException * @throws \InvalidArgumentException * * @return \Illuminate\Pagination\LengthAwarePaginator */ LineReader::readPaginatedLines(string $file_path, int $per_page, int $page, string $page_name = 'page');
🚨 异常
- readPaginatedLines 如果没有文件读取权限或找不到文件,则抛出 \FruiVita\LineReader\Exceptions\FileNotReadableException
- readPaginatedLines 如果每页数或页数小于1,则抛出 \InvalidArgumentException
⬆️ 返回
测试和持续集成
composer analyse
composer test
composer coverage
⬆️ 返回
变更日志
请参阅 CHANGELOG 了解每个版本中发生了什么更改。
⬆️ 返回
贡献
请参阅 CONTRIBUTING 了解如何贡献更多细节。
⬆️ 返回
行为准则
为确保每个人都能欢迎为这个开源项目做出贡献,请阅读并遵循 行为准则。
⬆️ 返回
安全漏洞
请参阅安全策略了解如何报告安全漏洞或缺陷。
⬆️ 返回
支持和更新
最新版本将在需要时收到支持和更新。其他版本在替换为新版本后6个月内将收到更新,然后停止维护。
🐛 发现了一个bug?!?!请开启一个问题。
⬆️ 返回
路线图
✨ 有任何新想法?!?!开始一个讨论。
以下列表包含已识别和批准的改进需求,将在首次机会窗口内实施。
- n/a
⬆️ 返回
致谢
⬆️ 返回
感谢
👋 感谢以下人员和组织捐赠他们的时间来构建本包中使用的开源项目。
-
❤️ Laravel 提供的包
-
❤️ Nuno Maduro 提供的包 nunomaduro/larastan
-
❤️ PEST 提供的包
-
❤️ PHPStan 提供的包
-
❤️ ergebnis 提供的包 ergebnis/composer-normalize
-
❤️ Shivam Mathur 提供的 Github Action shivammathur/setup-php
-
❤️ GP 提供的 Github Action paambaati/codeclimate-action
-
❤️ Stefan Zweifel 提供的 Github Actions
💸 一些人员或组织有可以购买的产品/服务。如果您通过购买其中之一或成为赞助商来帮助他们,即使只是短期内的,您也将帮助整个 开源 社区继续为所有人开发解决方案。
⬆️ 返回
许可证
MIT 许可证 (MIT)。请参阅许可文件获取更多信息。
⬆️ 返回