PHPackages                             clementdecou/qube - 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. clementdecou/qube

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

clementdecou/qube
=================

Qube is a lightweight and simple dependency injection container.

v1.0(2y ago)17MITPHP

Since Jul 29Pushed 2y ago1 watchersCompare

[ Source](https://github.com/Amorfx/qube)[ Packagist](https://packagist.org/packages/clementdecou/qube)[ GitHub Sponsors](https://github.com/amorfx)[ RSS](/packages/clementdecou-qube/feed)WikiDiscussions main Synced today

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

Qube
====

[](#qube)

[![Latest Version on Packagist](https://camo.githubusercontent.com/f33cb0928882d6f68c15f1eab554a8dfaeb7fc0b8e70640140e047e7badca274/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f636c656d656e746465636f752f717562652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/clementdecou/qube)[![Tests](https://github.com/Amorfx/qube/actions/workflows/run-tests.yml/badge.svg)](https://github.com/Amorfx/qube/actions/workflows/run-tests.yml)[![Total Downloads](https://camo.githubusercontent.com/6249255034a7aeddd2fa69ca2c87832a8ec032d14d87550b889016ffe5f8a896/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f636c656d656e746465636f752f717562652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/qube/qube-php)

Qube is a lightweight and simple dependency injection container.

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

[](#installation)

You can install the package via composer:

```
composer require clementdecou/qube
```

Usage
-----

[](#usage)

Creating a container is a matter of creating a Container instance:

```
use Qube\Container;

$container = new Container();
```

Or use the helper function:

```
use Amorfx\Qube\qube;

$container = qube();
```

It will return a unique instance of the container even if you call it multiple times.

Defining Services
-----------------

[](#defining-services)

Register a service with a unique identifier so that it can be retrieved later. Services can be objects or closures.

```
$container->set('my_service', new MyService());
```

### Defining Services with a factory

[](#defining-services-with-a-factory)

If a service has multiple dependencies you can define a factory for your service.

```
use Amorfx\Qube\DependencyInjection\ContainerInterface;

$container->set('my_service', function (ContainerInterface $container) {
    return new MyService($container->get('service1'), $container->get('service2'));
});
```

Notice that the anonymous function has access to the current container instance, allowing references to other services or parameters. As objects are only created when you get them, the order of the definitions does not matter.

### Defining Services not shareable

[](#defining-services-not-shareable)

By default, each time you get a service, the same instance is returned. If you want a different instance to be returned for all calls, use the share parameter.

```
$container->set('my_service', new MyService(), false);
```

Now, each call to 'my\_service' returns a new instance of the service.

### Defining Services with a tag

[](#defining-services-with-a-tag)

You can tag a service with a specific tag. This is useful when you want to retrieve multiple services with the same tag.

```
$container->set('my_service', new MyService(), tags: ['tag1', 'tag2']);
```

Retrieve a service
------------------

[](#retrieve-a-service)

Retrieve a previously registered service using its identifier.

```
$service = $container->get('my_service');
```

### Retrieve multiple services with tags

[](#retrieve-multiple-services-with-tags)

It will return a generator with all services tagged. You can iterate over it or use the `iterator_to_array` function to get an array.

```
$services = $container->getByTag('tag1');
```

Defining Parameters
-------------------

[](#defining-parameters)

Defining a parameter allows to ease the configuration of your container from the outside and to store global values:

```
$container->setParameter('my_param', 'my_value');
```

Retrieve a parameter
--------------------

[](#retrieve-a-parameter)

Retrieve a previously registered parameter using its identifier.

```
$param = $container->getParameter('my_param'); // return my_value
```

Extending a Container
---------------------

[](#extending-a-container)

If you use the same libraries over and over, you might want to reuse some services from one project to the next one. Package your services into a provider by implementing Qube\\DependencyInjection\\ServiceProviderInterface:

```
use Amorfx\Qube\DependencyInjection\ServiceProviderInterface;
use Amorfx\Qube\DependencyInjection\Container;
class ServiceProvider implements ServiceProviderInterface
{
    public function register(Container $container): void
    {
        // register services and parameters or other providers
    }
}
```

Then register your provider into the container:

```
$container->registerProviders([new ServiceProvider()]);
// OR
$container->registerProvider(new ServiceProvider());
```

Using the builder / or configuration file
-----------------------------------------

[](#using-the-builder--or-configuration-file)

You can use the builder to create your container. It will allow you to use a configuration file to define your services and parameters.

```
use Amorfx\Qube\DependencyInjection\ContainerBuilder;
use Amorfx\Qube\quBuilder;

$builder = new ContainerBuilder(configFilePath: __DIR__ . '/config.php');
$container = $builder->get();

// OR use helper function
$container = quBuilder()
->fromConfig(__DIR__ . '/config.php')
->get();

// Or directly from an array
$container = (new ContainerBuilder(config: [...]))->get();
```

The configuration file must return an array with the following structure:

```
return [
    'parameters' => [
        //list of parameter identifier with their value
        'param1' => 'value1',
        // ...
    ],

    'services' => [ // list of you services

        'service1' => [ // the service identifier
            'object' => // an instance of your service or use factory
            'factory' => function (ContainerInterface $container) {
                // the closure to return your service instance
            },
            'tags' => [], // a list of tags
            'share' => false, // if the service is shareable, default is true
        ],
        // ...
    ],

    'providers' => [
        // the list of your providers class name
    ],
];
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Clément Décou](https://github.com/amorfx)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

21

—

LowBetter than 18% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity44

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

1070d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/7e7eac39ebf589fa8398ec49675979aa783bd1b577b456000dbb35524ba7fb08?d=identicon)[Amorfx](/maintainers/Amorfx)

---

Top Contributors

[![Amorfx](https://avatars.githubusercontent.com/u/3883902?v=4)](https://github.com/Amorfx "Amorfx (22 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/clementdecou-qube/health.svg)

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

###  Alternatives

[symfony/dependency-injection

Allows you to standardize and centralize the way objects are constructed in your application

4.2k455.6M9.6k](/packages/symfony-dependency-injection)[illuminate/contracts

The Illuminate Contracts package.

706130.3M13.3k](/packages/illuminate-contracts)[illuminate/container

The Illuminate Container package.

31182.0M2.4k](/packages/illuminate-container)[ecotone/ecotone

Enterprise architecture layer for Laravel and Symfony — CQRS, Event Sourcing, Durable Workflows (Sagas, Orchestrators), Projections, and Outbox messaging via PHP attributes.

564576.7k53](/packages/ecotone-ecotone)[symfony/type-info

Extracts PHP types information.

20069.8M270](/packages/symfony-type-info)[civicrm/civicrm-core

Open source constituent relationship management for non-profits, NGOs and advocacy organizations.

751291.4k43](/packages/civicrm-civicrm-core)

PHPackages © 2026

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