Skip to content

systemson/container

Repository files navigation

GitHub last commit Latest Stable Version Latest Beta Version PHP from Packagist Build Status Coverage Status Total Downloads GitHub

Container

Simple PHP DI Container.

Instalation

composer require amber/container

API (Draft)

Basic Usage (PSR-11 compliance)

use Amber\Container\Container;

$container = new Container();

bind()

Binds an item to the Container's map by a unique key.

bind(string $key, mixed $value) : boolean

param string $key The unique item's key.

param mixed $value The value of the item.

return bool True on success. False if key already exists.

Bind an Service to the container by a unique key.

$container->bind($key, $value);

Or bind a class like this.

$container->bind($class);

get()

Gets an item from the Container's map by its unique key

get(string $key): mixed

param string $key *The unique item's key.

return mixed The value of the item.

$container->get($key);

has()

Checks for the existance of an item on the Container's map by its unique key.

has(string $key): bool param string $key *The unique item's key. return bool

$container->has($key);

unbind()

Unbinds an item from the Container's map by its unique key.

unbind(string $key): bool

param string $key *The unique item's key.

return bool true on success, false on failure.

$container->unbind($key);

Multiple actions

bindMultiple()

$container->bindMultiple([$key => $value]);

getMultiple()

$container->getMultiple($keys);

unbindMultiple()

$container->unbindMultiple($keys);

Advanced Usage

make()

Binds and Gets a Service from the Container's map by its unique key.

make(string $class): mixed

param string $class The item's class.

return mixed The value of the item.

$container->make($class);

register()

Binds an class to the Container and return the ServiceClass.

register(string $class, string $alias = null): ServiceClass

param string $class The item's class.

param string $alias The item's alias.

return ServiceClass

$container->register($class);

singleton()

Binds an class to the Container as singleton and return theServiceClassservice.

singleton(string $class, string $alias = null): ServiceClass

param string $class The item's class.

param string $alias The item's alias.

return ServiceClass

$container->singleton($class);

getClosureFor()

Gets a closure for a method of the provided class.

getClosureFor(string $class, string $method, array $binds = []): Closure

param string $class The class to instantiate.

param string $method The class method to call.

param array $binds The arguments for the service.

return Closure

$container->getClosureFor($class, $method);

More coming soon...