Drupal\Component\Plugin\LazyPluginCollection::getIterator requires to return an ArrayIterator, making it impossible to use resource efficient generators

Created on 21 June 2024, 11 months ago

Problem/Motivation

Since Drupal 10, I have a derived class implementation from Drupal\Component\Plugin\LazyPluginCollection. It is using a generator for implementing Drupal\Component\Plugin\LazyPluginCollection::getIterator like the following:

<?php
  #[\ReturnTypeWillChange]
  public function getIterator() {
    $empty = TRUE;
    foreach ($this->getInstanceIds() as $instance_id) {
      yield $instance_id => $this->get($instance_id);
      $empty = FALSE;
    }

    if ($empty) {
      return new \EmptyIterator();
    }
  }
?>

Reason I do is that plugin instantiation is expensive and the collection may hold many of them. So I'm using a generator that only loads the plugin instance when required, and it may be that the loop stops before touching all of the contained plugin IDs.

Now since D11 that method required to reurn an \ArrayIterator, making it impossible to use this approach. When I switch to an \ArrayIterator, that would mean all plugin instances of the collection need to be instantiated at once when the first instance is required to be loaded.

Steps to reproduce

Proposed resolution

Please consider a way to allow using generators again. I also don't know why exactly one type of iterator is required at that point. using an interface or something else would allow more flexibility.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Active

Version

11.0 🔥

Component
Base 

Last updated about 9 hours ago

Created by

🇩🇪Germany mxh Offenburg

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024