chubbyphp/chubbyphp-container

A simple PSR-11 container implementation.

Maintainers

👁 dominikzogg

Package info

github.com/chubbyphp/chubbyphp-container

pkg:composer/chubbyphp/chubbyphp-container

Statistics

Installs: 81 155

Dependents: 15

Suggesters: 1

Stars: 19

Open Issues: 0

2.5.0 2026-01-19 21:01 UTC

Requires

Suggests

None

Provides

None

Conflicts

None

Replaces

None

MIT e337620f8a80632d766a5bcc39d30b21eabef82d

  • Dominik Zogg <dominik.zogg.woop@gmail.com>

containerPSR-11chubbyphp

This package is auto-updated.

Last update: 2026-06-19 11:53:22 UTC


README

👁 CI
👁 Coverage Status
👁 Mutation testing badge
👁 Latest Stable Version
👁 Total Downloads
👁 Monthly Downloads

👁 bugs
👁 code_smells
👁 coverage
👁 duplicated_lines_density
👁 ncloc
👁 sqale_rating
👁 alert_status
👁 reliability_rating
👁 security_rating
👁 sqale_index
👁 vulnerabilities

Description

A minimal Dependency Injection Container (DIC) which implements PSR-11. DI Container Benchmark.

There is a laminas service manager adapter at chubbyphp/chubbyphp-laminas-config.

Requirements

Installation

Through Composer as chubbyphp/chubbyphp-container.

composer require chubbyphp/chubbyphp-container "^2.5"

Usage

There are two PSR-11 implementations:

  • Chubbyphp\Container\Container prototype (each get will return a new instance) and shared services
  • Chubbyphp\Container\MinimalContainer shared services

MinimalContainer / Container

Factories

<?php

use App\Service\MyService;
use Chubbyphp\Container\MinimalContainer;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

$container = new MinimalContainer();
$container->factories([
 MyService::class => static function (ContainerInterface $container): MyService {
 return new MyService($container->get(LoggerInterface::class));
 },
]);

Factory

<?php

use App\Service\MyService;
use Chubbyphp\Container\MinimalContainer;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

$container = new MinimalContainer();

// new
$container->factory(MyService::class, static function (ContainerInterface $container): MyService {
 return new MyService($container->get(LoggerInterface::class));
});

// existing (replace)
$container->factory(MyService::class, static function (ContainerInterface $container): MyService {
 return new MyService($container->get(LoggerInterface::class));
});

// existing (extend)
$container->factory(
 MyService::class,
 static function (ContainerInterface $container, callable $previous): MyService {
 $myService = $previous($container);
 $myService->setLogger($container->get(LoggerInterface::class));

 return $myService;
 }
);

Factory with Parameter

<?php

use Chubbyphp\Container\MinimalContainer;
use Chubbyphp\Container\Parameter;

$container = new MinimalContainer();
$container->factory('key', new Parameter('value'));

Get factory

<?php

use App\Service\MyService;
use Chubbyphp\Container\MinimalContainer;

$container = new MinimalContainer();

$myServiceFactory = $container->getFactory(MyService::class);

$myService = $myServiceFactory($container);

Get

<?php

use App\Service\MyService;
use Chubbyphp\Container\MinimalContainer;

$container = new MinimalContainer();

$myService = $container->get(MyService::class);

Has

<?php

use App\Service\MyService;
use Chubbyphp\Container\MinimalContainer;

$container = new MinimalContainer();
$container->has(MyService::class);

Container

All methods of the MinimalContainer and the following:

Prototype Factories

each get will return a new instance

<?php

use App\Service\MyService;
use Chubbyphp\Container\Container;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

$container = new Container();
$container->prototypeFactories([
 MyService::class => static function (ContainerInterface $container): MyService {
 return new MyService($container->get(LoggerInterface::class));
 },
]);

Prototype Factory

each get will return a new instance

<?php

use App\Service\MyService;
use Chubbyphp\Container\Container;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

$container = new Container();

// new
$container->prototypeFactory(
 MyService::class,
 static function (ContainerInterface $container): MyService {
 return new MyService($container->get(LoggerInterface::class));
 }
);

// existing (replace)
$container->prototypeFactory(
 MyService::class,
 static function (ContainerInterface $container): MyService {
 return new MyService($container->get(LoggerInterface::class));
 }
);

// existing (extend)
$container->prototypeFactory(
 MyService::class,
 static function (ContainerInterface $container, callable $previous): MyService {
 $myService = $previous($container);
 $myService->setLogger($container->get(LoggerInterface::class));

 return $myService;
 }
);

Migration

Copyright

2026 Dominik Zogg