Search by

debuss-a / piccolo-container

debuss-a

A PSR-11 dependency injection container with singleton, scoped and transient lifetimes, made for long-running PHP workers (FrankenPHP, RoadRunner, ...).

Package info

github.com/debuss/piccolo-container

pkg:composer/debuss-a/piccolo-container

Statistics

Installs: 83

Dependents: 1

Suggesters: 0

Stars: 0

Open Issues: 0

0.2.1 2026-11-27 21:05 UTC

This package is auto-updated.

Last update: 2026-11-27 21:06:41 UTC


README

A small PSR-11 dependency injection container with three lifetimes, as in Microsoft.Extensions.DependencyInjection:

Lifetime Instances Typical services
Singleton one for the whole container configuration, logger, router, HTTP factories
Scoped one per scope (per request) unit of work, current user, connection in a transaction
Transient a new one on each resolution request handlers, middlewares, light services

It is made for long-running PHP workers (FrankenPHP, RoadRunner, ...): the container is built once, each request gets its own scope, and nothing of a request leaks to the next one.

composer require debuss-a/piccolo-container

Usage

use Piccolo\Container\ContainerBuilder;

$builder = new ContainerBuilder();

$builder->singleton(LoggerInterface::class, static fn (Config $config) => new Logger($config->get('LOGGER_NAME')));
$builder->scoped(UnitOfWork::class);
$builder->transient(PostClientInterface::class, PostClient::class);
$builder->instance(Config::class, $config);
$builder->alias(RouterInterface::class, FastRouteRouter::class);

$container = $builder->build();

// For each request
$scope = $container->createScope();
try {
    $scope->get(PostHandler::class)->handle($request);
} finally {
    $scope->dispose();
}

A definition is either:

  • null: the class named by the identifier is autowired;
  • a class name: that class is autowired (or, if it is the identifier of another service, that service is resolved);
  • a closure: a factory whose parameters are autowired.

The classes that are not registered are autowired as transient services.

Autowiring

The parameters of the constructors and factories are resolved in this order:

  1. a registered service for a class or interface type;
  2. the default value of the parameter;
  3. an autowired instance for an instantiable class type;
  4. null for a nullable parameter.

Union and intersection types, and builtin types without a default value, cannot be resolved. How to create each service (the arguments of its constructor or factory, and the inflectors of its class) is worked out once, the first time it is resolved: in a worker, the next resolutions only create the instances, without reflection.

Special identifiers: ContainerInterface is the scope the service is resolved from (or the root container), ScopeInterface the current scope, Container and ScopeFactory the root container.

Scope validation

A scoped service resolved from the root container, or as a dependency of a singleton (a "captive dependency", which would keep the instance of the first request forever), throws a ScopeException with the resolution chain:

The scoped service "UnitOfWork" cannot be resolved outside a scope (from the root container, or as a dependency of a
singleton) (resolving PostRepository → UnitOfWork)

Service providers

final class LoggerServiceProvider implements ServiceProvider
{
    public function register(ContainerBuilder $builder): void
    {
        $builder->singleton(Logger::class, static fn (Config $config) => new Logger(...));
        $builder->alias(LoggerInterface::class, Logger::class);
    }
}

$builder->addProvider(new LoggerServiceProvider());

Providers are registered right away: they only store definitions, the services are still created on their first resolution. A service registered again replaces the previous definition, so an application can override the definitions of a provider.

Inflectors

Called with each new instance of a class or interface, whatever its lifetime, e.g. to inject optional dependencies:

$builder->inflect(
    LoggerAwareInterface::class,
    static fn (LoggerAwareInterface $service, ContainerInterface $container) => $service->setLogger($container->get(LoggerInterface::class))
);

Disposing and resetting

  • The scoped and transient instances implementing Disposable that a scope created are disposed with the scope, newest first.
  • Container::reset(), called by a worker after each request, resets the singletons already created that implement Resettable, and calls the hooks registered with $builder->onReset(Logger::class, fn (Logger $l) => $l->reset()).

Introspection

Container::definitions() gives the registered services (Definition: id, lifetime, concrete, hasInstance, instance), aliases() the aliases to the service they end on, and inflectedTypes() the types having inflectors, e.g. to list the services or to check that they can all be resolved before a deployment.

Tests

composer test     # Pest
composer analyse  # PHPStan, level max

The container is typed for PHPStan: get(Foo::class) is a Foo, and the callbacks of inflect() and onReset() get an instance of the given class (tests/Types checks these types).