PHPackages                             scruwi/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. scruwi/container

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

scruwi/container
================

PSR-11 Container implementation

v0.1.1(4y ago)44[1 issues](https://github.com/scruwi/container/issues)MITPHPPHP ^7.4|^8.0

Since Sep 24Pushed 4y ago1 watchersCompare

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

READMEChangelogDependencies (2)Versions (3)Used By (0)

My DI Container
===============

[](#my-di-container)

[![SWUbanner](https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/banner2.svg)](https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/banner2.svg)[![Latest Stable Version](https://camo.githubusercontent.com/b72f3d2774f3b3ca8a43529945b3fbc62a637e6e983c24227a4ab84f160bb847/68747470733a2f2f706f7365722e707567782e6f72672f7363727577692f636f6e7461696e65722f762f737461626c652e706e67)](https://packagist.org/packages/scruwi/container)[![Total Downloads](https://camo.githubusercontent.com/7e91d52256374d2cc6e3c164ea00c9bc49590d139578373b1ee8dd7b9056ea30/68747470733a2f2f706f7365722e707567782e6f72672f7363727577692f636f6e7461696e65722f646f776e6c6f6164732e706e67)](https://packagist.org/packages/scruwi/container)[![License](https://camo.githubusercontent.com/9ebc18cb075885d064e7d56151988c766ec67e8c7495c66b71f68d7d361cc739/687474703a2f2f706f7365722e707567782e6f72672f7363727577692f636f6e7461696e65722f6c6963656e7365)](https://packagist.org/packages/scruwi/container)[![PHP Version Require](https://camo.githubusercontent.com/450277b744d0ea9ab000c433e4d5b74011f58e1855917e3d8a283ec9506c7864/687474703a2f2f706f7365722e707567782e6f72672f7363727577692f636f6e7461696e65722f726571756972652f706870)](https://packagist.org/packages/scruwi/container)[![codecov](https://camo.githubusercontent.com/3dd2ea7f3cc6246b71e98e9f634f2e5d5a93a696c9a4d622bed8e22032b9cf3f/68747470733a2f2f636f6465636f762e696f2f67682f7363727577692f636f6e7461696e65722f6272616e63682f6d61696e2f67726170682f62616467652e7376673f746f6b656e3d583741344c4930463345)](https://codecov.io/gh/scruwi/container)

It's my own implementation PSR-11 Container Interface.

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

[](#installation)

```
composer require scruwi/container
```

Init
----

[](#init)

```
$container = new Container([
    new ClassResolver(['App\\Namespace1', 'App\\Namespace2']),
    new ParameterResolver(['param1' => true, 'param2' => 'string']),
    new DefinitionResolver([Definition1:class, Definition2:class]),
]);
```

Resolvers
---------

[](#resolvers)

- [`ClassResolver`](#ClassResolver) - defines namespaces, that are allowed to autowire
- [`ParameterResolver`](#ParameterResolver) - defines scalar parameters, that can be set to constructors
- [`DefinitionResolver`](#DefinitionResolver) - defines paths to definitions

You can implement your resolvers in a project if you need them. Look at the `Interfaces` namespace.

### ClassResolver

[](#classresolver)

Only classes from defined namespaces can be autowired.

### ParameterResolver

[](#parameterresolver)

Typical usage:

```
$rootParameters = ['param1' => true, 'param2' => 'string'];
$classSpecificParameters = [SpecificClass::class => ['param1' => false]];
$resolver = ParameterResolver($rootParameters, $classSpecificParameters);
```

Expected behaviour:

```
class SomeClass
{
    public function __construct(bool $param1) { /* $param1 === true */ }
}
```

```
class SpecificClass
{
    public function __construct(bool $param1) { /* $param1 === false */ }
}
```

Parameter with default value does not have to be resolved by the config:

```
class SpecificClass
{
    public function __construct(bool $param0 = 'default') { /* $param0 === 'default' */ }
}
```

You can create a specific resolver in your way and use it in your project. Just implement `ParameterResolverInterface`and specify it during container construct.

### DefinitionResolver

[](#definitionresolver)

Attach definitions to container.

**Definition**, there is no more than a factory that creates an object in a specific way. There is one specific `ReflectionClassDefinition` that constructs classes by their reflections. All autowired classes use that factory to create their object.

You should create as many definitions in your project as you need and specify them during container construct or attach them later by `$container->addDefinition()` method. Any definition must implement `DefinitionInterface`.

Typical definition class looks like this:

```
class ExampleDefinition implements DefinitionInterface
{
    public static function getId(): string
    {
        return ExampleClass::class;
    }

    public function __invoke(Container $container, ParameterResolver $resolver, BuildContext $context): object
    {
        return new ExampleClass();
    }
}
```

You can fetch some default parameters from the ParameterResolver:

```
    public function __invoke(Container $container, ParameterResolver $resolver, BuildContext $context): object
    {
        $param1 = $resolver->resolve('param1', ExampleClass::class);
        return new ExampleClass($param1);
    }
```

Also, you can find out from which context it was called. It may be useful for autowire interfaces:

```
class InterfaceDefinition implements DefinitionInterface
{
    public function getId(): string
    {
        return SomeInterface::class;
    }

    public function __invoke(Container $container, ParameterResolver $resolver, BuildContext $context): object
    {
        if ($context->getTargetClass() === ClassWithInterfaceDependency::class) {
            return new SomeClassImplementsInterface2();
        }

        return new SomeClassImplementsInterface1();
    }
}
```

Exceptions
----------

[](#exceptions)

- `NotFoundExceptionInterface` - for an unknown entry
- `ContainerExceptionInterface` - for all common exceptions in the container
- `CircularReferencesException` - there is a special exception for circular references in the container

###  Health Score

21

—

LowBetter than 18% of packages

Maintenance15

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity46

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

Total

2

Last Release

1693d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/473ff02e02c03daad55bda9fce9c6fec9f9b7af620f55139ce27b1d71545506c?d=identicon)[scruwi](/maintainers/scruwi)

---

Top Contributors

[![scruwi](https://avatars.githubusercontent.com/u/13875215?v=4)](https://github.com/scruwi "scruwi (26 commits)")

---

Tags

phpcontainerPSR-11di

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[devanych/di-container

Simple implementation of a PSR-11 dependency injection container

124.2k3](/packages/devanych-di-container)

PHPackages © 2026

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