PHPackages                             maiorano84/league-container-config - 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. maiorano84/league-container-config

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

maiorano84/league-container-config
==================================

A simple builder for league/container

v1.0.0(6y ago)215[1 PRs](https://github.com/maiorano84/league-container-config/pulls)MITPHPPHP ^7.4CI failing

Since May 10Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/maiorano84/league-container-config)[ Packagist](https://packagist.org/packages/maiorano84/league-container-config)[ RSS](/packages/maiorano84-league-container-config/feed)WikiDiscussions master Synced yesterday

READMEChangelog (1)Dependencies (3)Versions (3)Used By (0)

League Container Builder
========================

[](#league-container-builder)

A simple Builder pattern to create a complete [league/container](https://github.com/thephpleague/container) instance out of formatted arrays.

[![Author](https://camo.githubusercontent.com/b11b5e63f017bdcf711be74535b6fa3a9f00d574a0070b446ec50b5786d92a17/687474703a2f2f696d672e736869656c64732e696f2f62616467652f617574686f722d4d6174742532304d61696f72616e6f2d626c75652e7376673f7374796c653d666c61742d737175617265)](https://mattmaiorano.com)[![Latest Stable Version](https://camo.githubusercontent.com/9a4d6a227aa1aeb987f1235ba3cd2fb2d000d0986da1e15579e975baa792da6f/68747470733a2f2f706f7365722e707567782e6f72672f6d61696f72616e6f38342f6c65616775652d636f6e7461696e65722d636f6e6669672f762f737461626c65)](https://packagist.org/packages/maiorano84/league-container-config)[![Total Downloads](https://camo.githubusercontent.com/c8a0bb4fc2de31b7fa7f83c8feddd143851deb0276c65d0e17f89807174306b5/68747470733a2f2f706f7365722e707567782e6f72672f6d61696f72616e6f38342f6c65616775652d636f6e7461696e65722d636f6e6669672f646f776e6c6f616473)](https://packagist.org/packages/maiorano84/league-container-config)[![License](https://camo.githubusercontent.com/79977d8e737888c0afec276763ecf336cf9bf7d3c750adfa9efe188870ac08cd/68747470733a2f2f706f7365722e707567782e6f72672f6d61696f72616e6f38342f6c65616775652d636f6e7461696e65722d636f6e6669672f6c6963656e7365)](https://packagist.org/packages/maiorano84/league-container-config)[![Build Status](https://camo.githubusercontent.com/67c8de99654b414643577c8ce440423689bad903b3f97c3ae168b054bc4e001c/68747470733a2f2f7472617669732d63692e636f6d2f6d61696f72616e6f38342f6c65616775652d636f6e7461696e65722d636f6e6669672e7376673f6272616e63683d6d6173746572)](https://travis-ci.com/maiorano84/league-container-config)[![Code Coverage](https://camo.githubusercontent.com/dffaad7077e5981abc7ca57b5b1e5b40448f825e5897102ca336c2ba5ea6b485/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d61696f72616e6f38342f6c65616775652d636f6e7461696e65722d636f6e6669672f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/maiorano84/league-container-config/?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/3f860cade40673efed80e1f5aff19068b92f081a957a9e90eea7cd87ae52b5d1/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d61696f72616e6f38342f6c65616775652d636f6e7461696e65722d636f6e6669672f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/maiorano84/league-container-config/?branch=master)[![StyleCI](https://camo.githubusercontent.com/3711a1cf57260b2f878066c5893e8ce80b9a7b2c417e229c4c828097bbe66daa/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3236323732363232322f736869656c643f6272616e63683d6d6173746572)](https://github.styleci.io/repos/262726222)

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

[](#requirements)

League Container Builder requires PHP 7.4 or greater.

Composer
--------

[](#composer)

This package may be installed as a Composer dependency by running the following command:

`composer require maiorano84/league-container-config`

If you would like to use the latest unstable version, you can run:

`composer require maiorano84/league-container-config:dev-master`

Usage
-----

[](#usage)

This is a small addon to [The PHP League's Container](https://container.thephpleague.com/) package, which allows for passing in configuration objects and receiving complete Container instances in your application.

There are 4 Builders available for use:

- **ContainerBuilder** - The primary builder used both for instantiating its Builder dependencies as well as constructing the Container instance
- **DefinitionBuilder** - Responsible for constructing Definition instances and adding them to its internal DefinitionAggregate
- **InflectorBuilder** - Responsible for constructing Inflector instances and adding them to its internal InflectorAggregate
- **ServiceProviderBuilder** - Responsible for constructing ServiceProvider instances and adding them to its internal ServiceProviderAggregate

All builders implement the `Maiorano\ContainerConfig\BuilderInterface` which grants access to a simple `build` method that takes in a configuration array, and sends back the object each builder is responsible for constructing.

Why?
----

[](#why)

If you require your application's DI Container to be configurable by the user, then this might help. Rather than passing around a Container instance directly and leaving it to the user to figure out how to wire up everything, you may instead look to configuration files (PHP, JSON, XML, YAML, etc.) to parse and then build all at once.

Even if you don't have plans in releasing your own application that requires that level of abstraction, this can still be used as a way to consolidate your logic into a simpler and expressive structure.

Outside of those reasons, this offers no extra benefits that Container itself doesn't handle.

---

### The Container Builder

[](#the-container-builder)

It is recommended to instantiate the Container Builder with the `make` factory method:

```
use Maiorano\ContainerConfig\ContainerBuilder;

$builder = ContainerBuilder::make();

```

From here, generating Container objects can be done with the `build` command:

```
use League\Container\ReflectionContainer;
use Maiorano\ContainerConfig\ContainerBuilder;

$builder = ContainerBuilder::make();
$container = $builder->build([
    'definitions' => [
        AbstractInterface::class => Concrete::class, // Simple Mapping
        'name' => [ // Complete Definition structure
            'alias' => AbstractInterface::class,
            'concrete' => Concrete::class,
            'shared' => true,
            'arguments' => [Dependency::class, 'arg2', 'arg3'],
            'methods'   => ['methodName' => ['arg1', 'arg2']],
            'tags'      => ['tag1', 'tag2', 'tag3'],
        ],
    ],
    'serviceProviders' => [
        CustomServiceProvider::class,
        new UserDefinedServiceProvider,
    ],
    'inflectors' => [
        DefinitionAlias::class => 'methodName', // Simple inflector
        OtherDefinitionAlias::class => [ // Complete Inflector Structure
            'callback' => function($object){ var_dump($object); },
            'properties' => ['propery' => 'value'],
            'methods' => ['methodName' => ['arg1', 'arg2']],
        ],
    ],
    'delegates' => [
        $anotherContainerObject,
        ReflectionContainer::class,
    ],
]);

```

In this example, each top-level configuration block is passed down to its corresponding builder and the various aggregates are constructed and sent to the Container object.

---

### The Definition Builder

[](#the-definition-builder)

A Definition Builder only requires an object that fulfills the `DefinitionAggregateInterface`. [Container Definitions](https://container.thephpleague.com/3.x/definitions/) may be built in bulk or one at a time.

Generating definitions in bulk using `build`:

```
use Maiorano\ContainerConfig\DefinitionBuilder;
use League\Container\Definition\DefinitionAggregate;

$builder = new DefinitionBuilder(new DefinitionAggregate);
$aggregate = $builder->build([
    AbstractInterface::class => Concrete::class, // Simple Mapping
    'name' => [ // Complete Definition structure
        'alias' => AbstractInterface::class,
        'concrete' => Concrete::class,
        'shared' => true,
        'arguments' => [Dependency::class, 'arg2', 'arg3'],
        'methods'   => ['methodName' => ['arg1', 'arg2']],
        'tags'      => ['tag1', 'tag2', 'tag3'],
    ],
]);

```

In the unusual case that you would need the definition builder's own `buildDefinition` method, single Definitions can be constructed in the following way:

```
use Maiorano\ContainerConfig\DefinitionBuilder;
use League\Container\Definition\DefinitionAggregate;

$builder = new DefinitionBuilder(new DefinitionAggregate);
$definition = $builder->buildDefinition('name', [
    'alias' => AbstractInterface::class,
    'concrete' => Concrete::class,
    'shared' => true,
    'arguments' => [Dependency::class, 'arg2', 'arg3'],
    'methods'   => ['methodName' => ['arg1', 'arg2']],
    'tags'      => ['tag1', 'tag2', 'tag3'],
]);

```

#### Definition aliasing

[](#definition-aliasing)

While it is always recommended to define your aliases using array keys or the 'alias' mapping, the Definition Builder will also do its best to resolve an alias for you in case it's not provided.

Definition aliases are resolved in the following order:

1. Does the configuration value contain an inner 'alias' key? If so, use it.
2. Is the configuration key non-numeric? If so, use it.
3. Is the provided concrete value a string that represents a valid class or interface? If so, use it.
4. Is the provided concrete value an object? If so, generate an alias using `get_class`
5. If none of the above are true, use the provided key as an alias.

In the unlikely case that a concrete is provided that implements `League\Container\Definition\DefinitionInterface`, then the builder will assume that the user intends to provide their own definition and will circumvent its own build process in favor of the user's implementation.

In this case, aliases are resolved in the following way:

1. Does the configuration value contain an inner 'alias' key? If so, use it.
2. Is the configuration key non-numeric? If so, use it.
3. If none of the above are true, use the existing definition alias.

---

### The Service Provider Builder

[](#the-service-provider-builder)

A Service Provider Builder only requires an object that fulfills the `ServiceProviderAggregateInterface`. [Service Providers](https://container.thephpleague.com/3.x/service-providers/) may be built in bulk or one at a time.

Generating service providers in bulk using `build`:

```
use Maiorano\ContainerConfig\ServiceProviderBuilder;
use League\Container\ServiceProvider\ServiceProviderAggregate;

$builder = new ServiceProviderBuilder(new ServiceProviderAggregate);
$aggregate = $builder->build([
    CustomServiceProvider::class,
    new UserDefinedServiceProvider,
]);

```

In the unusual case that you would need the service provider builder's own `buildServiceProvider` method, single Service Providers can be added to the aggregate in the following way:

```
use Maiorano\ContainerConfig\ServiceProviderBuilder;
use League\Container\ServiceProvider\ServiceProviderAggregate;

$builder = new ServiceProviderBuilder(new ServiceProviderAggregate);
$aggregate = $builder->buildServiceProvider(CustomServiceProvider::class);

```

**IMPORTANT NOTE**
Even when using the `buildServiceProvider` method, the underlying call to the aggregate's `add` method results in the `ServiceProviderAggregate` being returned. Not the Service Provider itself.

---

### The Inflector Builder

[](#the-inflector-builder)

An Inflector Builder only requires an object that fulfills the `InflectorAggregateInterface`. [Inflectors](https://container.thephpleague.com/3.x/inflectors/) may be built in bulk or one at a time.

Generating inflectors in bulk using `build`:

```
use Maiorano\ContainerConfig\InflectorBuilder;
use League\Container\Inflector\InflectorAggregate;

$builder = new InflectorBuilder(new InflectorAggregate);
$aggregate = $builder->build([
    DefinitionAlias::class => 'methodName', // Simple inflector
    OtherDefinitionAlias::class => [ // Complete Inflector Structure
        'properties' => ['propery' => 'value'],
        'methods' => ['methodName' => ['arg1', 'arg2']],
    ],
]);

```

In the unusual case that you would need the inflector builder's own `buildInflector` method, single Inflectors can be added to the aggregate in the following way:

```
use Maiorano\ContainerConfig\InflectorBuilder;
use League\Container\Inflector\InflectorAggregate;

$builder = new InflectorBuilder(new InflectorAggregate);
$inflector = $builder->buildInflector(OtherDefinitionAlias::class, [
    'callback' => function($object){ var_dump($object); },
    'properties' => ['propery' => 'value'],
    'methods' => ['methodName' => ['arg1', 'arg2']],
]);

```

---

### Other Notes

[](#other-notes)

The purpose of the builder pattern is to construct *new* objects. While it's certainly possible to use existing aggregates and mutating them through Builder calls, this is not recommended, and could be considered a misuse of builders in general.

Instead, if you need fine-tuned control over your Container's various moving parts, it would be recommended that you interact with either the resulting Container or its aggregates directly after the initial build has completed.

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance54

Moderate activity, may be stable

Popularity9

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity57

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

2193d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/3030410?v=4)[Matt Maiorano](/maintainers/maiorano84)[@maiorano84](https://github.com/maiorano84)

---

Top Contributors

[![maiorano84](https://avatars.githubusercontent.com/u/3030410?v=4)](https://github.com/maiorano84 "maiorano84 (14 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

![Health badge](/badges/maiorano84-league-container-config/health.svg)

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

###  Alternatives

[consolidation/robo

Modern task runner

2.7k62.9M410](/packages/consolidation-robo)[roach-php/core

A complete web scraping toolkit for PHP

1.5k352.4k3](/packages/roach-php-core)[ec-europa/toolkit

Toolkit packaged for Drupal projects based on Robo.

38244.6k16](/packages/ec-europa-toolkit)

PHPackages © 2026

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