PHPackages                             delights/box - 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. delights/box

ActiveLibrary

delights/box
============

Box, a smart autowiring container for PHP.

0.6.2(6y ago)242MITPHPPHP ^7.4

Since May 3Pushed 4y ago2 watchersCompare

[ Source](https://github.com/felixdorn/box)[ Packagist](https://packagist.org/packages/delights/box)[ RSS](/packages/delights-box/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (5)Versions (9)Used By (0)

 [ ![](https://camo.githubusercontent.com/928e195c2b96128d0f49459218fd189a0ed6ab6ad1888962c666b3436da89e07/68747470733a2f2f7265732e636c6f7564696e6172792e636f6d2f6479336a78686962612f696d6167652f75706c6f61642f76313538383439333038342f6c6f676f5f7278387935732e737667) ](https://github.com/felixdorn/box)

 Box, a smart autowiring container for PHP.
============================================

[](#--------box-a-smart-autowiring-container-for-php----)

 [![CI](https://github.com/felixdorn/box/workflows/CI/badge.svg?branch=master)](https://github.com/felixdorn/box/workflows/CI/badge.svg?branch=master) [![Style CI](https://camo.githubusercontent.com/f3a9dde135fbe6c298fc10a7945b55c2b2ae1963c65464ae2459615f503c1142/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3236303835383331342f736869656c643f6272616e63683d6d6173746572267374796c653d666c6174)](https://camo.githubusercontent.com/f3a9dde135fbe6c298fc10a7945b55c2b2ae1963c65464ae2459615f503c1142/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3236303835383331342f736869656c643f6272616e63683d6d6173746572267374796c653d666c6174) [![Codecov](https://camo.githubusercontent.com/f924084ca7a381ff7ec330466b26782b7dcc57089c371448730c4f3be0edfe7c/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f66656c6978646f726e2f626f78)](https://camo.githubusercontent.com/f924084ca7a381ff7ec330466b26782b7dcc57089c371448730c4f3be0edfe7c/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f66656c6978646f726e2f626f78) [![License](https://camo.githubusercontent.com/09b1fbe8ddc54800eed15ab6602db3050ef98a6145e8fecdedf4e6764d70e85f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f64656c69676874732f626f78)](https://camo.githubusercontent.com/09b1fbe8ddc54800eed15ab6602db3050ef98a6145e8fecdedf4e6764d70e85f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f64656c69676874732f626f78) [![Last Version](https://camo.githubusercontent.com/5835e81755110ebfef5143566b7ac5430609a4c5a46ff634cf1c3682ec869245/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f64656c69676874732f626f78)](https://camo.githubusercontent.com/5835e81755110ebfef5143566b7ac5430609a4c5a46ff634cf1c3682ec869245/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f64656c69676874732f626f78)

Getting started
---------------

[](#getting-started)

### Installation

[](#installation)

This library can be installed using composer, if you don't have it already, [download it](https://getcomposer.org/download).

You can either run this command :

```
composer require delights/box
```

Or by adding a requirement in your `composer.json` :

```
{
  "require": {
    "delights/box": "0.3.0"
  }
}
```

Don't forget to run `composer install` after adding the requirement.

Before diving into the autowiring and stuff, we need to create a container instance.

```
use Delights\Box\Container;

$container = new Container();
```

Bindings
--------

[](#bindings)

You can bind something into the container, it works just like a key =&gt; value array.

```
$container->bind('key', 'value');
echo $container->resolve('key'); // prints "value"
```

You may want to bind a class in a closure if you need more specific arguments,

```
$container->bind(Crawler::class, function () {
    return new Crawler('f4dg65gd6fg465g');
});
```

Every time you ask for a `Crawler::class`, this closure will be executed and a fresh instance of `Crawler` will be created. To avoid that, you can use the `singleton` method.

```
$container->singleton(Connection::class, function () {
    return new Connection();
});
```

Now, the `Connection` class will be instantiated once, and the closure never executed again.

You may want to use an interface for your `Crawler` so you can easily swap instances and stuff. However, you want to retrieve the `Crawler` instance when `CrawlerInterface` is needed. Instead of manually bind these classes, you can use the `bindToImplementation` method.

```
$container->bindToImplementation(CrawlerInterface::class, Crawler::class);
// same as
$container->bindToImplementation(CrawlerInterface::class, new Crawler);
// same as
$container->bindToImplementation(CrawlerInterface::class, function () {
    return new Crawler;
});
```

Resolving
---------

[](#resolving)

Smartly resolving parameters is the primary goal of this package. You can resolve anything that needs parameter including, constructors, closures, methods, functions. We even support resolving properties if there is an annotation .

### Autowiring

[](#autowiring)

Autowiring allows the container to auto-magically resolve dependencies using the Reflection API.

```
use Delights\Box\Container;
$container = new Container();

class SomeClass {}
$container->resolve(SomeClass::class); // returns an instance of "SomeClass"

class SomeOtherClass {
    public function __construct(SomeClass $dep) {
        $this->dep = $dep;
    }
}
$container->resolve(SomeOtherClass::class);
// returns an instance of "SomeOtherClass"
// with $this->dep set to an instance of "SomeClass"
```

The container can resolve non-typed argument but only in two cases : they should either allow null or have a default value.

### Resolving an object method

[](#resolving-an-object-method)

```
class PostsRepository {
    public function all() {
        return ['My article'];
    }
}

class PostController {
    public function index(PostsRepository $repository) {
        return $repository->all();
    }
}

$container->call(PostController::class, 'index');
// This returns |'My article']
```

### Resolving a Closure

[](#resolving-a-closure)

```
$container->closure(function (SomeClass $class) {
    return $class instanceof SomeClass;
}); // returns true
```

### Resolve with arbitrary parameters

[](#resolve-with-arbitrary-parameters)

```
class Vec2 {
    protected int $x;
    protected int $y;

    public function __construct(int $x, int $y) {
        $this->x = $x;
        $this->y = $y;
    }
}
$container->resolve(Vec2::class, [
    'x' => 2,
    'y' => 4
]); // returns an instance of "Vec2"
```

The order does not matter, in our example, it can be either `[x, y]` or `[y, x]`.

You can pass arbitrary parameters to any resolve function.

Singleton
---------

[](#singleton)

You may want to have a global Container that keeps the same bindings across your app.

> Usually, you want to avoid this kind of behavior.

```
use Delights\Box\PersistentContainer;

PersistentContainer::getInstance();
```

Here, we this is the first time you call it, a new instance will be created. However, if you already created one, the same instance will be returned.

### Static Proxies

[](#static-proxies)

There is a little shortcut.

```
use Delights\Box\PersistentContainer;

// instead of this
PersistentContainer::getInstance()->bind('some', 'thing');

// you can do that
PersistentContainer::bind('some', 'thing');
```

This is available for any of the `PersistentContainer` public methods.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Félix Dorn](https://felixdorn.fr)

Licensing
---------

[](#licensing)

Copyright 2020 Félix Dorn

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 98.4% 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 ~2 days

Total

8

Last Release

2190d ago

### Community

Maintainers

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

---

Top Contributors

[![felixdorn](https://avatars.githubusercontent.com/u/55788595?v=4)](https://github.com/felixdorn "felixdorn (61 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (1 commits)")

---

Tags

containerdependency-injectionpackagephppsr-11psr-11-compliant

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/delights-box/health.svg)

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

###  Alternatives

[pimple/pimple

Pimple, a simple Dependency Injection Container

2.7k130.5M1.4k](/packages/pimple-pimple)[neos/flow

Flow Application Framework

862.0M451](/packages/neos-flow)[api-platform/state

API Platform state interfaces

223.4M57](/packages/api-platform-state)[internal/dload

Downloads binaries.

98142.7k10](/packages/internal-dload)[symfony/json-streamer

Provides powerful methods to read/write data structures from/into JSON streams.

14440.0k8](/packages/symfony-json-streamer)[rubix/server

Deploy your Rubix ML models to production with scalable stand-alone inference servers.

632.3k](/packages/rubix-server)

PHPackages © 2026

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