PHPackages                             ricotta/container - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. ricotta/container

ActiveLibrary[Framework](/categories/framework)

ricotta/container
=================

Dependency Injection Container for the Ricotta project

0.1.2(1y ago)0221MITPHPPHP &gt;=8.4CI passing

Since Jan 18Pushed 11mo ago1 watchersCompare

[ Source](https://github.com/thomasnordahl-dk/container)[ Packagist](https://packagist.org/packages/ricotta/container)[ RSS](/packages/ricotta-container/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)Dependencies (4)Versions (4)Used By (1)

`ricotta/container`
===================

[](#ricottacontainer)

**Dependency Injection Container** for the Ricotta project.

Installation
------------

[](#installation)

The package can be installed via composer.

```
composer require ricotta/container
```

Usage
-----

[](#usage)

### Lifecycle

[](#lifecycle)

The Ricotta is created by passing a `Bootstrapping` object to the `Container`constructor.

```
use Ricotta\Container\Bootstrapping;
use Ricotta\Container\Container;

$bootstrap = new Ricotta\Container\Bootstrapping();

$container = new Ricotta\Container\Container($bootstrap);
```

The `Bootstrapping` object is used to register components and configure the container.

Once the container is created, any changes to the `Bootstrapping` object will not be reflected in the container.

This secures a consistent state of the container and prevents changes to the container after it is created.

### The `Bootstrapping` object

[](#the-bootstrapping-object)

The `Bootstrapping` object is used to register components and configure the container.

To define a component, use the `Bootstrapping` object as an array, where the key is the component name.

```
$boostrap = new Ricotta\Container\Bootstrapping();

$bootstrap[MyService::class]->register();
$bootstrap[MyService::class]->configure(fn (MyService $service) => $service->setConfig('value'));

$container = new Ricotta\Container\Container($bootstrap);

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

Registering components by their type as their type is the simplest way to register a component.

The container will automatically resolve dependencies by their type whenever possible.

Custom bootstrapping is done through the returned `Registration` object through a declarative API.

```
$bootstrap[MyService::class]
    ->register()
    ->callback(fn (MyRepository $repository) => new Service($repository))
    ->arguments([
        MyRepository::class => Bootstrapping['my.repository']->reference()
    ]);

$bootstrap[MyService::class]
    ->configure(fn (MyService $service) => $service->setConfig('value'));
$bootstrap[MyService::class]
    ->replace(fn (MyService $service) => new MyServiceDecorator($service));

$container = new Ricotta\Container\Container($bootstrap);

// Returns a MyServiceDecorator instance with MyService instance injected.
$service = $container->get(MyService::class);
```

### Registering Components

[](#registering-components)

All components should be registered with the `Bootstrapping` object. Autowiring is not enabled by default, but may be enabled for specific subtypes. See `Boostrapping::allowAutowiring()`.

#### Registering by Type

[](#registering-by-type)

Components can be registered by their type. The container will automatically resolve dependencies by their type.

```
$bootstrap[MyService::class]->register();
```

#### Registering by Name

[](#registering-by-name)

Components can also be registered by a custom name.

```
$bootstrap['my.service']->register()->type(MyService::class);
```

#### Registering with a Callback

[](#registering-with-a-callback)

Components can be registered with a callback to create the component.

```
$bootstrap[MyService::class]->register();
$bootstrap[MyRepository::class]
    ->register()
    ->callback(
        fn (MyService $service) => new MyRepository($service, 'my_table')
    );
```

#### Registering with a type

[](#registering-with-a-type)

Components can be registered with a type, which is handy for dependency inversion patterns.

```
$bootstrap[MyServiceInterface::class]
    ->register()
    ->type(MyService::class);

$container = new Ricotta\Container\Container($bootstrap);

// Returns an instance of MyService.
$service = $container->get(MyServiceInterface::class);
```

#### Registering with argument map.

[](#registering-with-argument-map)

For components with dependencies that cannot be resolved by type, an argument map can be used.

Values can be injected directly or by using a bootstrapping reference to a component name.

References will not be resolved until the component is created.

```
$bootstrap['my.service']->register()->type(MyService::class);
$bootstrap[MyRepository::class]
    ->register()
    ->callback(function (MyService $service, string $table) {
        return new MyRepository($service, 'my_table')
    })
    ->arguments([
        'table' => 'my_table',
        'service' => Bootstrapping['my.service']->reference()
    ]);
```

The arguments can also be used to inject into the constructor when no callback is provided.

```
$bootstrap['my.service']
    ->register()
    ->type(MyService::class);
$bootstrap[MyRepository::class]
    ->register()
    ->arguments([
        'table' => 'my_table',
        MyService::class => Bootstrapping['my.service']->reference()
    ]);
```

### Alias names

[](#alias-names)

Components can be aliased to a different name by using a callback method like this:

```
$bootstrap[MyService::class]->register();
$bootstrap[MyServiceInterface::class]
    ->register()
    ->callback(fn (MyService $service) => $service);

$container = new Ricotta\Container\Container($bootstrap);

// Returns an instance of MyService.
$service = $container->get(MyServiceInterface::class);
// Reference to the same instance.
$service = $container->get(MyService::class);
```

### Configuring Components

[](#configuring-components)

Components can be configured after they are created by using the `configure`method.

```
$bootstrap[MyService::class]->register();
$bootstrap[MyService::class]
    ->configure(fn (MyService $service) => $service->setConfig('value'));
$bootstrap[MyService::class]
    ->configure(fn (MyService $service) => $service->setConfig('value 2'));

$container = new Ricotta\Container\Container($bootstrap);

$service = $container->get(MyService::class);
echo $service->getConfig(); // Outputs 'value 2'.
```

### Replacing Components

[](#replacing-components)

Components can be replaced by using the `replace` method.

```
$bootstrap[MyService::class]->register();
$bootstrap[MyService::class]
    ->replace(fn (MyService $service) => new MyServiceDecorator($service));

$container = new Ricotta\Container\Container($bootstrap);

$service = $container->get(MyService::class); // Returns an instance of MyServiceDecorator.
```

### Order of `configure()` and `replace()` calls

[](#order-of-configure-and-replace-calls)

The order of `configure()` and `replace()` calls is important. The `configure()`callbacks and `replace()` callbacks are executed in the order they are registered.

```
$bootstrap[MyService::class]->register();

// Executed first.
$bootstrap[MyService::class]
    ->configure(fn (MyService $service) => $service->setConfig('value'));

// Executed second.
$bootstrap[MyService::class]
    ->replace(fn (MyService $service) => new MyServiceDecorator($service));

// Executed third.
$bootstrap[MyService::class]
    ->configure(fn (MyService $service) => $service->setConfig('value 2'));

$container = new Ricotta\Container\Container($bootstrap);

// Returns an instance of MyServiceDecorator.
$service = $container->get(MyService::class);
// Outputs 'value 2'.
echo $service->getConfig();
```

### Autowiring

[](#autowiring)

Autowiring can be enabled for specific subtypes by using the `allowAutowiring`method.

```
class Controller implements ControllerInterface
{
    // ...
}

$bootstrap = new Ricotta\Container\Bootstrapping();

$bootstrap->allowAutowiring(ControllerInterface::class);

$container = new Ricotta\Container\Container($bootstrap);

// Because autowiring is enabled for ControllerInterface, the container will
// automatically resolve the Controller class without the need to register it.
// This is useful for usage that allows autowiring of certain types.

$controller = $container->get(Controller::class);
```

### Bootstrapping References

[](#bootstrapping-references)

Bootstrapping references can be used to reference components that are not yet created.

The reference will not be resolved before it is needed.

```
$bootstrap[MyService::class]->register();
$bootstrap[MyService::class]
    ->configure(fn (MyService $service) => $service->setConfig('value'));

$bootstrap[MyRepository::class]
    ->register()
    ->arguments(['parameterName' => $bootstrap[MyService::class]->reference()]);
]);
```

What's Not Here
---------------

[](#whats-not-here)

- The container does not support circular dependencies.
- The container does not support autowiring by default.

Modularity support is not added. Creating a modular system is up to the user. A framework could define a module system that uses the container to create modules.

```
interface Module
{
    // Register components and configure the container for the module.
    public function register(Bootstrapping $bootstrap): void;
}
```

License
-------

[](#license)

The Ricotta project is open-sourced software licensed under the [MIT license](/LICENSE).

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance48

Moderate activity, may be stable

Popularity6

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~20 days

Total

3

Last Release

445d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ec1a54f85a6e46909df4ca4852ada0cc4b1482f7208dfc487d05554087e09cc9?d=identicon)[thomasnordahl-dk](/maintainers/thomasnordahl-dk)

---

Top Contributors

[![thomasnordahl-dk](https://avatars.githubusercontent.com/u/5709900?v=4)](https://github.com/thomasnordahl-dk "thomasnordahl-dk (5 commits)")

###  Code Quality

TestsCodeception

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/ricotta-container/health.svg)

```
[![Health](https://phpackages.com/badges/ricotta-container/health.svg)](https://phpackages.com/packages/ricotta-container)
```

###  Alternatives

[cakephp/cakephp

The CakePHP framework

8.8k18.5M1.6k](/packages/cakephp-cakephp)[silverstripe/framework

The SilverStripe framework

7213.5M2.5k](/packages/silverstripe-framework)[drupal/core-recommended

Locked core dependencies; require this project INSTEAD OF drupal/core.

6939.5M343](/packages/drupal-core-recommended)[cakephp/core

CakePHP Framework Core classes

6126.8M39](/packages/cakephp-core)[contao/core-bundle

Contao Open Source CMS

1231.6M2.4k](/packages/contao-core-bundle)[neos/flow

Flow Application Framework

862.0M451](/packages/neos-flow)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
