GithubHelp home page GithubHelp logo

ultra-lite / container Goto Github PK

View Code? Open in Web Editor NEW
20.0 4.0 0.0 27 KB

An extremely lightweight DI container in PHP

License: MIT License

PHP 81.00% Gherkin 19.00%
container-interop di-container php psr-11 delegate-containers

container's Introduction

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.

container's People

Contributors

morozov avatar sam-burns avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

container's Issues

PSR compatibility

Hello!
We have some problems with PSR compatibility..

[29-Apr-2021 10:21:17 Europe/Moscow] PHP Fatal error: Declaration of UltraLite\Container\Container::get(string $serviceId) must be compatible with Psr\Container\ContainerInterface::get($id) in path_to_project/vendor/ultra-lite/container/src/UltraLite/Container/Container.php on line 47

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.