All Projects → ultra-lite → container

ultra-lite / container

Licence: MIT license
An extremely lightweight DI container in PHP

Programming Languages

PHP
23972 projects - #3 most used programming language
Gherkin
971 projects

Projects that are alternatives of or similar to container

UniDi
Dependency Injection for Unity
Stars: ✭ 68 (+223.81%)
Mutual labels:  di-container
SimpleConfig
No description or website provided.
Stars: ✭ 22 (+4.76%)
Mutual labels:  di-container
hypo-container
A dependency injection container.
Stars: ✭ 16 (-23.81%)
Mutual labels:  di-container
DpdtInject
Highly efficient compile-time general purpose DI container based on C# source generators.
Stars: ✭ 25 (+19.05%)
Mutual labels:  di-container
Php Di
The dependency injection container for humans
Stars: ✭ 2,273 (+10723.81%)
Mutual labels:  container-interop
slim-php-di
Slim Framework PHP-DI container integration
Stars: ✭ 13 (-38.1%)
Mutual labels:  container-interop
Swinject
Dependency injection framework for Swift with iOS/macOS/Linux
Stars: ✭ 4,958 (+23509.52%)
Mutual labels:  di-container
simple-container
Yet another DI container
Stars: ✭ 12 (-42.86%)
Mutual labels:  di-container
refuel
Lightweight dependency injection engine and DI-driven tools.
Stars: ✭ 21 (+0%)
Mutual labels:  di-container
unbox
Fast, simple, easy-to-use DI container
Stars: ✭ 45 (+114.29%)
Mutual labels:  di-container
AutoDI
Dependency injection made simple.
Stars: ✭ 87 (+314.29%)
Mutual labels:  di-container

Build Status Scrutinizer Code Quality Latest Stable Version MIT Licence

logo Ultra-Lite Container

An ultra-lightweight DI container. The Ultra-Lite container is PSR-11 compliant. (Previous versions supported container-interop instead.)

Use anonymous functions as factories to specify your services.

This container also supports the Delegate Lookup pattern, and comes with a basic composite container.

Usage

Setting services individually

$container->set(
    'service-id',
    function (\Psr\Container\ContainerInterface $container) {
        return new \stdClass();
    }
);

Setting services from a config file

Add factory closures to the container inline, or using a config file as below.

Example config file:

return [
    'service-id' =>
        function (\Psr\Container\ContainerInterface $container) {
            return new \stdClass();
        },
];

Using config file:

$container->configureFromFile('/wherever/config/di.php');

Service retrieval

Get services out of the container like this:

$object = $container->get('service-id');

See if a service exists

Check if something is in the container like this:

$thingExists = $container->has('service-id');

Use with the Delegate Lookup pattern

If you're not using the Delegate Lookup concept from the Container-Interop standard, ignore this bit. If you are, you can do this:

$container = new \UltraLite\Container\Container();
$delegateContainer = new \UltraLite\CompositeContainer\CompositeContainer(); // or any delegate container
$compositeContainer->addContainer($container); // will vary for other composite containers
$container->setDelegateContainer($myCompositeContainer);

The Ultra-Lite Composite Container is an extremely lightweight delegate container you may wish to use.

When the container is asked for a service using get(), it will return it. It will pass the Composite Container into the factory closure, so it is from here that any dependencies of your service will be retrieved.

Alternatives

Ultra-Lite Container was originally inspired by Pimple, which still makes an excellent DI container in PHP. Container-Interop compliant wrappers are available. Another excellent project, Picotainer, is along similar lines, with the principle difference being that the dependencies are defined at the time of instantiation of the container.

Installation

composer require ultra-lite/container

Contributions

Contributions welcome.

You can run the tests with ./vendor/bin/behat -c tests/behat/behat.yml and ./vendor/bin/phpspec r -c tests/phpspec/phpspec.yml.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].