PHPackages                             robertwesner/dependency-injection - 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. robertwesner/dependency-injection

ActiveLibrary[Framework](/categories/framework)

robertwesner/dependency-injection
=================================

A fun PSR-11 container implementation

v1.5.0(3mo ago)075[1 issues](https://github.com/RobertWesner/dependency-injection/issues)5MITPHPPHP &gt;=8.4CI passing

Since May 15Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/RobertWesner/dependency-injection)[ Packagist](https://packagist.org/packages/robertwesner/dependency-injection)[ RSS](/packages/robertwesner-dependency-injection/feed)WikiDiscussions main Synced today

READMEChangelog (9)Dependencies (14)Versions (14)Used By (5)

PSR-11 container with autowiring
=================================

[](#psr-11-container-with-autowiring)

[![](https://github.com/RobertWesner/dependency-injection/actions/workflows/tests.yml/badge.svg)](https://github.com/RobertWesner/dependency-injection/actions/workflows/tests.yml/badge.svg)[![](https://raw.githubusercontent.com/RobertWesner/dependency-injection/image-data/coverage.svg)](https://raw.githubusercontent.com/RobertWesner/dependency-injection/image-data/coverage.svg)[![](https://camo.githubusercontent.com/8c49924bbbb42afebd57ae36a4c7409cec86983996d359c2fccbce6926df6b92/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f526f626572745765736e65722f646570656e64656e63792d696e6a656374696f6e)](https://camo.githubusercontent.com/8c49924bbbb42afebd57ae36a4c7409cec86983996d359c2fccbce6926df6b92/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f526f626572745765736e65722f646570656e64656e63792d696e6a656374696f6e)[![License: MIT](https://camo.githubusercontent.com/65fe61b468aa86f6f5cfcdb25a79eae7a538d831f4a493b9134b8998e2f0fceb/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f526f626572745765736e65722f646570656e64656e63792d696e6a656374696f6e)](../../raw/main/LICENSE.txt)

What is this?
-------------

[](#what-is-this)

This is a small and fun PSR-11 container implementation with autowiring.

It provides plenty of alternative ways to autowire non-object values from .env, JSON files, constants, and more via PHP Attributes.

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

[](#installation)

```
composer require robertwesner/dependency-injection
```

Usage
-----

[](#usage)

```
// Instantiate new container
$container = new Container();

// Load class MyClass with all it's dependencies. Store and return its instance.
$instance = $container->get(MyClass::class);
$instance->myMethod('Some text.', 1234);
```

Autowiring non-instance values
------------------------------

[](#autowiring-non-instance-values)

This package provides multiple ways to load scalar values and arrays into classes.

```
readonly class Foo
{
    public function __construct(
        // Resolves all dependencies for Bar, if any, and uses its instance
        private Bar $bar,

        // Load any fixed value, can take values from constants...
        #[AutowireValue(Moody::MOOD)]
        private string $myMood,
        // ...or value literals
        #[AutowireValue(1234)]
        private int $scalar,

        // Load from superglobals like _COOKIE, _SESSION, or GLOBALS
        #[AutowireGlobal('GLOBALS', 'demo')]
        private string $demo,

        // Call any function or static method, optionally with arguments
        #[AutowireCallable([StaticProvider::class, 'provide'], ['123', 'test'])]
        private string $provided,

        // Load a full file
        #[AutowireFile(__DIR__ . '/../../cat.txt')]
        private string $asciiCat,

        // Load from a .env, .env.local, or similar file
        #[AutowireEnv(__DIR__ . '/../../.env', 'TEST')]
        private string $envTest,

        // Load from JSON file based on JSONPath
        // Can return an array of all JSONPath matches with the "multiple" Parameter
        #[AutowireJson(__DIR__ . '/../../foo.json', '$.test.value')]
        private int $val,

        // Load from YAML file based on JSONPath
        // Can return an array of all JSONPath matches with the "multiple" Parameter
        #[AutowireYaml(__DIR__ . '/../../foo.yaml', '$.test.value')]
        private int $fromYaml,

        // Load from TOML file based on JSONPath
        // Can return an array of all JSONPath matches with the "multiple" Parameter
        #[AutowireToml(__DIR__ . '/../../config.toml', '$.database')]
        private array $databaseConfig,

        // Load an XML file and parses it as SimpleXml. Then applies xPath to it to acquire an array element result
        #[AutowireXml(__DIR__ . '/../../test.xml', '/document/chapters/chapter[2]/@title')]
        private array $chapter2TitleResult,

        // Reads a header and stores its value. Missing Headers are always NULL so all usages should be nullable!
        #[AutowireHeader('User-Agent')]
        private ?string $userAgent,
    ) {}
}
```

Buffering autowired files for multiple access
---------------------------------------------

[](#buffering-autowired-files-for-multiple-access)

When using a single file multiple times you should consider adding the Attribute `#[BufferFile]`to store the (parsed) file in memory and load it when reused.

applicable to:

- `#[AutowireFile]`
- `#[AutowireJson]`
- `#[AutowireEnv]`
- `#[AutowireYaml]`
- `#[AutowireToml]`
- `#[AutowireXml]`

```
readonly class DatabaseService
{
    public function __construct(
        #[AutowireEnv(__DIR__ . '../../.env', 'MYSQL_SERVER')]
        #[BufferFile]
        private string $dbServer,
        #[AutowireEnv(__DIR__ . '../../.env', 'MYSQL_USERNAME')]
        #[BufferFile]
        private string $dbUsername,
        #[AutowireEnv(__DIR__ . '../../.env', 'MYSQL_PASSWORD')]
        #[BufferFile]
        private string $dbPassword,
    ) {}
}
```

###  Health Score

44

—

FairBetter than 90% of packages

Maintenance82

Actively maintained with recent releases

Popularity11

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity62

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

Recently: every ~74 days

Total

10

Last Release

95d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/155840994?v=4)[Robert Wesner](/maintainers/RobertWesner)[@RobertWesner](https://github.com/RobertWesner)

---

Top Contributors

[![RobertWesner](https://avatars.githubusercontent.com/u/155840994?v=4)](https://github.com/RobertWesner "RobertWesner (38 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/robertwesner-dependency-injection/health.svg)

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

###  Alternatives

[tempest/framework

The PHP framework that gets out of your way.

2.2k34.4k15](/packages/tempest-framework)[laravel/framework

The Laravel Framework.

34.8k543.8M20.1k](/packages/laravel-framework)[drupal/core-recommended

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

6942.5M421](/packages/drupal-core-recommended)[typo3/cms

TYPO3 CMS is a free open source Content Management Framework initially created by Kasper Skaarhoj and licensed under GNU/GPL.

1.2k1.9M121](/packages/typo3-cms)[sulu/sulu

Core framework that implements the functionality of the Sulu content management system

1.3k1.4M203](/packages/sulu-sulu)[silverstripe/framework

The SilverStripe framework

7313.7M2.8k](/packages/silverstripe-framework)

PHPackages © 2026

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