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

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

jnjxp/container
===============

jnjxp container package

0.3.0(1y ago)02.5k↑150%MITPHPPHP ^8.0CI passing

Since Feb 17Pushed 1y ago1 watchersCompare

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

READMEChangelogDependencies (6)Versions (5)Used By (0)

jnjxp.container
===============

[](#jnjxpcontainer)

[![CI](https://github.com/jnjxp/jnjxp.container/actions/workflows/ci.yml/badge.svg)](https://github.com/jnjxp/jnjxp.container/actions/workflows/ci.yml)[![Packagist Version](https://camo.githubusercontent.com/5e26c0119e6e17b5e8e575a0ad6a26a61408f7bcfc1c84a326e91cbccbc7a96d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a6e6a78702f636f6e7461696e6572)](https://packagist.org/packages/jnjxp/container)

Yet another dependency injection container.

Install
-------

[](#install)

```
$ composer require jnjxp/container
```

Usage
-----

[](#usage)

### Factories

[](#factories)

Define how to create entries based on string identifier.

```
// Given

use Jnjxp\Container\Container;

class Foo {
    public function __construct(public int $dependency) { }
}

class Bar extends Foo { }

class BarFactory {
    public function __invoke() { return new Bar(1); }
}

// Configure Container

$factories = [
    Foo::class => fn() => new Foo(1), // callable
    Bar::class => BarFactory::class, // string identifier
];

$container = new Container(factories: $factories);

// Use Container

$foo = $container->get(Foo::class);
$bar = $container->get(Bar::class);

print_r($foo);
print_r($bar);

// Foo Object
// (
//     [dependency] => 1
// )
// Bar Object
// (
//     [dependency] => 1
// )
```

#### Resolving Dependencies

[](#resolving-dependencies)

`Container` is passed to factories.

```
// Given

use Jnjxp\Container\Container;
use Psr\Container\ContainerInterface as CI;

class Foo {
    public function __construct(public Bar $bar) { }
}

class Bar {}

// Configure Container

$factories = [
    Foo::class => fn(CI $container) => new Foo($container->get(Bar::class)),
    Bar::class => fn() => new Bar(),
];

$container = new Container(factories: $factories);

// Use Container

$foo = $container->get(Foo::class);

print_r($foo);

// Foo Object
// (
//     [bar] => Bar Object
//         (
//         )
//
// )
```

### Aliases

[](#aliases)

Add additional identifiers to entries.

```
// Given

use Jnjxp\Container\Container;

interface FooInterface {}

class Foo implements FooInterface {}

// Configure Container

$factories = [ Foo::class => fn() => new Foo() ];

$aliases = [
    FooInterface::class => Foo::class
];

$container = new Container(
    factories: $factories,
    aliases: $aliases,
);

// Use Container

$foo = $container->get(FooInterface::class);

print_r($foo);

// Foo Object
// (
// )
```

### Existing Instances

[](#existing-instances)

Container won't look for factories or extensions.

```
// Given

use Jnjxp\Container\Container;

// Configure Container

$instances = [ 'FOO' => 'foo' ];

$container = new Container(
    instances: $instances,
);

// Use Container

$foo = $container->get('FOO');

echo "$foo\n";

//foo
```

### Extensions

[](#extensions)

- Should be an `array` of callables or identifiers that resolve to them.
- 1st parameter is the `Container`, 2nd is the existing Instance. ([should be swapped](https://github.com/container-interop/service-provider/issues/50))
- The entry will be **replaced** by what is returned from the extension.

```
// Given

use Jnjxp\Container\Container;
use Psr\Container\ContainerInterface as CI;

class Foo {}

class FooDecorator
{
    public function __construct(public Foo $foo) { }
}

class Bar { public $bar ; }

// Configure Container

$extensions = [
    Foo::class => [ fn(CI $container, Foo $foo) => new FooDecorator($foo) ],
    Bar::class => [ function(CI $container, Bar $bar) { $bar->bar = 1 ; return $bar; } ],
];

$container = new Container(
    extensions: $extensions,
);

// Use Container

$foo = $container->get(Foo::class);
$bar = $container->get(Bar::class);

print_r($foo);
print_r($bar);

// FooDecorator Object
// (
//     [foo] => Foo Object
//         (
//         )
//
// )
// Bar Object
// (
//     [bar] => 1
// )
```

### Autowire

[](#autowire)

Enable auto-wiring by providing an `AutowireInterface`

```
// Given

use Jnjxp\Container\Container;
use Jnjxp\Container\Autowire\Autowire;

class Foo {
    public function __construct(public Bar $bar) { }
}

class Bar { }

// Configure Container

$autowire = new Autowire();

$container = new Container(
    autowire: $autowire,
);

// Use Container

$foo = $container->get(Foo::class);

print_r($foo);

// Foo Object
// (
//     [bar] => Bar Object
//         (
//         )
//
// )
```

#### `AutowireInterface`

[](#autowireinterface)

```
use Psr\Container\ContainerInterface;

interface AutowireInterface
{
    public function create(string $className, ?ContainerInterface $container = null): object;
}
```

### Builder

[](#builder)

User `ContainerBuilder` to build a `Container`.

```
use Jnjxp\Container\ContainerBuilder;

$builder = new Builder();

// set factories
$builder->factory($name, $factory);
$builder->factories($factories);

// set aliases
$builder->alias($alias, $identifier);
$builder->aliases($aliases);

//set instances
$builder->instance($name, $instance);
$builder->instances($instances);

// set extensions
$builder->extension($name, $extension);
$builder->extensions($extensions);

// enable auto-wiring
$builder->autowire(true);
$builder->autowire($autowireImplementation);

// disable auto-wiring
$builder->autowire(false);

// Bulid the Container
$container = $builder->build();
```

### Service Providers

[](#service-providers)

Service Providers can define *factories* and *extensions* in the `ContainerBuilder`.

See [container-interop/service-provider](https://github.com/container-interop/service-provider/tree/v0.4.1).

#### `ServiceProviderInterface`

[](#serviceproviderinterface)

```
namespace Interop\Container;

interface ServiceProviderInterface
{
    public function getFactories();

    public function getExtensions();
}
```

#### Service Provider Example

[](#service-provider-example)

```
// Given

use Jnjxp\Container\ContainerBuilder;
use Jnjxp\Container\ServiceProvider\BaseServiceProvider;

class Foo {
    public function __construct(public int $dependency) { }
}

class ServiceProvider extends BaseServiceProvider
{
    public function getFactories(): array
    {
        return [ Foo::class => fn() => new Foo(1) ];
    }
}

$builder = new ContainerBuilder();
$builder->provider(ServiceProvider::class);
$container = $builder->build();

$foo = $container->get(Foo::class);

print_r($foo);

// Foo Object
// (
//     [dependency] => 1
// )
```

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance45

Moderate activity, may be stable

Popularity20

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity51

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

Total

3

Last Release

429d ago

PHP version history (2 changes)0.2.0PHP ^7.2 || ^8.0

0.3.0PHP ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/64baf5fff415327ccc832998afe54b34ac3c621e401f128a9343d4e0b0a3f9e4?d=identicon)[jnj](/maintainers/jnj)

---

Top Contributors

[![jakejohns](https://avatars.githubusercontent.com/u/174708?v=4)](https://github.com/jakejohns "jakejohns (35 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Psalm

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[illuminate/contracts

The Illuminate Contracts package.

705122.9M10.1k](/packages/illuminate-contracts)[ecotone/ecotone

Supporting you in building DDD, CQRS, Event Sourcing applications with ease.

558549.8k17](/packages/ecotone-ecotone)[civicrm/civicrm-core

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

728272.9k20](/packages/civicrm-civicrm-core)[internal/dload

Downloads binaries.

98142.7k10](/packages/internal-dload)[symfony/object-mapper

Provides a way to map an object to another object

34885.7k18](/packages/symfony-object-mapper)[jaxon-php/jaxon-core

Jaxon is an open source PHP library for easily creating Ajax web applications

73142.3k25](/packages/jaxon-php-jaxon-core)

PHPackages © 2026

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