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

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

hgraca/micro-di
===============

A small dependency injection library that uses reflection and a service locator container, to instantiate and recursively resolve, instantiate and inject dependencies.

1.0.0(9y ago)019MITPHPPHP &gt;=7.0

Since Dec 10Pushed 9y ago1 watchersCompare

[ Source](https://github.com/hgraca/php-micro-di)[ Packagist](https://packagist.org/packages/hgraca/micro-di)[ RSS](/packages/hgraca-micro-di/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (1)Dependencies (7)Versions (2)Used By (0)

Hgraca\\MicroDI
===============

[](#hgracamicrodi)

[![Author](https://camo.githubusercontent.com/e80528a6fbd7a1766b221b1a32d700b339f6a1992a8e0a1f602d24c256c74d66/687474703a2f2f696d672e736869656c64732e696f2f62616467652f617574686f722d406867726163612d626c75652e7376673f7374796c653d666c61742d737175617265)](https://www.herbertograca.com)[![Software License](https://camo.githubusercontent.com/942e017bf0672002dd32a857c95d66f28c5900ab541838c6c664442516309c8a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![Latest Version](https://camo.githubusercontent.com/d3b37095f2977bab6278bbd58f1a9f8f8cdcd86e7b1d65c1bb16aef4ee0f4f66/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f6867726163612f7068702d6d6963726f2d64692e7376673f7374796c653d666c61742d737175617265)](https://github.com/hgraca/php-micro-di/releases)[![Total Downloads](https://camo.githubusercontent.com/0df76d742e2e8713d50ccc25064fc7554fb896efa5e38377350b369e1d229add/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6867726163612f6d6963726f2d64692e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/hgraca/micro-di)

[![Build Status](https://camo.githubusercontent.com/20d18446f9b5e2b5d79e275f00d019eafd6bf345b930b6a89a7afa48b40a4e29/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f6275696c642f672f6867726163612f7068702d6d6963726f2d64692e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/hgraca/php-micro-di/build)[![Coverage Status](https://camo.githubusercontent.com/ce707058228ab15cc97fbae6e857a6ceef0750baade9219a0a6d80767c9e465a/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f6867726163612f7068702d6d6963726f2d64692e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/hgraca/php-micro-di/code-structure)[![Quality Score](https://camo.githubusercontent.com/ba467c9cd07071ab2ccf488c775fdae2979934bdc890ce5d6b748f782e69a466/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6867726163612f7068702d6d6963726f2d64692e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/hgraca/php-micro-di)

A small dependency injection library that uses reflection and a service locator container, to instantiate and recursively resolve, instantiate and inject dependencies.

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

[](#installation)

To install the library, run the command below and you will get the latest version:

```
composer require hgraca/micro-di

```

Usage
-----

[](#usage)

### Factories

[](#factories)

The factories used in `Builder::buildFromFactory` need to extend `Hgraca\MicroDI\FactoryInterface`.

The method `Builder::buildFromFactory` instantiates the factory and calls the `create` method on it. The arguments given to `Builder::buildFromFactory` will be used both when instantiating the factory and when calling the `create` method:

- They will be injected in the `__construct()` if the arguments keys match any of the dependencies names.
- They will all be injected in the `create()`, together with whatever is in the container with the key '.context', where is the relevant factory FQCN.

### Dependency resolution process

[](#dependency-resolution-process)

The builder will resolve and instantiate dependencies. The dependencies, both when instantiating and when calling a method with `Builder::call`, will fe filled in the following priority:

1. By matching the dependencies names with the name of the provided arguments
2. If its a class/interface, it will try to instantiate it, which in turn:
    1. tries to fetch it from the container, by searching for the class/interface fqcn as one of the containers keys
        1. If the key (class/interface fqcn) is found:
            - If its an instance, it will be used (**singleton**)
            - If its a closure, it will be used to instantiate the required class (**singleton**)
            - If its a factory, it will be used to build the required class through the method `Builder::buildFromFactory`, and build a new instance every time (making it **not a singleton**)
        2. If the key (class/interface fqcn) is not found:
            1. If its a class it will be instantiated (recursively resolving and injecting its dependencies) and it will be cached in the container
            2. If its an interface, an error will be thrown
3. If its not a class/interface it will try to find the dependencies in the container, by their name.
4. If it fails to find all mandatory dependencies, it will throw an error while instantiating the class.

Todo
----

[](#todo)

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity58

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

Unknown

Total

1

Last Release

3442d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/4af14428505ab93e698bb1fa7a540b0f71dbae0246b6c534f39011aaafc13717?d=identicon)[hgraca](/maintainers/hgraca)

---

Top Contributors

[![hgraca](https://avatars.githubusercontent.com/u/1809002?v=4)](https://github.com/hgraca "hgraca (12 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

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

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

###  Alternatives

[kbs1/laravel-abbreviations

Abbreviations / acronyms support for your app.

1018.6k](/packages/kbs1-laravel-abbreviations)

PHPackages © 2026

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