ServiceProviderContainer

Class
implements ContainerInterface
Final: Yes

Implements a PSR-11 compliant dependency injection container using a container-interop/service-provider.

Description

This container SHALL resolve services by delegating to the factories and extensions defined in the provided ServiceProviderInterface instance.

Services are lazily instantiated on first request and cached for subsequent retrieval, enforcing singleton-like behavior within the container scope.

The container supports service extension mechanisms by allowing callable extensions to modify or enhance services after construction, based on the service identifier or its concrete class name.

If an optional wrapper container is provided, it SHALL be passed to service factories and extensions, allowing for delegation or decoration of service resolution. If omitted, the container defaults to itself.

Table of Contents

Interfaces

ContainerInterface

Extends the PSR-11 ContainerInterface to provide a consistent, domain-specific container interface for the FastForward ecosystem.

Properties

$cache

Cache of resolved services keyed by their identifier or class name.

 : array<string, mixed>
 : ServiceProviderInterface
$wrapperContainer

The container instance used for service resolution and extension application.

 : ContainerInterface

Methods

__construct()

Constructs a new ServiceProviderContainer instance.

 : mixed
get()

Retrieves a service from the container by its identifier.

 : mixed
has()

Determines if the container can return an entry for the given identifier.

 : bool
applyServiceExtensions()

Applies service extensions to the constructed service instance.

 : void
Properties

$cache

Private

Cache of resolved services keyed by their identifier or class name.

private array<string, mixed> $cache

Description

This array SHALL store already constructed services to enforce singleton-like behavior within the container scope.

$wrapperContainer

Private Read-only

The container instance used for service resolution and extension application.

private ContainerInterface $wrapperContainer

Description

This property MAY reference another container for delegation, or default to this container instance.

Methods

__construct()

Public

Constructs a new ServiceProviderContainer instance.

public __construct(ServiceProviderInterface  $serviceProvider[, ContainerInterface|null  $wrapperContainer = null]) : mixed

Description

This constructor SHALL initialize the container with a service provider and an optional delegating container. If no wrapper container is provided, the container SHALL delegate to itself.

Parameters
$serviceProvider : ServiceProviderInterface

Description

the service provider supplying factories and extensions

$wrapperContainer : ContainerInterface|null = null

Description

An optional container for delegation. Defaults to self.

get()

Public

Retrieves a service from the container by its identifier.

public get( string  $id) : mixed

Description

This method SHALL return a cached instance if available, otherwise it resolves the service using the factory provided by the service provider.

If the service has a corresponding extension, it SHALL be applied post-construction.

Parameters
$id : string

Description

the identifier of the service to retrieve

Tags
throws

Description

if no factory exists for the given identifier

throws

Description

if service construction fails due to container errors

Return values

Description

the service instance associated with the identifier

has()

Public

Determines if the container can return an entry for the given identifier.

public has( string  $id) : bool

Description

This method MUST return true if the entry exists in the cache or factories, false otherwise.

Parameters
$id : string

Description

identifier of the entry to look for

Return values
bool

Description

true if the entry exists, false otherwise

applyServiceExtensions()

Private

Applies service extensions to the constructed service instance.

private applyServiceExtensions( string  $id, string  $class, mixed  $service) : void

Description

This method SHALL inspect the set of extensions returned by the service provider, checking both the original service identifier and the concrete class name of the service instance. If a corresponding extension is found, it MUST be a callable and SHALL be invoked with the container and service instance as arguments.

Extensions MAY be used to modify or enhance services after creation. Invalid extensions (non-callables) SHALL be ignored silently.

Parameters
$id : string

Description

the identifier of the resolved service

$class : string

Description

the fully qualified class name of the service

$service : mixed

Description

the service instance to apply extensions to

Tags
throws

Description

if an extension callable fails during execution