PHPackages                             codenamephp/platform.di - 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. codenamephp/platform.di

ActiveLibrary[Framework](/categories/framework)

codenamephp/platform.di
=======================

Simple dependency injection container based on www.php-di.org

5.2.0(5y ago)032.6k↓34.6%[1 PRs](https://github.com/codenamephp/platform.di/pulls)5Apache-2.0PHPPHP ^7.4 || ^8.0

Since Jan 30Pushed 3y ago1 watchersCompare

[ Source](https://github.com/codenamephp/platform.di)[ Packagist](https://packagist.org/packages/codenamephp/platform.di)[ RSS](/packages/codenamephp-platformdi/feed)WikiDiscussions release Synced 1mo ago

READMEChangelog (10)Dependencies (7)Versions (20)Used By (5)

platform.di
===========

[](#platformdi)

[![Packagist Version](https://camo.githubusercontent.com/181e8be9ad89d09a638ff45c4a11aaff91732cc70190ba422b36bbfad569d751/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f636f64656e616d657068702f706c6174666f726d2e6469)](https://camo.githubusercontent.com/181e8be9ad89d09a638ff45c4a11aaff91732cc70190ba422b36bbfad569d751/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f636f64656e616d657068702f706c6174666f726d2e6469)[![Packagist PHP Version Support](https://camo.githubusercontent.com/4db0f55b5e90464afe24ad48603b4f5aca1e4bf5c5c34d2f203db31f7fcb8352/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f636f64656e616d657068702f706c6174666f726d2e6469)](https://camo.githubusercontent.com/4db0f55b5e90464afe24ad48603b4f5aca1e4bf5c5c34d2f203db31f7fcb8352/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f636f64656e616d657068702f706c6174666f726d2e6469)[![Lines of code](https://camo.githubusercontent.com/c8d7ab8b8d3a3862dd9b2398ba02e118532a7bba8c1fa0b4007156dad20c99b4/68747470733a2f2f696d672e736869656c64732e696f2f746f6b65692f6c696e65732f6769746875622f636f64656e616d657068702f706c6174666f726d2e6469)](https://camo.githubusercontent.com/c8d7ab8b8d3a3862dd9b2398ba02e118532a7bba8c1fa0b4007156dad20c99b4/68747470733a2f2f696d672e736869656c64732e696f2f746f6b65692f6c696e65732f6769746875622f636f64656e616d657068702f706c6174666f726d2e6469)[![GitHub code size in bytes](https://camo.githubusercontent.com/af346eb55693f8d61c92c12189e5acba8b889516409d316123445650a6dd6bea/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c616e6775616765732f636f64652d73697a652f636f64656e616d657068702f706c6174666f726d2e6469)](https://camo.githubusercontent.com/af346eb55693f8d61c92c12189e5acba8b889516409d316123445650a6dd6bea/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c616e6775616765732f636f64652d73697a652f636f64656e616d657068702f706c6174666f726d2e6469)[![CI](https://github.com/codenamephp/platform.di/workflows/CI/badge.svg)](https://github.com/codenamephp/platform.di/workflows/CI/badge.svg)[![Mutation testing badge](https://camo.githubusercontent.com/12a6c407273a948b871417e9ec22edcaf0cdfca71b292fa87d3d032ba104b817/68747470733a2f2f696d672e736869656c64732e696f2f656e64706f696e743f7374796c653d666c61742675726c3d687474707325334125324625324662616467652d6170692e737472796b65722d6d757461746f722e696f2532466769746875622e636f6d253246636f64656e616d65706870253246706c6174666f726d2e64692532466d6173746572)](https://camo.githubusercontent.com/12a6c407273a948b871417e9ec22edcaf0cdfca71b292fa87d3d032ba104b817/68747470733a2f2f696d672e736869656c64732e696f2f656e64706f696e743f7374796c653d666c61742675726c3d687474707325334125324625324662616467652d6170692e737472796b65722d6d757461746f722e696f2532466769746875622e636f6d253246636f64656e616d65706870253246706c6174666f726d2e64692532466d6173746572)[![Packagist Downloads](https://camo.githubusercontent.com/7800feaca72136af851f15358737237791d36fc9ab6b280a8f4f83c9067d3c2f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f636f64656e616d657068702f706c6174666f726d2e6469)](https://camo.githubusercontent.com/7800feaca72136af851f15358737237791d36fc9ab6b280a8f4f83c9067d3c2f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f636f64656e616d657068702f706c6174666f726d2e6469)[![GitHub](https://camo.githubusercontent.com/e9e4843312fb8ba4b58dd494ed09d386983bdef8d5b29d97a1525b1a9ddf56a9/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f636f64656e616d657068702f706c6174666f726d2e6469)](https://camo.githubusercontent.com/e9e4843312fb8ba4b58dd494ed09d386983bdef8d5b29d97a1525b1a9ddf56a9/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f636f64656e616d657068702f706c6174666f726d2e6469)

Simple dependency injection container based on [www.php-di.org](http://www.php-di.org)

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

[](#installation)

Easiest way is via composer. Just run `composer require codenamephp/platform.di` in your cli which should install the latest version for you.

Usage
-----

[](#usage)

```
$builder = new de\codenamephp\platform\di\ContainerBuilder();
$container = $builder->build();
$container->get('...');
```

This creates a builder without definitions. To add definitions I recommend using one of the provider options below, especially the `de\codenamephp\platform\di\definitionsProvider\iArray` provider.

From there you just get your dependencies from the container.

### Using providers

[](#using-providers)

The best way to have configurations within modules and libraries is via providers. This way, the provider class will be used to add the files or definitions. Every time the provider class is updated, the configuration will be updated as well.

All providers need to implement one of the `de\codenamephp\platform\di\definitionsProvider\*` interfaces

```
use de\codenamephp\platform\di\definitionsProvider\iDefinitionsProvider;

$builder = new de\codenamephp\platform\di\ContainerBuilder();
$builder->addDefinitionsByProvider(new class() implements iDefinitionsProvider{});
$container = $builder->build();
$container->get('...');
```

#### Array

[](#array)

Probably the most performant provider since the definitions are defined within the method and don't require any additional file lookups:

```
class DefinitionsProvider implements de\codenamephp\platform\di\definitionsProvider\iArray {

  public function getDefinitions() : array {
    return ['some class' => 'some defintion'];
  }
}
```

#### File

[](#file)

The file provider provides absolute file paths to definition files:

```
class DefinitionsProvider implements de\codenamephp\platform\di\definitionsProvider\iFiles {

  public function getFiles() : array {
    return [__DIR__ . '/path/to/file'];
  }
}
```

#### MetaProvider

[](#metaprovider)

Sometimes you want to split dependencies into multiple providers so they don't get too long and/or to group them into logical units. But you don't want to add multiple providers to the actual project that uses the provider. This is what the `\de\codenamephp\platform\di\definitionsProvider\iMetaProvider` interface is for. It basically creates multiple providers and returns them as array which are then added by the container builder like any other provider including dependency checks and nesting other meta providers.

```
use de\codenamephp\platform\di\definitionsProvider\iArray;
use de\codenamephp\platform\di\definitionsProvider\iFiles;
use de\codenamephp\platform\di\definitionsProvider\iMetaProvider;

class MyArrayProvider implements iArray{
    public function getDefinitions() : array {
     return [];
    }
}
class MyFileProvider implements iFiles {
    public function getFiles() : array {
      return [];
    }
}
class MyNestedMetaProvider implements iMetaProvider {
    public function getProviders() : array{
      return [new MyFileProvider()];
    }
}
class MyMetaProvider implements iMetaProvider {
  public function getProviders() : array {
    return [
        new MyArrayProvider(),
        new MyNestedMetaProvider()
    ];
  }
}
```

But even in this example it becomes clearly visible that we are dealing with an Uncle Ben situation here: With great recursion comes bad headache! Dependencies are still checked so the providers need to be in the correct order which can become a real pain real fast if you go crazy with nesting providers.

I recommend not to use more than one level of nesting and if possible avoid it all together. After all, it's just a side effect of the implementation rather than a planned feature. ;)

### Provider Dependencies

[](#provider-dependencies)

Providers can depend on other providers, e.g. to override their definitions. If that is the case, providers can implement on of the `de\codenamephp\platform\di\definitionsProvider\dependency\*` interfaces.

#### Dependency Providers

[](#dependency-providers)

##### DependencyFactory

[](#dependencyfactory)

Since 5.1 a `\de\codenamephp\platform\di\definitionsProvider\factory\byClassname\iByClassname` interface was added that can be used to create dependencies on the fly. There is a very simple `\de\codenamephp\platform\di\definitionsProvider\factory\byClassname\SimpleNew` (hence the name) that just takes a class name and "news" it.

##### iDependsOn

[](#idependson)

This interface declares that a provider depends on other providers and must implement the getDependencies() method which returns all the class names of providers that have to be added to the container before this provider can be added.

```
use de\codenamephp\platform\di\definitionsProvider\iDefinitionsProvider;

class MustBeAddedBeforeMe implements iDefinitionsProvider {}

class DefinitionsProvider implements de\codenamephp\platform\di\definitionsProvider\dependency\iDependsOn {

  public function getDependencies() : array {
    return [MustBeAddedBeforeMe::class ];
  }
}
```

### Dependency checks

[](#dependency-checks)

When you have modules that depend on each other most often the definitions depend on each other as well. This is what the dependency collections are for. They collect the providers (duh) and also check the dependencies using the interfaces from above. They implement the `\de\codenamephp\platform\di\definitionsProvider\collection\iCollection` and do different levels of checks and sorting.

#### SimpleArray

[](#simplearray)

This collection doesn't actually do any dependency checks and just collects the providers and stores them in an array. This is used in most other collection as a simple storage.

#### ClassNamesInArray

[](#classnamesinarray)

This collection collects the class names of dependencies in an array and checks the dependencies against them. If the [iDependsOn](#idependson) interface is not added to the provider, the class name of the provider is added automatically, so if your provider only covers it's own dependency, you don't need to implement the interface.

This is a very simple check so it's also easy to debug. The dependencies are checked every time you add a dependency so it will fail early if something is missing. The drawback of this is that you have to add the providers in the correct order.

```
use de\codenamephp\platform\di\ContainerBuilder;
use de\codenamephp\platform\di\definitionsProvider\collection\ClassNamesInArray;
use de\codenamephp\platform\di\definitionsProvider\dependency\iDependsOn;
use de\codenamephp\platform\di\definitionsProvider\iDefinitionsProvider;

class Dependency implements iDefinitionsProvider {}
class Dependant implements iDependsOn { public function getDependencies() : array{ return [Dependency::class]; } }

$collection = new ClassNamesInArray();
$collection->add(new Dependency());
$collection->add(new Dependant()); // would fail if those were reversed
//...

$containerBuilder = new ContainerBuilder();
foreach($collection->get() as $provider) { $containerBuilder->addDefinitionsByProvider($provider); }
$container = $containerBuilder->build();
//...
```

#### TopoGraph

[](#topograph)

This collection sorts the provides by their dependencies. The sort and check is performed once you get the providers. This enables you to add the providers in any way you see fit. But it also means that there's a slight performance overhead and debugging might be a bit harder.

It also means that you have no way to influence the sequence other than declaring the dependencies so this is not only recommended but almost necessary.

```
use de\codenamephp\platform\di\ContainerBuilder;
use de\codenamephp\platform\di\definitionsProvider\collection\TopoGraph;
use de\codenamephp\platform\di\definitionsProvider\dependency\iDependsOn;
use de\codenamephp\platform\di\definitionsProvider\iDefinitionsProvider;

class Dependency implements iDefinitionsProvider {}
class Dependant implements iDependsOn { public function getDependencies() : array{ return [Dependency::class]; } }

$collection = new TopoGraph();
$collection->add(new Dependant()); // the sequence doesn't matter
$collection->add(new Dependency());
//...

$containerBuilder = new ContainerBuilder();
foreach($collection->get() as $provider) { $containerBuilder->addDefinitionsByProvider($provider); } // Dependency will be returned/added first
$container = $containerBuilder->build();
//...
```

#### CreateAndAddDependenciesBeforeProvider

[](#createandadddependenciesbeforeprovider)

This collection was create for [\#34](https://github.com/codenamephp/platform.di/issues/34). When a provider has the `\de\codenamephp\platform\di\definitionsProvider\dependency\iDependsOn` interface all dependencies are created using a `\de\codenamephp\platform\di\definitionsProvider\factory\byClassname\iByClassname`(`\de\codenamephp\platform\di\definitionsProvider\factory\byClassname\SimpleNew` by default) and added to an underlying collection (`\de\codenamephp\platform\di\definitionsProvider\collection\SimpleArray` by default).

Use this with caution since your provider can't have constructors since the factory cannot guess the arguments. You can of course implement you own factory but at that point it's probably easier to just add the dependencies yourself.

###  Health Score

39

—

LowBetter than 86% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community19

Small or concentrated contributor base

Maturity77

Established project with proven stability

 Bus Factor1

Top contributor holds 97.2% 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 ~133 days

Recently: every ~68 days

Total

17

Last Release

1990d ago

Major Versions

0.0.2 → 1.0.02016-03-01

1.1.0 → 2.0.02016-04-04

2.2.0 → 3.0.02018-08-19

3.0.0 → 4.0.02020-03-07

4.1.2 → 5.0.02020-08-15

PHP version history (6 changes)0.0.1PHP &gt;=5.6.0

1.1.0PHP &gt;=7

2.0.1PHP &gt;=5.6

3.0.0PHP ^7

4.0.0PHP ^7.4

5.2.0PHP ^7.4 || ^8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/6865819?v=4)[Bastian Schwarz](/maintainers/bastianschwarz)[@bastianschwarz](https://github.com/bastianschwarz)

---

Top Contributors

[![bastianschwarz](https://avatars.githubusercontent.com/u/6865819?v=4)](https://github.com/bastianschwarz "bastianschwarz (241 commits)")[![actions-user](https://avatars.githubusercontent.com/u/65916846?v=4)](https://github.com/actions-user "actions-user (3 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (2 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (1 commits)")[![jrfnl](https://avatars.githubusercontent.com/u/663378?v=4)](https://github.com/jrfnl "jrfnl (1 commits)")

---

Tags

dependency-injectionphp-di

###  Code Quality

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/codenamephp-platformdi/health.svg)

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

###  Alternatives

[cakephp/cakephp

The CakePHP framework

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

The SilverStripe framework

7213.5M2.5k](/packages/silverstripe-framework)[elgg/elgg

Elgg is an award-winning social networking engine, delivering the building blocks that enable businesses, schools, universities and associations to create their own fully-featured social networks and applications.

1.7k15.7k5](/packages/elgg-elgg)[neos/flow

Flow Application Framework

862.0M451](/packages/neos-flow)

PHPackages © 2026

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