takuya/php-genetator-array-access 问题修复 & 功能扩展

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

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

takuya/php-genetator-array-access

Composer 安装命令:

composer require takuya/php-genetator-array-access

包简介

make yield into an Array / ArrayAccess.

README 文档

README

php / GeneratorArrayAccess

Make generator(yield) into ArrayAccess.

Example

<?php
// generators by yield.
$generator = (function(){foreach (['x'.'y','z'] as $e){ yield $e;}})();
// Using with new.
use Takuya\Php\GeneratorArrayAccess;
$iter = new GeneratorArrayAccess($generator);

// Access as Array. 
$iter[0]; #=> 'x'
$iter[1]; #=> 'y'
$iter[2]; #=> 'z'

Why use.

Generator can foreach itself. but, cannot access as Array. This characteristic is against developers's intention. Like this.

Generator cannot be a array.

class MyClass{
  public function elements(){
    foreach ($this->paths[] as $path){
      yield get_something($path);
    }
  }
}

$node = new MyClass();
// Generator can Access foreach
foreach ($node->elements() as $item) {
  // something.    
}
// but Cannot access as Array
$first = $node->elements()[0]; //=> Error

CachingIterator is not enough

Using \CachingIterator is a common way, but make a problem

\CachingIterator cannot be a array.

This behaviour is very confusing.

$node = new MyClass();
$elements = new CachingIterator($node->elements())
// CachingIterator cannot access Directory, before cached.
$first = $elements[1]; //=> BadMethodCallException
// after caching, CachingIterator can access as Array.
foreach ($elements as $e){;;}
$first = $elements[1]; //=> not null.

With FullCache option, cached at initializing.

$node = new MyClass();
$elements = new \CachingIterator(
  $node->elements(),
  \CachingIterator::FULL_CACHE
);// <= All Cached on constructor.

If generator is API call, It can spend a lot of time, caching time inevitable.

CachingIterator is useless. Everyone uses iterator_to_array() instead. iterator_to_array()function has same problem ( load all, mess up Generator concept, get data when using ).

Dynamically get, make Unnecessary api call avoidable.

To solve that problem ,GeneratorArrayAccess cache dynamically.

$node = new MyClass();
$iter = new GeneratorArrayAccess($node->elements());
$iter[1]; //=> make cache $iter[0],$iter[1];
$iter[9]; //=> make cache $iter[0]...$iter[9]

Cache is able to reuse.

$node = new MyClass();
$iter = new GeneratorArrayAccess($node->elements());
// first access 
foreach($iter as $e){;;}
// cache access with rewind.
foreach($iter as $e){;;}

When use this.

Reduce WebAPI Call. without re-arrange code.

Current exists code.

function my_list_items(){
  foreach(  $api->call('list_item') as $id){
    $list[]=$api->call('get_item', $id);
  }
  return $list;
}
$items = $my_list_items();
$item = $items[0];

Use Generator.

function my_list_items(){
  foreach(  $api->call('list_item') as $id){
    $item  = $api->call('get_item', $id);
    yield $item;
  }
}
$items = $my_list_items();
$item = $items[0];//<= No Code changed. Becomes ERORR!. 

Use GeneratorArrayAccess

function my_list_items(){
  return new GeneratorArrayAccess((function(){
  foreach(  $api->call('list_item') as $id){
    $item  = $api->call('get_item', $id);
    yield $item;
  }  
  })());
}
$items = $my_list_items();
$item = $items[0];//<= No Code changed. **No Error**.

This class supports to make use of Generator(yield), Less code changed.

Limitations.

Infinite generator will be no response.

$generator = (function(){
  while(true){ yield 'a';}
})();
$iter = new GeneratorArrayAccess($generator);
sizeof($iter);//=> infinite loop

Installation

from github

repository='php-generators-into-array-access'
composer config repositories.$repository \
vcs https://github.com/takuya/$repository  
composer require takuya/$repository:master
composer install

from composer

composer require takuya/php-genetator-array-access

takuya/php-genetator-array-access 适用场景与选型建议

takuya/php-genetator-array-access 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 98 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 02 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「generator」 「iterator」 「yield」 「ArrayAccess」 「dynamically」 「CachingIteraror」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 takuya/php-genetator-array-access 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: GPL-3.0-or-later
  • 更新时间: 2023-02-26