GithubHelp home page GithubHelp logo

matt-allan / container Goto Github PK

View Code? Open in Web Editor NEW
17.0 3.0 1.0 13 KB

A lightweight container-interop compatible DI container.

License: MIT License

PHP 100.00%
psr-11 container-interop dependency-injection

container's Introduction

Container

Latest Version on Packagist Software License Build Status

Container is a lightweight dependency injection container. It's compatible with the PSR-11 container interface, so you can use it with lots of different projects out of the box.

The container is really simple; It's basically an array of identifier => callable mappings, and the callable is invoked to get the resulting object.

New to dependency injection and containers? I wrote a blog post explaining dependency injection and how this container works.

This package is compliant with PSR-1, PSR-2, PSR-4, and PSR-11.

Install

Via Composer

$ composer require yuloh/container

Usage

Adding Entries

Adding an entry to the container is really simple. Just specify the identifier as the first argument, and a callable as the second argument.

use Yuloh\Container\Container;

$container = new Container();

$container->set(Psr\Log\LoggerInterface::class, function () {
    $logger = new Monolog\Logger();
    $logger->pushHandler(new StreamHandler('error.log'));
    return $logger;
});

The closure will receive the container as it's only argument, so you can use the container to resolve the dependencies of your entry.

$container->set('db', function ($container) {
    $db = new Database();
    $logger = $container->get(Psr\Log\LoggerInterface::class);
    $db->setLogger($logger);
    return $db;
});

All entries are shared (singletons), which means an entry will be resolved once and reused for subsequent calls.

Getting Entries

To check if an entry exists, use has. To get an entry, use get. If you are just retrieving entries you can typehint Psr\Container\ContainerInterface instead of the actual Container.

if ($container->has('db')) {
    $db = $container->get('db');
}

Why Another Container?

There are a lot of containers out there. I was working on a project and wanted a lightweight default container and couldn't find what I wanted. This container:

  • Implements container-interop.
  • Supports PHP 5.4+
  • Supports adding entries at runtime.
  • Is incredibly lightweight, with the bare minimum of code to support the first 3 goals.

Testing

$ composer test
$ composer cs

container's People

Stargazers

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

Watchers

 avatar  avatar  avatar

Forkers

qa1

container's Issues

Parameters

It would be good to set single parameters to the container like this:
$container->set('foo', 'bar');

Doing it like this would work but its not good:

$container->set('foo', function () {
    return 'bar';
});

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.