PHPackages                             brightnucleus/injector - 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. brightnucleus/injector

ActiveLibrary

brightnucleus/injector
======================

Config-driven extension of Auryn Dependency Injector.

v0.5.0(1y ago)53.9k2[1 issues](https://github.com/brightnucleus/injector/issues)1MITPHPCI passing

Since Apr 28Pushed 8mo ago1 watchersCompare

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

READMEChangelogDependencies (8)Versions (16)Used By (1)

Bright Nucleus Injector Component
=================================

[](#bright-nucleus-injector-component)

> Config-driven Dependency Injector, based in large parts on Auryn.

[![Build Status](https://github.com/brightnucleus/injector/actions/workflows/testing.yml/badge.svg)](https://github.com/brightnucleus/injector/actions/workflows/testing.yml)[![Latest Stable Version](https://camo.githubusercontent.com/8e4d85d7c138c4ca5c26533a9145fcbba3130f624bb76f8c5e4a2f2f9cd6f10f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6272696768746e75636c6575732f696e6a6563746f722e737667)](https://packagist.org/packages/brightnucleus/injector)[![Total Downloads](https://camo.githubusercontent.com/2fcb011012a51f376b4b533301bbe40a3f82bdbc24c88b03673a9a7887ff75a8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6272696768746e75636c6575732f696e6a6563746f722e737667)](https://packagist.org/packages/brightnucleus/injector)[![License](https://camo.githubusercontent.com/8c6fd1dbb4ce0524d3855639de7dc5445bacc60ae76a111e2e3dc2223186f6e0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6272696768746e75636c6575732f696e6a6563746f722e737667)](https://packagist.org/packages/brightnucleus/injector)

This is a config-driven dependency injector, to allow easy registration of alias mappings through the [`brightnucleus/config`](https://github.com/brightnucleus/config) component.

It includes large parts of code from the [`rdlowrey/auryn`](https://github.com/rdlowrey/auryn) package.

Notable changes compared to Auryn:

- Injector configuration can be done through a Config file.
- Aliases are case-sensitive.
- Closures can receive an `InjectionChain` object that let you iterate over the instantiation hierarchy.

Table Of Contents
-----------------

[](#table-of-contents)

- [Requirements](#requirements)
- [Installation](#installation)
- [Basic Usage](#basic-usage)
    - [Standard Aliases](#standard-aliases)
    - [Shared Aliases](#shared-aliases)
    - [Argument Definitions](#argument-definitions)
    - [Argument Providers](#argument-providers)
    - [Delegations](#delegations)
    - [Preparations](#preparations)
- [Registering Additional Mappings](#registering-additional-mappings)
- [Contributing](#contributing)
- [License](#license)

Requirements
------------

[](#requirements)

BrightNucleus Injector requires PHP 7.0+.

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

[](#installation)

The best way to use this component is through Composer:

```
composer require brightnucleus/injector
```

Basic Usage
-----------

[](#basic-usage)

> This documentation only deals with passing in mappings through a `Config` file. Documentation for the basic methods still needs to be synced. For now, just refer to the [Auryn README](https://github.com/rdlowrey/auryn/blob/master/README.md) for these..

The Bright Nucleus Injector expects to get an object through its constructor that implements the `BrightNucleus\Config\ConfigInterface`. You need to pass in the correct "sub-configuration", so that the keys that the `Injector` is looking for are found at the root level.

The `Injector` looks for three configuration keys: `standardAliases`, `sharedAliases` and `configFiles`.

The injector works by letting you map aliases to implementations. An alias is a specific name that you want to be able to instantiate. Aliases can be classes, abstract classes, interfaces or arbitrary strings. You can map each alias to a concrete implementation that the injector should instantiate.

This allows you to have your classes only depend on interfaces, through constructor injection, and choose the specific implementation to use through the injector config file.

As an example, imagine the following class:

```
class BookReader
{
    /** @var BookInterface */
    protected $book;

    public function __construct(BookInterface $book)
    {
        $this->book = $book;
    }

    public function read()
    {
        echo $this->book->getContents();
    }
}
```

If we now define an alias `'BookInterface' => 'LatestBestseller'`, we can have code like the following:

```
