2017 © Pedro Peláez
 

library container

A lightweight container-interop compatible DI container

image

yuloh/container

A lightweight container-interop compatible DI container

  • Monday, April 24, 2017
  • by yuloh
  • Repository
  • 1 Watchers
  • 14 Stars
  • 56 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 6 % Grown

The README.md

Container

Latest Version on Packagist ![Software License][ico-license] Build Status, (*1)

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., (*2)

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

New to dependency injection and containers? I wrote a blog post explaining dependency injection and how this container works., (*4)

This package is compliant with PSR-1, PSR-2, PSR-4, and PSR-11., (*5)

Install

Via Composer, (*6)

``` bash $ composer require yuloh/container, (*7)


## 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. ``` php 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., (*8)

$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., (*9)

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., (*10)

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:, (*11)

  • 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

bash $ composer test $ composer cs, (*12)

The Versions

24/04 2017

dev-master

9999999-dev https://github.com/yuloh/container

A lightweight container-interop compatible DI container

  Sources   Download

MIT

The Requires

 

The Development Requires

container yuloh

20/04 2017

v1.0.0

1.0.0.0 https://github.com/yuloh/container

A lightweight container-interop compatible DI container

  Sources   Download

MIT

The Requires

 

The Development Requires

container yuloh

01/09 2016

dev-autowiring

dev-autowiring https://github.com/yuloh/container

A lightweight container-interop compatible DI container

  Sources   Download

MIT

The Requires

 

The Development Requires

container yuloh

11/04 2016

v0.1.0

0.1.0.0 https://github.com/yuloh/container

A lightweight container-interop compatible DI container

  Sources   Download

MIT

The Requires

 

The Development Requires

container yuloh