flamecore/container

Simple and lightweight Dependency Injection Container

dev-master 2020-05-01 13:28 UTC

This package is auto-updated.

Last update: 2020-05-01 14:12:08 UTC


README

Latest Stable Build Status Scrutinizer Coverage License

This library provides a simple and lightweight Dependency Injection Container.

Features

  • Interfaces: Extended container interfaces compatible with PSR-11

    • ModifiableContainerInterface

    • LockableContainerInterface

  • Classes: Advanced and feature-complete container implementations based on the interfaces

    • Container

    • LockableContainer

Usage

To make use of the API, include the vendor autoloader and use the classes:

namespace Acme\MyApplication;

use FlameCore\Container\Container;

$container = new Container();

You can define() items to require a value type. To assign values to items, use the set() method.

$container->define('foo', 'Acme\MyApplication\Config'); // or Config::class
$container->set('foo', new Config());
if ($container->has('foo')) {
    $value = $container->get('foo'); // Returns Acme\MyApplication\Config object
}

This works also with arrays and scalar types:

$container->define('bar', Container::TYPE_STRING); // or ':string'
$container->set('bar', 'value');
$value2 = $container->get('bar'); // Returns "value"

You can also get a fallback value if an item is not set:

$value3 = $container->getOr('foobar', 'fallback value'); // Returns "fallback value"

Installation

Install Composer if you don't already have it present on your system.

To install the library, run the following command and you will get the latest version:

$ php composer.phar require flamecore/container

Requirements

  • You must have at least PHP version 5.4 installed on your system.

Contributors

If you want to contribute, please see the CONTRIBUTING file first.

Thanks to the contributors:

  • Christian Neff (secondtruth)