PHPackages                             alanvdb/dependency-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. [PSR &amp; Standards](/categories/psr-standards)
4. /
5. alanvdb/dependency-container

ActiveLibrary[PSR &amp; Standards](/categories/psr-standards)

alanvdb/dependency-container
============================

Basic PSR-11 dependency containers

v0.1(1y ago)014MITPHP

Since Aug 13Pushed 1y ago1 watchersCompare

[ Source](https://github.com/Alanvdb/dependency-container)[ Packagist](https://packagist.org/packages/alanvdb/dependency-container)[ RSS](/packages/alanvdb-dependency-container/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (2)Versions (2)Used By (0)

Dependency Container
====================

[](#dependency-container)

A PHP PSR-11 Dependency Injection System

Overview
--------

[](#overview)

This project provides a lightweight dependency injection system for PHP. It includes the following main components:

1. `LazyContainer`: A container that lazily instantiates services.
2. `IterableLazyContainer`: An extension of `LazyContainer` that implements the `Iterator` interface.
3. `ContainerFactory`: A factory to create instances of `LazyContainer` and `IterableLazyContainer`.

This library is fully compatible with the PSR-11 standard, which defines a common interface for dependency injection containers. This ensures that your dependency injection containers are interoperable with other PSR-11 compliant libraries.

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

[](#installation)

To install the dependency-container, you can use Composer:

```
composer require alanvdb/dependency-container
```

Usage
-----

[](#usage)

### LazyContainer

[](#lazycontainer)

The `LazyContainer` class allows you to register services with a callable that will be invoked only when the service is requested. This provides lazy instantiation of services, which can improve performance by deferring the creation of services until they are actually needed.

#### Example

[](#example)

```
use AlanVdb\Dependency\LazyContainer;

$container = new LazyContainer();

$container->add('service1', function() {
    return new Service1();
});

if ($container->has('service1')) {
    $service1 = $container->get('service1');
}
```

### IterableLazyContainer

[](#iterablelazycontainer)

The `IterableLazyContainer` extends `LazyContainer` and implements the `Iterator` interface, allowing you to iterate over the registered services.

#### Example

[](#example-1)

```
use AlanVdb\Dependency\IterableLazyContainer;

$container = new IterableLazyContainer();

$container->add('service1', function() {
    return new Service1();
});

$container->add('service2', function() {
    return new Service2();
});

foreach ($container as $serviceId => $service) {
    // Process each service
}
```

### ContainerFactory

[](#containerfactory)

The `ContainerFactory` class provides methods to create instances of `LazyContainer` and `IterableLazyContainer`.

#### Example

[](#example-2)

```
use AlanVdb\Dependency\Factory\ContainerFactory;

$factory = new ContainerFactory();

$lazyContainer = $factory->createLazyContainer();
$iterableLazyContainer = $factory->createIterableLazyContainer();
```

PSR-11 Compatibility
--------------------

[](#psr-11-compatibility)

This project adheres to the [PSR-11 Container Interface](https://www.php-fig.org/psr/psr-11/), ensuring that the `LazyContainer` and `IterableLazyContainer` classes conform to the standard. This allows for seamless integration with other libraries and frameworks that support PSR-11.

### Key PSR-11 Methods

[](#key-psr-11-methods)

#### `LazyContainer` and `IterableLazyContainer`

[](#lazycontainer-and-iterablelazycontainer)

These classes implement the following PSR-11 methods:

- `get(string $id)`: Retrieves an entry from the container by its identifier.
    - If the identifier is not found, an exception implementing `Psr\Container\NotFoundExceptionInterface` is thrown.
- `has(string $id): bool`: Returns true if the container can return an entry for the given identifier.

By adhering to PSR-11, these containers can be used wherever a PSR-11 compliant container is expected.

API Documentation
-----------------

[](#api-documentation)

### LazyContainer

[](#lazycontainer-1)

#### Methods

[](#methods)

- `add(string $id, callable $generator)`: Adds a new service to the container.

    - `string $id`: The service identifier.
    - `callable $generator`: A callable that returns the service instance.
- `get(string $id)`: Retrieves a service from the container.

    - `string $id`: The service identifier.
    - Returns the service instance.
    - Throws `IdNotFoundException` if the service identifier is not found.
- `has(string $id): bool`: Checks if a service identifier exists in the container.

    - `string $id`: The service identifier.
    - Returns `true` if the service exists, `false` otherwise.

### IterableLazyContainer

[](#iterablelazycontainer-1)

The `IterableLazyContainer` inherits all methods from `LazyContainer` and implements additional methods from the `Iterator` interface:

- `current()`: Returns the current element.
- `key()`: Returns the key of the current element.
- `next()`: Moves forward to the next element.
- `rewind()`: Rewinds back to the first element.
- `valid()`: Checks if the current position is valid.

### ContainerFactory

[](#containerfactory-1)

#### Methods

[](#methods-1)

- `createLazyContainer(): LazyContainerInterface`: Creates a new instance of `LazyContainer`.
- `createIterableLazyContainer(): LazyContainerInterface & Iterator`: Creates a new instance of `IterableLazyContainer`.

Testing
-------

[](#testing)

To run the tests, use the following command:

```
vendor/bin/phpunit
```

The tests are located in the `tests` directory and cover the functionality of `LazyContainer`, `IterableLazyContainer`, and `ContainerFactory`.

License
-------

[](#license)

This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance34

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity29

Early-stage or recently created project

 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

Unknown

Total

1

Last Release

642d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/79d5351bc1e25eb96224d5dd1bb67f5a60578d968784acf5f2c1edb897247b38?d=identicon)[alanvdb](/maintainers/alanvdb)

---

Top Contributors

[![Alanvdb](https://avatars.githubusercontent.com/u/35227520?v=4)](https://github.com/Alanvdb "Alanvdb (2 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[pimple/pimple

Pimple, a simple Dependency Injection Container

2.7k130.5M1.4k](/packages/pimple-pimple)[league/container

A fast and intuitive dependency injection container.

86387.8M343](/packages/league-container)[lctrs/psalm-psr-container-plugin

Let Psalm understand better psr11 containers

17648.1k13](/packages/lctrs-psalm-psr-container-plugin)[phpwatch/simple-container

A fast and minimal PSR-11 compatible Dependency Injection Container with array-syntax and without auto-wiring

1810.1k2](/packages/phpwatch-simple-container)

PHPackages © 2026

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