interitty/output-buffer-manager
Composer 安装命令:
composer require interitty/output-buffer-manager
包简介
Output buffer manager brings a safe way to work with outputs.
关键字:
README 文档
README
Output buffer manager brings a safe way to work with outputs.
Requirements
- PHP >= 8.5
Installation
The best way to install interitty/output-buffer-manager is using Composer:
composer require interitty/output-buffer-manager
Then register the extension in the Nette config file:
# app/config/config.neon
extensions:
outputBufferManager: Interitty\OutputBufferManager\Nette\DI\OutputBufferManagerExtension
Features
For more complex applications where the work with the output buffer
is needed, this package helps to organize individual handlers.
The naming of each handler is requested, because it helps to prevent very difficult-to-detect and unnecessary problems.
It also allows for an easy route to multiple concurrent handlers at the same moment.
Output buffer manager does nothing until requested. Because of that, it can be registered as a service in the
dependency injection container
or service locator and be used wherever it is needed.
In the following examples, the new instance of OutputBufferManager class is created for simplification. The manager
works with the so-called "handlers", which are anonymous functions
or callbacks.
These "handlers" are encapsulated into objects of class Handler, that represent simple implementation
of the HandlerInterface, which is used internally.
This mechanism provides additional events, that can be useful in some cases.
Example: catch content into a variable
The following example catches any content that was sent to the output and appends it to the given $output variable.
It also shows, that "handler" can be defined as an anonymous functions
Because the handler doesn't have a return value, nothing will be sent to the output or another registered handler.
$output = null;
$outputBufferManager = new OutputBufferManager();
$outputBufferManager->begin('…', function(string $bufferOutput) use(&$output): string {
$output .= $bufferOutput;
return '';
});
// Any content that was sent to the output
echo 'testContent';
$outputBufferManager->end('…');
Example: catch content into property
The following example catches any content that was sent to the output and appends it into the Example::$output property.
It also shows, that "handler" can be defined as a callback.
class Example
{
public string $output = '';
public function __construct(): void
{
$outputBufferManager = new OutputBufferManager();
$outputBufferManager->begin('…', [$this, 'handleCatchContent']);
// Any content that was sent to the output
echo 'testContent';
$outputBufferManager->end('…');
}
public function handleCatchContent(string $bufferOutput): string
{
$this->output .= $bufferOutput;
return '';
}
}
Example: proxy content into a variable
The following example proxy any content that was sent to the output and appends it into the given $output variable.
Because the handler has the return value, everything will be also sent to the output or another registered handler.
$output = null;
$outputBufferManager = new OutputBufferManager();
$outputBufferManager->begin('…', static function(string $bufferOutput) use(&$output): string {
$output .= $bufferOutput;
return $bufferOutput;
});
// Any content that was sent to the output
echo 'testContent';
$outputBufferManager->end('…');
Example: flush content of output buffer
In some cases, it could be useful to manually flush the content of the output buffer into handlers.
For that purpose, there is a flush method on the OutputBufferManager object.
As with other methods, it also expects the name of the current handler to prevent ugly problems.
$outputBufferManager = new OutputBufferManager();
// in some cases like in the foreach cycle …
$outputBufferManager->flush('…');
$outputBufferManager->end('…');
When things go wrong
In some cases is possible to see the following exceptions.
LogicException: Output buffer handler "…" is already registered
As the message of the exception says, another handler with the same name was registered before. The core of the problem is similar to the following code.
$outputBufferManager = new OutputBufferManager();
$outputBufferManager->begin('…', static function(): string {return '';});
$outputBufferManager->begin('…', static function(): string {return '';});
There can be just one handler of the same name at the same time.
Solution
The solution is to use a different (unique) name or stop using the last one and begin work with the new one.
$outputBufferManager = new OutputBufferManager();
$outputBufferManager->begin('…', static function(): string {return '';});
// warning: calling end flush unflushed data
$outputBufferManager->end('…');
$outputBufferManager->begin('…', static function(): string {return '';});
LogicException: Current output buffer manager is not OutputBufferManager
The core of the problem is, that OutputBufferManager is not the current Output buffer handler.
It can happen when OutputBufferManager was not started yet.
$outputBufferManager = new OutputBufferManager();
// Forgotten call of $outputBufferManager->begin('…', static function(): string {return '';});
$outputBufferManager->end('…');
Another possibility is that there is a registered different handler than OutputBufferManager.
$outputBufferManager = new OutputBufferManager();
$outputBufferManager->begin('…', static function(): string {return '';});
// Maybe the old mechanism of the Output buffer was forgotten in the code
ob_start(static function(): string {return '';});
$outputBufferManager->end('…');
Solution
The possible solution depends on the use case:
- Replace old handlers with the
OutputBufferManager. - Flush the old handler (
ob_end_cleanorob_end_flush) to fall back intoOutputBufferManager. - Register new
OutputBufferManagerviaOutputBufferManager::begin.
LogicException: Expected output buffer handler "…" is not registered
The core of the problem is mostly in the misspelled name of the handler.
$outputBufferManager = new OutputBufferManager();
$outputBufferManager->begin('...', static function(): string {return '';});
$outputBufferManager->end('…');
Solution
Fix the name of the expected handler. It is better to use constants for handler names to prevent this problem.
define('OUTPUT_BUFFER_MAIN', '…');
$outputBufferManager = new OutputBufferManager();
$outputBufferManager->begin(OUTPUT_BUFFER_MAIN, static function(): string {return '';});
$outputBufferManager->end(OUTPUT_BUFFER_MAIN);
LogicException: Current output buffer handler "..." is not expected "…"
The core of the problem is mostly in the forgotten termination of the last used handler or crossing the call.
$outputBufferManager = new OutputBufferManager();
$outputBufferManager->begin('…', static function(): string {return '';});
$outputBufferManager->begin('...', static function(): string {return '';});
// Somebody forgot call $outputBufferManager->end('...');
$outputBufferManager->end('…');
Solution
Add forgotten termination of the previous handler or correct the position of the handler calling.
$outputBufferManager = new OutputBufferManager();
$outputBufferManager->begin('…', static function(): string {return '';});
$outputBufferManager->end('…');
$outputBufferManager->begin('...', static function(): string {return '';});
$outputBufferManager->end('...');
interitty/output-buffer-manager 适用场景与选型建议
interitty/output-buffer-manager 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 06 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「output buffer」 「interitty」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 interitty/output-buffer-manager 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 interitty/output-buffer-manager 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 interitty/output-buffer-manager 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A php buffer to work with binary data.
Library implementing buffer utilities
Article CSS-ID Frontend Output Optimization
Library to communicate with CRMBuffer
This library allows you to use the Buffer API using PHP
phpABLE file-system library
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 36
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-06-25