定制 abordji/php-generator-chunk 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

abordji/php-generator-chunk

Composer 安装命令:

composer require abordji/php-generator-chunk

包简介

A utility function to split a PHP Generator into chunks of a specified length, returning a Generator of Generators, with optional key preservation.

README 文档

README

A utility function to split a PHP Generator into chunks of a specified length, returning a Generator of Generators, with optional key preservation.

Description

This library provides a single function, generator_chunk, that allows you to efficiently process large datasets by splitting a PHP Generator into smaller, manageable chunks. This is particularly useful when dealing with data that cannot fit into memory all at once.

Features

  • Chunking: Splits a Generator into chunks of a specified length.
  • Key Preservation: Optionally preserves the original keys of the Generator.
  • Generator of Generators: Returns a Generator where each element is itself a Generator.
  • Usage Considerations: Requires careful handling of the returned Generator of Generators. Directly using iterator_to_array is not recommended.
  • Error Handling: Throws a ValueError if the chunk length is invalid (less than or equal to 0).
  • PHP 8.1+: Requires PHP 8.1 or later due to the use of ValueError.

Requirements

  • PHP 8.1 or later (Requirement for the code itself)
  • Docker (for the development and testing environment)
  • Docker Compose (for orchestrating the Docker environment)
  • Make (for automating development tasks)

Installation

You can install this library using Composer:

composer require abordji/php-generator-chunk

(If you are contributing to this library, please refer to the Contributing section for development setup instructions. You do not need Composer on your host machine to contribute.)

API

generator_chunk(Generator $generator, int $length, bool $preserve_keys = false): Generator

Splits a PHP Generator into chunks of a specified length.

  • $generator (Generator): The Generator to be chunked.
  • $length (int): The length of each chunk. Must be greater than 0.
  • $preserve_keys (bool, optional): Whether to preserve the original keys. Defaults to false.

Returns: (Generator) A Generator where each yielded element is itself a Generator containing the items for that chunk.

Throws: ValueError if $length is less than or equal to 0.

Usage

<?php

// Example usage:
$sourceGenerator = (function () {
    yield 'a' => 1;
    yield 'b' => 2;
    yield 'c' => 3;
    yield 'd' => 4;
    yield 'e' => 5;
})();

$chunkedGenerator = generator_chunk($sourceGenerator, 2, true); // Chunk size 2, preserve keys

echo "Processing chunked generator:\n";
foreach ($chunkedGenerator as $index => $chunk) {
    echo "Chunk " . ($index + 1) . ": [";
    $items = [];
    // $chunk is also a Generator
    foreach ($chunk as $key => $value) {
        $items[] = $key . ':' . $value;
    }
    echo implode(', ', $items) . "]\n";
}
// Output:
// Processing chunked generator:
// Chunk 1: [a:1, b:2]
// Chunk 2: [c:3, d:4]
// Chunk 3: [e:5]

Contributing

Contributions are welcome! Please feel free to submit pull requests or open issues on the repository.

Development Workflow

This project uses Make to automate common development tasks within a Dockerized environment. Ensure you have Docker, Docker Compose, and Make installed on your system. (You do not need PHP or Composer installed locally).

After cloning the repository, start by setting up the development environment:

make setup

This command performs the initial project setup: installs Composer dependencies (inside Docker), builds the required Docker image, and configures Git pre-commit hooks for automated quality checks.

Here are the main commands available for development:

  • make help: Displays all available make commands and their descriptions.
  • make setup: Performs the full initial project setup (run once after cloning).
  • make install: Installs or updates Composer dependencies inside the Docker container.
  • make update: Updates Composer dependencies inside the Docker container.
  • make test: Runs the PHPUnit test suite inside the Docker container. (Also run automatically before commits).
  • make cs-check: Checks for code style violations using PHP CS Fixer inside the Docker container without applying fixes.
  • make cs-fix: Automatically formats the code according to PSR-12 standards using PHP CS Fixer inside the Docker container.
  • make grumphp: Manually runs all checks configured in GrumPHP (tests, code style, etc.) inside the Docker container. This simulates the pre-commit hook.
  • make build: Builds or updates the Docker development image using the cache.
  • make rebuild: Forces a rebuild of the Docker development image without using the cache (use this after changing the Dockerfile).

Pre-commit Hooks: Note that after running make setup, Git hooks are installed. These hooks will automatically run tasks like make test and make fix before each commit to ensure code quality and consistency. You can also run these checks manually using the commands listed above.

License

This project is licensed under the MIT License - see the LICENSE file for details.

abordji/php-generator-chunk 适用场景与选型建议

abordji/php-generator-chunk 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 04 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 abordji/php-generator-chunk 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-17