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

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

infuse-di/infuse
================

A PSR-11 compliant dependency injection library

1.1.0(1y ago)110[2 issues](https://github.com/davisenra/infuse/issues)MITPHPPHP 8.2.\*|8.3.\*|8.4.\*

Since Jun 27Pushed 1y ago1 watchersCompare

[ Source](https://github.com/davisenra/infuse)[ Packagist](https://packagist.org/packages/infuse-di/infuse)[ Docs](https://github.com/davisenra/infuse)[ RSS](/packages/infuse-di-infuse/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (5)Versions (3)Used By (0)

[![Infuse](https://camo.githubusercontent.com/5707b4e1f1202b18c1a385fc166471265d2879cb590073b24d96a2d3abd158fe/68747470733a2f2f6c68332e676f6f676c6575736572636f6e74656e742e636f6d2f752f302f64726976652d7669657765722f414b4770696859543344654b656e5876717a415458765045442d4f7557666575506d564b4d716f4c435a55516e5a68377465564176644a63414436554a6c71502d435f41707071695a464c4f6b314d4f45797a51626c624979324730653650715a5736663179633d77323536302d683932372d72772d7631)](https://camo.githubusercontent.com/5707b4e1f1202b18c1a385fc166471265d2879cb590073b24d96a2d3abd158fe/68747470733a2f2f6c68332e676f6f676c6575736572636f6e74656e742e636f6d2f752f302f64726976652d7669657765722f414b4770696859543344654b656e5876717a415458765045442d4f7557666575506d564b4d716f4c435a55516e5a68377465564176644a63414436554a6c71502d435f41707071695a464c4f6b314d4f45797a51626c624979324730653650715a5736663179633d77323536302d683932372d72772d7631)

[![CI](https://github.com/davisenra/infuse/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/davisenra/infuse/actions/workflows/ci.yml/badge.svg?branch=main)[![Packagist](https://camo.githubusercontent.com/d82afe4d12ffc9c1ef62bd1249f4eec5bf04529b781310b71f0af46d927e9f73/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f696e667573652d64692f696e66757365)](https://camo.githubusercontent.com/d82afe4d12ffc9c1ef62bd1249f4eec5bf04529b781310b71f0af46d927e9f73/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f696e667573652d64692f696e66757365)[![PHP](https://camo.githubusercontent.com/37adb4aa40a1b651dc4a266d871a5c46d869ba7a69db7a6945a246455c1ec6c5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f696e667573652d64692f696e667573652f706870)](https://camo.githubusercontent.com/37adb4aa40a1b651dc4a266d871a5c46d869ba7a69db7a6945a246455c1ec6c5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f696e667573652d64692f696e667573652f706870)[![License](https://camo.githubusercontent.com/2bdc9d915fc0749bbdcf483d56d4f97a2342ab0299fd5cfcbbc9388b24e7ec56/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6461766973656e72612f696e66757365)](https://camo.githubusercontent.com/2bdc9d915fc0749bbdcf483d56d4f97a2342ab0299fd5cfcbbc9388b24e7ec56/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6461766973656e72612f696e66757365)

Infuse 🍃
========

[](#infuse-)

*A minimal PSR-11 implementation.*

Features
--------

[](#features)

- ✔️ Autowiring (powered by Reflection)
- ✔️ Simple API (only 3 methods)
- ✔️ Container can be built from definitions
- ✔️ Singletons
- ✔️ Detects circular dependencies
- ⏳ Compilable for production
- ⏳ PHPStan generics support for container bindings

Documentation
-------------

[](#documentation)

Infuse has a very minimal API with only has three methods:

```
/**
 * Finds an entry of the container by its identifier and returns it.
 *
 * @param string $id identifier of the entry to look for
 *
 * @return mixed entry
 *
 * @throws NotFoundExceptionInterface  no entry was found for the provided identifier
 * @throws ContainerExceptionInterface error while retrieving the entry
 */
public function get(string $id): mixed;

/**
 * Returns true if the container can return an entry for the given identifier.
 * Returns false otherwise.
 *
 * `has($id)` returning true does not mean that `get($id)` will not throw an exception.
 * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`.
 *
 * @param string $id identifier of the entry to look for
 */
public function has(string $id): bool;

/**
 * @param \Closure(Container): mixed $definition
 *
 * @throws ContainerExceptionInterface if provided id is not unique
 */
public function bind(string $id, \Closure $definition): void;
```

Example:

```
use Infuse\Container;

$container = new Container();

// Container::bind tells the Container how to build a Bar object
$container->bind(Bar::class, function () {
    return new Bar();
});

// Container::has checks if there's a binding with the provided id
$container->has(Bar::class); // true

// the callable parameter receives the Container itself as argument
$container->bind(Foo::class, function (Container $c) {
    $bar = $c->get(Bar::class);
    return new Foo($bar):
});

// Container::get retrieves an instance from the Container, the bound callable will be called at this moment
$foo = $container->get(Foo::class);
$isFoo = $foo instanceof Foo; // true

// This will throw a ContainerException, ids must be unique
$container->bind(Bar::class, function (Container $c) {
    return new Bar();
});

// This will throw a NotFoundException, this id has not been bound
$container->get(Zee::class);

// You can bind basically anything
$container->bind('some_array', fn () => ['hello' => 'world']);
$container->bind('some_scalar', fn () => 42);
```

You can also create a ready to use Container from a definitions array:

```
// definitions.php
