amber / container
Simple PHP DI Container.
Installs: 1 432
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=7.4
- amber/cache: ^1.0@beta
- amber/collection: ^0.6@beta
- amber/validator: ^0.1@dev
- psr/container: ^1.0
Requires (Dev)
- php-coveralls/php-coveralls: ^2.1
- symfony/var-dumper: ^4.2
This package is auto-updated.
Last update: 2024-11-15 22:32:04 UTC
README
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...