phpro/resource-stream 问题修复 & 功能扩展

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

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

phpro/resource-stream

Composer 安装命令:

composer require phpro/resource-stream

包简介

A safe OOP wrapper around resource streams

README 文档

README

A safe OOP wrapper around resource streams in PHP:

  • No need to worry about streams that have been closed.
  • Instead of returning false, detailed exceptions are thrown when something goes wrong.
  • Streams are automatically closed when the object is destructed (with option to opt-out).
  • Handy factory methods for common streams.

Installation

composer require phpro/resource-stream

Usage

use Phpro\ResourceStream\ResourceStream;

$stream = (new ResourceStream(fopen('php://temp', 'r+')))
    ->write('Hello World')
    ->rewind();

// Various ways to read:
echo $stream->read();
echo $stream->read($bufferSize);
echo $stream->readLine();
echo $stream->readLine($bufferSize, ending: \PHP_EOL);
echo $stream->getContents();

// Or in batches (Generator<string>)
$cursor = $stream->readBatches($bufferSize);
$cursor = $stream->readLines($bufferSize, ending: \PHP_EOL); 

// Get access to PHP's inner resource stream
$innerStream = $stream->unwrap();

// Get access to common information:
$stream->isOpen();
$stream->isEof();
$stream->uri();
$stream->size();

// Possibility to copy contents across streams
$stream->copyTo($anotherStream);
$stream->copyFrom($anotherStream);

// Streams will automatically be closed on destruction.
// Of course, you can choose to keep it open or close it manually:
$stream->keepAlive();
$stream->close();

Built-in Streams

The following streams are available by default:

CliStream

Can be used to open up CLI stdin, stdout and stderr streams.

use Phpro\ResourceStream\Factory\CliStream;

$stdin = CliStream::stdin();
$stdout = CliStream::stdout();
$stderr = CliStream::stderr();

FileStream

Validates if the local file exists and opens it up for you to use.

use Phpro\ResourceStream\Factory\FileStream;

$stream = FileStream::create('/path/to/file', FileStream::READ_WRITE_MODE);

IOStream

Can be used to open up CGI php://input and php://output streams.

use Phpro\ResourceStream\Factory\IOStream;

$input = IOStream::input();
$output = IOStream::output();

MemoryStream

Creates an in-memory stream for you to use.

use Phpro\ResourceStream\Factory\MemoryStream;

$stream = MemoryStream::create();

Psr7Stream

Creates a stream from a PSR-7 stream / request / response. before you can use this stream, you'll need to install the guzzlehttp/psr-7 package which contains a stream wrapper implementation.

composer require psr/http-message guzzlehttp/psr-7
use Phpro\ResourceStream\Factory\Psr7Stream;

$stream = Psr7Stream::createFromStream($anyPsr7Stream);
$stream = Psr7Stream::createFromRequest($anyPsr7Request);
$stream = Psr7Stream::createFromResponse($anyPsr7Response);

TmpStream

Creates a temporary file and opens it up for you to use. After the stream is closed, the temporary file will be removed.

use Phpro\ResourceStream\Factory\TmpStream;

$stream = TmpStream::create();

ZipStream

Opens a single entry from a zip archive as a read-only stream. Validates that the archive exists before opening; failures (missing entry, corrupt archive, wrong/missing password) surface as a StreamActionFailureException carrying the underlying PHP message.

use Phpro\ResourceStream\Factory\ZipStream;

$stream = ZipStream::read('/path/to/archive.zip', 'folder/file.txt');

// For encrypted entries, pass any options accepted by the `zip` stream context:
$stream = ZipStream::read('/path/to/archive.zip', 'secret.txt', ['password' => 'secret']);

Note: the zip:// stream wrapper is read-only. To write into an archive, use PHP's ZipArchive directly.

phpro/resource-stream 适用场景与选型建议

phpro/resource-stream 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 51.34k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 10 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 phpro/resource-stream 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 51.34k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 13
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-10-17