Skip to content

Repository files navigation

Runner IoC Container

A small IoC Container for PHP

StyleCI

Installation

$ composer require runner/container

Usage

create an instance of the container, and bind services into the container with a name.

basic binding

useRunner\Container\Container;
$container = newContainer();
$container->bind('stack', SplStack::class);
$container->make('stack');
$container->bind(ArrayAccess::class, function () {
returnnewArrayObject();
});

binding implementation

use an interface name as name and bind a concrete implementation to it

$container->bind(ArrayAccess::class, function () {
returnnewArrayObject();
});
$container->make(ArrayAccess::class);

binding singleton

$container->bind(
'db', function () {
returnnewPDO();
}, true
);
$container->bind();

binding instance

just another way to binding singleton

$pdo = newPDO();
$container->instance('db', $pdo);

alias binding

bind an alias as concrete to a registered service

$container->bind(CacheInterface::class, function () {
returnnewFileCache();
});
$container->bind('cache', CacheInterface::class, true);
$container->make('cache');

have fun :)

contextual binding

bind different implementation to classes while doing injecting

$container->bind(CacheInterface::class, function () {
returnnewFileCache();
});
$container->bind('redis_cache', function () {
returnnewRedisCache();
});
$container->bindContext(
PageController::class,
CacheInterface::class,
function (Container$container) {
return$container->make('redis_cache');
}
);
$controller = $container->make(PageController::class);

References

License

MIT

About

runner framework

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages