Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/chubbyphp/chubbyphp-container

A minimal Dependency Injection Container (DIC) which implements PSR-11.
https://github.com/chubbyphp/chubbyphp-container

chubbyphp-container container psr-11 servicefactory

Last synced: 15 days ago
JSON representation

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

Awesome Lists containing this project

README

        

# chubbyphp-container

[![CI](https://github.com/chubbyphp/chubbyphp-container/actions/workflows/ci.yml/badge.svg)](https://github.com/chubbyphp/chubbyphp-container/actions/workflows/ci.yml)
[![Coverage Status](https://coveralls.io/repos/github/chubbyphp/chubbyphp-container/badge.svg?branch=master)](https://coveralls.io/github/chubbyphp/chubbyphp-container?branch=master)
[![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fchubbyphp%2Fchubbyphp-container%2Fmaster)](https://dashboard.stryker-mutator.io/reports/github.com/chubbyphp/chubbyphp-container/master)
[![Latest Stable Version](https://poser.pugx.org/chubbyphp/chubbyphp-container/v)](https://packagist.org/packages/chubbyphp/chubbyphp-container)
[![Total Downloads](https://poser.pugx.org/chubbyphp/chubbyphp-container/downloads)](https://packagist.org/packages/chubbyphp/chubbyphp-container)
[![Monthly Downloads](https://poser.pugx.org/chubbyphp/chubbyphp-container/d/monthly)](https://packagist.org/packages/chubbyphp/chubbyphp-container)

[![bugs](https://sonarcloud.io/api/project_badges/measure?project=chubbyphp_chubbyphp-container&metric=bugs)](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-container)
[![code_smells](https://sonarcloud.io/api/project_badges/measure?project=chubbyphp_chubbyphp-container&metric=code_smells)](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-container)
[![coverage](https://sonarcloud.io/api/project_badges/measure?project=chubbyphp_chubbyphp-container&metric=coverage)](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-container)
[![duplicated_lines_density](https://sonarcloud.io/api/project_badges/measure?project=chubbyphp_chubbyphp-container&metric=duplicated_lines_density)](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-container)
[![ncloc](https://sonarcloud.io/api/project_badges/measure?project=chubbyphp_chubbyphp-container&metric=ncloc)](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-container)
[![sqale_rating](https://sonarcloud.io/api/project_badges/measure?project=chubbyphp_chubbyphp-container&metric=sqale_rating)](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-container)
[![alert_status](https://sonarcloud.io/api/project_badges/measure?project=chubbyphp_chubbyphp-container&metric=alert_status)](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-container)
[![reliability_rating](https://sonarcloud.io/api/project_badges/measure?project=chubbyphp_chubbyphp-container&metric=reliability_rating)](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-container)
[![security_rating](https://sonarcloud.io/api/project_badges/measure?project=chubbyphp_chubbyphp-container&metric=security_rating)](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-container)
[![sqale_index](https://sonarcloud.io/api/project_badges/measure?project=chubbyphp_chubbyphp-container&metric=sqale_index)](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-container)
[![vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=chubbyphp_chubbyphp-container&metric=vulnerabilities)](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-container)

## Description

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

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

## Requirements

* php: ^8.2
* [psr/container][2]: ^2.0.2

## Installation

Through [Composer](http://getcomposer.org) as [chubbyphp/chubbyphp-container][1].

```sh
composer require chubbyphp/chubbyphp-container "^2.3"
```

## 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
factories([
MyService::class => static function (ContainerInterface $container): MyService {
return new MyService($container->get(LoggerInterface::class));
},
]);
```

#### Factory

```php
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
factory('key', new Parameter('value'));
```

#### Get

```php
get(MyService::class);
```

#### Has

```php
has(MyService::class);
```

### Container

All methods of the `MinimalContainer` and the following:

#### Prototype Factories

**each get will return a new instance**

```php
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
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

* [From Pimple][5]

## Copyright

2024 Dominik Zogg

[1]: https://packagist.org/packages/chubbyphp/chubbyphp-container
[2]: https://packagist.org/packages/psr/container
[3]: https://kocsismate.github.io/php-di-container-benchmarks/benchmark.html
[4]: https://github.com/chubbyphp/chubbyphp-laminas-config
[5]: doc/MigrateFromPimple.md