mallardduck/immutable-read-file 问题修复 & 功能扩展

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

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

mallardduck/immutable-read-file

Composer 安装命令:

composer require mallardduck/immutable-read-file

包简介

An experiment in making an 'immutable' wrapper for PHP's file functions.

README 文档

README

Latest Stable Version Total Downloads License codecov Coverage Status Latest Unstable Version

If you've ever used fopen/SplFileObject and wanted the results to be idempotent1 this is de way.

With this library you get a read-only immutable wrapper for the basic SplFileObject - which is essentially the OOP fopen.

You probably would only rarely have a use case for this but if you do you'll know...and it may be hecking useful.

1 = Only technically because the wrapper tracks a "canonical" position to always work from.

Installation

You can install the package via composer:

composer require mallardduck/immutable-read-file

Usage

use MallardDuck\ImmutableReadFile\ImmutableFile;

$fileName = __DIR__ . '/tests/stubs/json.txt'; // CONTENT: {"hello": "world"}

$step1 = ImmutableFile::fromFilePath($fileName);
echo $step1->fgetc(); // { - The first character of your file
echo $step1->fgetc(); // { - The first character of your file, again
$step2 = $step1->advanceBytePosition(); // A new r/o immutable entity advanced a single byte by default.
echo $step2->fgetc(); // " - The second character of your file
echo $step2->fgetc(); // " - The second character of your file, again

Why! I don't get it?

So, you were warned that this would only be useful rarely - but when it is useful it's hecking useful. You did read that right?

The main way to understand when this is useful is to understand how it's different from fopen or SplFileObject. Since this will be useful in cases when the default behavior of those is not desired.

So see for yourself! Examples...

How fopen works by default

The use of a second fopen, and the fseek, are to emulate what $step1->advanceBytePosition() does in the usage example.

$fileName = __DIR__ . '/tests/stubs/json.txt'; // CONTENT: {"hello": "world"}

$step1 = fopen($fileName, 'r');
echo fgetc($step1) . PHP_EOL; // { - The first character of your file
echo fgetc($step1) . PHP_EOL; // " - The second character of your file
$step2 = fopen($fileName, 'r');
fseek($step2, 1);
echo fgetc($step2) . PHP_EOL; // " - The second character of your file
echo fgetc($step2) . PHP_EOL; // h - The third character of your file

How SplFileObject works by default

The use of a second new SplFileObject, and the $step2->fseek(1), are to emulate what $step1->advanceBytePosition() does in the usage example.

$fileName = __DIR__ . '/tests/stubs/json.txt'; // CONTENT: {"hello": "world"}

$step1 = new SplFileObject($fileName, 'r');
echo $step1->fgetc() . PHP_EOL; // { - The first character of your file
echo $step1->fgetc() . PHP_EOL; // " - The second character of your file
$step2 = new SplFileObject($fileName, 'r');
$step2->fseek(1);
echo $step2->fgetc() . PHP_EOL; // " - The second character of your file
echo $step2->fgetc() . PHP_EOL; // h - The third character of your file

Summary

Comparing the ways that fopen and SplFileObject work we can see they are functionally identical to each other. However, this also highlights how they are different from ImmutableReadFile.

When you use a method on either fopen/SplFileObject that returns content, then the current cursor position is incremented. That means when you run fgetc on these you'll always get either: the next character, or the EOF.

However for ImmutableReadFile, these methods do not affect the cursor position. This means that if you run fgetc, you'll always get the same exact character. Only until you explicitly advance the byte position will you get a novel character. When that happens you're actually getting a new instance of ImmutableFile too.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

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

mallardduck/immutable-read-file 适用场景与选型建议

mallardduck/immutable-read-file 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14 次下载、GitHub Stars 达 1, 最近一次更新时间为 2020 年 12 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 mallardduck/immutable-read-file 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 14
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 9
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-12-18