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

ActiveLibrary[Framework](/categories/framework)

hafo/di
=======

Hafo Framework Dependency Injection

1.0.0(6y ago)1259PHPPHP &gt;=7.1.0CI failing

Since Aug 26Pushed 6y agoCompare

[ Source](https://github.com/HafoFW/di)[ Packagist](https://packagist.org/packages/hafo/di)[ RSS](/packages/hafo-di/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (6)Dependencies (2)Versions (11)Used By (0)

Hafo DI
=======

[](#hafo-di)

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

[](#what-is-this)

This is a small PHP Dependency Injection Container with autowiring and as little configuration as possible. It extends `Psr\Container\ContainerInterface` and provides some goodies to help accelerate development.

How do I install this?
----------------------

[](#how-do-i-install-this)

Via Composer:

```
composer require hafo/di

```

PHP version 7.1 or higher is required.

How to use?
-----------

[](#how-to-use)

The easiest way is to use the `ContainerBuilder` class:

```
use Hafo\DI\ContainerBuilder;

$builder = new ContainerBuilder();

// configure the builder
$builder->addFactories([
    Symfony\Component\Console\Application::class => function () {
        return new Symfony\Component\Console\Application();
    }
]);

$container = $builder->createContainer();

// run the application
$application = $container->get(Symfony\Component\Console\Application::class);
$application->run();
```

### Adding service factories

[](#adding-service-factories)

You can add services via ContainerBuilder:

```
$builder->addFactories([
    Symfony\Component\Console\Application::class => function () {
        return new Symfony\Component\Console\Application();
    },
    // ... add more services
]);
```

It is recommended to use the classnames as identifiers as these can be used for autowiring, if enabled. Also, it's best practice.

Parameter can be an array, or any iterable really, as long as it provides both the keys and the factory callbacks.

### Decorating services

[](#decorating-services)

A decorator is a simple callback that gets called when a service is created. It can be used to modify the service before returning it.

```
use Hafo\DI\Container;

$builder->addDecorators([
    Symfony\Component\Console\Application::class => [
        function (Symfony\Component\Console\Application $application, Container $container) {
            $application->add($container->get(MyProject\Console\SomeCommand::class));
        },
        // ... add more decorators for Application class
    ],
    // ... add more decorators for other services
]);
```

Decision whether a decorator should be used for a service is done via simple `is_a()` check, so you can easily decorate multiple services that implement same interface, for example.

The container makes sure that each decorator gets called only once for each service instance.

### Adding parameters

[](#adding-parameters)

You can also add parameters:

```
$builder->addParameters([
    'rootDir' => __DIR__,
    // ... add more parameters
]);
```

Parameters are then registered in the DI container and can be accessed via the `get()` method, just like the services.

### Using autowiring

[](#using-autowiring)

If you want to avoid writing many factories by hand, you can use autowiring. Just make sure that the constructor arguments for your service are resolvable, which means that all the dependencies must be instantiable using the DI container or they must have default values.

```
use Hafo\DI\Autowiring\AutowiringCache\MemoryCache;
use Hafo\DI\Autowiring\DefaultAutowiring;
use Hafo\DI\ContainerBuilder;

$builder = new ContainerBuilder();

$builder->setAutowiring(new DefaultAutowiring(new MemoryCache()));

$container = $builder->createContainer();
```

You can use `Hafo\DI\Autowiring\AutowiringCache\NetteCache` instead of `MemoryCache` if you use the `nette/caching` package.

You can also implement your own `Hafo\DI\Autowiring\AutowiringCache` or even `Hafo\DI\Autowiring`.

### Interface-implementation map

[](#interface-implementation-map)

It is good practice to use interfaces for your services. The DI container however doesn't know by default which specific class you want to return. We need to specify it:

```
$builder->addInterfaceImplementationMap([
    Doctrine\ORM\EntityManagerInterface::class => Doctrine\ORM\EntityManager::class
]);
```

### Reusable modules

[](#reusable-modules)

You can create self-contained reusable modules simply by implementing the `Hafo\DI\Module` interface. Making the module work in your project is then as easy as instantiating it and calling a method `install`, passing the builder as an argument.

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity64

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

Recently: every ~237 days

Total

10

Last Release

2446d ago

Major Versions

v0.6.1 → 1.0.0-rc12019-07-11

### Community

Maintainers

![](https://www.gravatar.com/avatar/e355c4a95369341c4d21dcda1279dcd227f73ea58e88171e91affd56c2fadacb?d=identicon)[Zax](/maintainers/Zax)

---

Top Contributors

[![zaxxx](https://avatars.githubusercontent.com/u/7381443?v=4)](https://github.com/zaxxx "zaxxx (18 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/hafo-di/health.svg)

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

###  Alternatives

[laravel/framework

The Laravel Framework.

34.8k532.1M19.4k](/packages/laravel-framework)[symfony/symfony

The Symfony PHP framework

31.4k86.9M2.2k](/packages/symfony-symfony)[cakephp/cakephp

The CakePHP framework

8.8k19.1M1.7k](/packages/cakephp-cakephp)[sulu/sulu

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

1.3k1.4M196](/packages/sulu-sulu)[tempest/framework

The PHP framework that gets out of your way.

2.2k31.1k12](/packages/tempest-framework)[typo3/cms

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

1.2k1.9M122](/packages/typo3-cms)

PHPackages © 2026

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