duon/container

A PSR-11 compatible dependency injection library

Maintainers

Package info

github.com/duoncode/container

Homepage

pkg:composer/duon/container

Statistics

Installs: 108

Dependents: 1

Suggesters: 0

Stars: 0

Open Issues: 0

0.2.0 2026-02-21 18:07 UTC

This package is auto-updated.

Last update: 2026-04-24 20:38:05 UTC


README

Software License Codacy Badge Codacy Badge Psalm coverage Psalm level

A PSR-11 compatible dependency injection container.

Entry lifetimes

Container entries use explicit lifetimes:

  • shared() caches one instance per container (default for added entries)
  • scoped() caches one instance per requesting container scope
  • transient() never caches
  • value() returns the configured definition as-is (for literals or raw closures)
$container->add('config', ['debug' => true])->value();
$container->add(Service::class)->shared();
$container->add(RequestContext::class)->scoped();
$container->add(Builder::class)->transient();

Scope mode

Use scope() to create an isolated container for one unit of work:

$root = new Container();
$root->add('app-name', 'duon')->value();
$root->add('global-service', GlobalService::class)->shared();
$root->add('request-service', RequestService::class)->scoped();

$scope = $root->scope();
$scope->add(Request::class, $request)->value();

// ... resolve request-local services
$scope->reset();

After the first scope() call, the root container is sealed and no longer accepts structural mutations. Scope tags can inherit pre-defined root tags while keeping their own local caches.

Services that should be cleaned between scopes can implement Duon\Container\Resettable and will be reset during $scope->reset().

License

This project is licensed under the MIT license.