Service provider loading facility

Maintainers

Details

github.com/Nevay/spi

Source

Issues

Installs: 481

Dependents: 2

Suggesters: 1

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

Type:composer-plugin

v0.2.1 2024-05-14 20:03 UTC

This package is auto-updated.

Last update: 2024-05-14 20:04:09 UTC


README

Service provider loading facility, inspired by Javas ServiceLoader.

Install

composer require tbachert/spi

Usage

Registering service providers

Service provider implementations must provide a public zero-arguments constructor.

Registering via composer.json extra.spi

composer config --json --merge extra.spi.Namespace\\Service '["Namespace\\Implementation"]'

Registering via php

ServiceLoader::register('Namespace\Service', 'Namespace\Implementation');

Application authors

Make sure to allow the composer plugin to be able to load service providers.

composer config allow-plugins.tbachert/spi true

Loading service providers

foreach (ServiceLoader::load('Namespace\Service') as $provider) {
    // ...
}

Handling invalid service configurations

$loader = ServiceLoader::load('Namespace\Service');
for ($it = $loader->getIterator(); $it->valid(); $it->next()) {
    try {
        $provider = $it->current();
    } catch (ServiceConfigurationError) {}
}