Skip to content

ioc-interop/impl

Repository files navigation

ioc-interop/impl

PDS Skeleton PDS Composer Script Names

Reference implementations of the Ioc-Interop interfaces for PHP 8.4+.

Installation

composer require ioc-interop/impl

Usage

Compose a container by populating a IocContainerFactory with shared instances and service factories, then ask it for a new container:

use IocInterop\Impl\ContainerFactory;
use IocInterop\Interface\IocContainer;

$containerFactory = new ContainerFactory();

$containerFactory->instances = [
    PDO::class => new PDO('sqlite::memory:'),
];

$containerFactory->factories = [
    Logger::class => fn (IocContainer $ioc) => new Logger(),
    UserService::class => fn (IocContainer $ioc) => new UserService(
        $ioc->getService(PDO::class),
        $ioc->getService(Logger::class),
    ),
];

$ioc = $containerFactory->newContainer();

Retrieve services by name. The first call to getService() resolves and shares the instance for subsequent calls:

$logger = $ioc->getService(Logger::class);
$users = $ioc->getService(UserService::class);

Check for service availability:

if ($ioc->hasService(Logger::class)) {
    // ...
}

Requesting a service that is not registered throws a ContainerException:

use IocInterop\Interface\IocThrowable;

try {
    $ioc->getService('does-not-exist');
} catch (IocThrowable $e) {
    // ...
}

Classes

Interface Implementation
IocContainer Container
IocContainerFactory ContainerFactory
IocThrowable ContainerException

All classes are in the IocInterop\Impl namespace.

See the Ioc-Interop interface package for the full specification.

About

Reference implementations of the Ioc-Interop interfaces.

Resources

License

Contributing

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages