PHPackages                             dmt-software/di-plug - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. dmt-software/di-plug

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

dmt-software/di-plug
====================

An PHP dependency container abstraction

0.5.2(1mo ago)01893MITPHPPHP &gt;=8.4

Since Mar 13Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/dmt-software/di-plug)[ Packagist](https://packagist.org/packages/dmt-software/di-plug)[ RSS](/packages/dmt-software-di-plug/feed)WikiDiscussions master Synced today

READMEChangelog (10)Dependencies (24)Versions (14)Used By (3)

DI-Plug
=======

[](#di-plug)

Introduction
------------

[](#introduction)

[psr-11](https://www.php-fig.org/psr/psr-11/) introduced a standardized way for packages, libraries and frameworks to retrieve objects from containers. This package also allows adding dependencies to a container by a uniform interface without the use of a specific container implementation. This will help developers to access the dependency injection container to make installation of their packages easier.

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

[](#installation)

```
composer require dmt-software/di-plug
```

Usage
-----

[](#usage)

### Manual create the container

[](#manual-create-the-container)

```
use DMT\DependencyInjection\Adapters\PimpleAdapter;
use DMT\DependencyInjection\Container;
use Pimple\Container as PimpleContainer;

$container = new Container(new PimpleAdapter(new PimpleContainer()));
$container->set(SomeClass::class, fn() => new SomeClass($container->get(SomeDependency::class)));
```

### Autodetect

[](#autodetect)

Use the container builder to wrap your container instance within the `DMT\DependencyInjection\Container`.

```
use DMT\DependencyInjection\ContainerFactory;

/** @var object $supportedContainerInstance */
$factory = new ContainerFactory();
$container = $factory->createContainer($supportedContainerInstance);
$container->set(SomeClass::class, fn() => new SomeClass($container->get(SomeDependency::class)));
```

### Autodiscovery

[](#autodiscovery)

> NOTE: When another dependency uses a supported container, autodiscovery might end up using a different container than expected.

The code can discover the dependency container installed.

```
use DMT\DependencyInjection\ContainerFactory;

$factory = new ContainerFactory();
$container = $factory->createContainer();
$container->set(SomeClass::class, fn() => new SomeClass($container->get(SomeDependency::class)));
```

Concepts
--------

[](#concepts)

### Instances without registration

[](#instances-without-registration)

If the `Container::get()` is called with a className that is not present in the container will create a new instance for that class. This also counts for calls with made with constructor arguments.

callsetreturnsContainer::get(Some::class)falsenew instanceContainer::get(Some::class)truefrom dependencyContainer::get(Some::class, 12)truenew instance> NOTE: when the container creates a new instance it will NOT be set in the container. Not even when it is called with the same constructor arguments.

### Configuration Discovery

[](#configuration-discovery)

When ConfigurationInterface is implemented and available in the container, the constructor arguments can be injected from the configuration using the `ConfigValue` attribute.

```
use DMT\DependencyInjection\Attributes\ConfigValue;

class Foo
{
    public function __construct(
        #[ConfigValue('configKeyForBar')]
        public string $bar,
    )
}
```

The value for the bar in the constructor is fetched from configuration when it is omitted from the `container::get`call.

### Auto Inject Container

[](#auto-inject-container)

Instead of injection all the dependencies, one can choose to inject just the container and resolve the dependencies when needed. This can easily be achieved by using the `HasContainer` trait.

```
use DMT\DependencyInjection\Container;
use DMT\DependencyInjection\Traits\HasContainer;

class SomeClass
{
    use HasContainer;

    public function doSomething(): void
    {
        $dependency = $this->getContainer()->get(SomeDependency::class);
        $dependency->doSomething();
    }
}
```

This even works when the `HasContainer` is used within another trait, but just one level deep. Using the trait below f.i. enables `getTwigEngine()` call to retrieve the template engine in the parent class.

```
use DMT\DependencyInjection\Traits\HasContainer;
use Twig\Environment;

trait HasTwigEngine
{
    use HasContainer;

    public function getTwigEngine(): Environment
    {
        return $this->getContainer()->get(Environment::class);
    }
}
```

### ServiceProvider

[](#serviceprovider)

A service provider can be used to register or change dependencies in the container.

```
use DMT\DependencyInjection\Container;
use DMT\DependencyInjection\ServiceProviderInterface;
use Twig\Environment;
use Twig\Extension\StringLoaderExtension;

class MyServiceProvider implements ServiceProviderInterface
{
    /**
     * Register dependencies and ensure twig is enabled with string loader extension.
     */
    public function register(Container $container): void
    {
        if (!$container->has(Environment::class)) {
            $container->set(Environment::class, fn() => new Environment());
        }

        $env = $container->get(Environment::class);
        if (!$env->hasExtention(StringLoaderExtension::class)) {
            $env->addExtension(new StringLoaderExtension());
        }

        $container->set(MyClassInterface::class, fn() => new MyClass($container->get(Environment::class)));
    }
}
```

Supports
--------

[](#supports)

- [Aura dependency injection container](https://packagist.org/packages/aura/di)
- [Illuminate container](https://packagist.org/packages/illuminate/container)
- [League container](https://packagist.org/packages/league/container)
- [PHP-DI container](https://packagist.org/packages/php-di/php-di)
- [Pimple container](https://packagist.org/packages/pimple/pimple)

###  Health Score

45

—

FairBetter than 91% of packages

Maintenance89

Actively maintained with recent releases

Popularity10

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity60

Established project with proven stability

 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 ~96 days

Recently: every ~7 days

Total

13

Last Release

55d ago

PHP version history (2 changes)v0.1.0PHP &gt;=8.1

0.4.0PHP &gt;=8.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/ec6b7fbb4d231b5c2c76348cf3a13ec5e82c64a1826142f6ec0db1cf15db2873?d=identicon)[proggeler](/maintainers/proggeler)

---

Top Contributors

[![proggeler](https://avatars.githubusercontent.com/u/18281353?v=4)](https://github.com/proggeler "proggeler (39 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/dmt-software-di-plug/health.svg)

```
[![Health](https://phpackages.com/badges/dmt-software-di-plug/health.svg)](https://phpackages.com/packages/dmt-software-di-plug)
```

###  Alternatives

[symfony/dependency-injection

Allows you to standardize and centralize the way objects are constructed in your application

4.2k455.6M9.6k](/packages/symfony-dependency-injection)[illuminate/contracts

The Illuminate Contracts package.

706130.3M13.3k](/packages/illuminate-contracts)[illuminate/container

The Illuminate Container package.

31182.0M2.4k](/packages/illuminate-container)[ecotone/ecotone

Enterprise architecture layer for Laravel and Symfony — CQRS, Event Sourcing, Durable Workflows (Sagas, Orchestrators), Projections, and Outbox messaging via PHP attributes.

564576.7k51](/packages/ecotone-ecotone)[symfony/type-info

Extracts PHP types information.

20069.8M269](/packages/symfony-type-info)[civicrm/civicrm-core

Open source constituent relationship management for non-profits, NGOs and advocacy organizations.

751291.4k43](/packages/civicrm-civicrm-core)

PHPackages © 2026

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