ehough/generators
最新稳定版本:v1.0.0
Composer 安装命令:
composer require ehough/generators
包简介
Simulate generators in PHP 5.3 and 5.4. Useful for backporting code.
关键字:
README 文档
README
Easily backport generators to PHP 5.3 and 5.4.
ABANDONED
The vast majority of PHP installations now natively support generators, so this library is obsolete.
Why?
This library makes it (relatively) easy to backport code that relies on generators for use on legacy (PHP < 5.5) systems. If you don't need to backport code to PHP 5.3 or 5.4, you don't need this library.
Quick Start
Say you need to use the following code on PHP 5.3:
$generator = function ($values) { print "Let's get started\n"; foreach ($values as $key => $value) { yield $key => $value; } print "Nothing more to do\n"; }; $items = array('foo' => 'bar', 'some' => 'thing'); foreach ($generator($items) as $k => $v) { print "The generator gave us $k => $v\n"; }
The above code results in:
Let's get started The generator gave us foo => bar The generator gave us some => thing Nothing more to do Since the code above uses generators, it won't run on PHP 5.4 or lower. This library provides you with the AbstractGenerator class, which requires you to implement resume($position). $position is incremented each time the generator resumes execution, and you can use the position to determine which part of the generator to run. So the above generator could be rewritten as:
use Hough\Generators\AbstractGenerator class MyGenerator extends \Hough\Promise\AbstractGenerator { private $keys; private $values; public function __construct(array $items) { $this->keys = array_keys($items); $this->values = array_values($items); } protected function resume($position) { // first execution if ($position === 0) { print "Let's get started\n"; } // still inside the for loop if ($position < count($this->values)) { // return an array of two items: the first is the yielded key, the second is the yielded value return array( $this->keys[$position], $this->values[$position] ); } // we must be done with the for loop, so print our last statement and return null to signal we're done print "Nothing more to do\n"; return null; } } $items = array('foo' => 'bar', 'some' => 'thing'); foreach (new MyGenerator($items) as $k => $v) { print "The generator gave us $k => $v\n"; }
The above code results in:
Let's get started The generator gave us foo => bar The generator gave us some => thing Nothing more to do The code is not nearly as clean and simple, but any generator can be rewritten using this library.
Yielding Keys and Values
You have three choices for what you return from resume($position):
- If you return null, you are signaling that there are no more statements inside the generator. The generator will be considered to be closed at this point.
- If you return an array with two values, the first element is interpreted to be the yielded key and the second value is the yielded value.
- If you return an array with one value, it is interpreted to be a yielded value, and
$positionwill be used as the key.
Accessing Sent Values
You can access the last value sent in from the caller with getLastValueSentIn(). This might be null.
Handling Exceptions
By default, if an exception is thrown into the generator (via throw(\Exception $e)) it will be rethrown back to the calling context. If you'd like to "catch" these exceptions, you can override onExceptionThrownIn(\Exception $e) and swallow or otherwise handle the exception.
ehough/generators 适用场景与选型建议
ehough/generators 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.38k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「generator」 「generators」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ehough/generators 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ehough/generators 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ehough/generators 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Memio's PrettyPrinter, used to generate PHP code from given Model
Extend Laravel's generators scaffold.
Code generators for Zend Framework 2 in console.
Laravel Artisan Command To Generate Facades (php artisan make generator now execpts facade Facadename)
Advanced Laravel generators, that include schema information.
Generate service and repository file and interface
统计信息
- 总下载量: 8.38k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 14
- 依赖项目数: 6
- 推荐数: 0
其他信息
- 授权协议: MPL-2.0
- 更新时间: 2026-01-04