function ServiceLoaderIterator::current
File
-
vendor/
tbachert/ spi/ src/ ServiceLoaderIterator.php, line 39
Class
- ServiceLoaderIterator
- @template-covariant S of object service type @implements Iterator<class-string, S>
Namespace
Nevay\SPICode
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);
}
}