Skip to content

Nevay/spi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Service Provider Interface

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) {}
}

About

Service Provider Interface

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages