Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. ServiceLoaderIterator.php

class ServiceLoaderIterator

@template-covariant S of object service type @implements Iterator<class-string, S>

@internal

Hierarchy

  • class \Nevay\SPI\ServiceLoaderIterator implements \Iterator

Expanded class hierarchy of ServiceLoaderIterator

File

vendor/tbachert/spi/src/ServiceLoaderIterator.php, line 16

Namespace

Nevay\SPI
View source
final class ServiceLoaderIterator implements Iterator {
    
    /** @var class-string<S> */
    private readonly string $service;
    
    /** @var list<class-string> */
    private readonly array $providers;
    
    /** @var array<int, S|false> */
    private array $cache;
    
    /** @var int<0, max> */
    private int $index = 0;
    
    /**
     * @param class-string<S> $service
     * @param list<class-string> $providers
     * @param array<int, S|false> $cache
     */
    public function __construct(string $service, array $providers, array &$cache) {
        $this->service = $service;
        $this->providers = $providers;
        $this->cache =& $cache;
    }
    public function current() : ?object {
        $index = $this->index;
        if ($instance = $this->cache[$index] ?? null) {
            return $instance;
        }
        if (($class = $this->providers[$index] ?? null) === null) {
            return null;
        }
        $this->cache[$index] = false;
        if (!is_subclass_of($class, $this->service)) {
            throw new ServiceConfigurationError(sprintf('Invalid service provider, expected implementation of "%s", got "%s"', $this->service, $class));
        }
        try {
            return $this->cache[$index] = new $class();
        } catch (Throwable $e) {
            throw new ServiceConfigurationError(sprintf('Invalid service provider, failed to instantiate "%s" provider "%s": %s', $this->service, $class, $e->getMessage()), previous: $e);
        }
    }
    public function next() : void {
        $this->index++;
        $this->skipInvalid();
    }
    public function key() : ?string {
        return $this->providers[$this->index] ?? null;
    }
    public function valid() : bool {
        return $this->index < count($this->providers);
    }
    public function rewind() : void {
        $this->index = 0;
        $this->skipInvalid();
    }
    private function skipInvalid() : void {
        while (($this->cache[$this->index] ?? null) === false) {
            $this->index++;
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary
ServiceLoaderIterator::$cache private property @var array&lt;int, S|false&gt;
ServiceLoaderIterator::$index private property @var int&lt;0, max&gt;
ServiceLoaderIterator::$providers private property @var list&lt;class-string&gt;
ServiceLoaderIterator::$service private property @var class-string&lt;S&gt;
ServiceLoaderIterator::current public function
ServiceLoaderIterator::key public function
ServiceLoaderIterator::next public function
ServiceLoaderIterator::rewind public function
ServiceLoaderIterator::skipInvalid private function
ServiceLoaderIterator::valid public function
ServiceLoaderIterator::__construct public function

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal