This library provides a simple and lightweight Dependency Injection Container.
-
Interfaces: Extended container interfaces compatible with PSR-11
-
ModifiableContainerInterfaceexposes methods to write and remove container entries. -
DefinableContainerInterfaceexposes methods to define container entries. -
FactoryContainerInterfaceexposes a method to create new object instances from a factory entry. -
LockableContainerInterfaceexposes methods to lock and unlock container entries.
-
-
Classes: Advanced and feature-complete container implementations based on the interfaces
-
Containerimplements common features like defining, writing, instantiating and removing container entries. -
LockableContainerextends the above container to enable locking and unlocking of container entries.
-
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 use get() and has() on the container as usual. You can also set() and remove() entries.
$container->set('foo', new Configuration(...));
if ($container->has('foo')) {
$value = $container->get('foo'); // Returns Acme\MyApplication\Configuration object
$container->remove('foo');
}You can also use object factories:
$container->set('bar', function () {
return new Configuration(...);
});
$value2 = $container->get('bar'); // Returns Acme\MyApplication\Configuration objectYou can also get a fallback value if an entry is not set:
$value3 = $container->getOr('foobar', function () {
return new Fallback(...);
}); // Returns Acme\MyApplication\Fallback objectInstall 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
- You must have at least PHP version 7.2 installed on your system.
If you want to contribute, please see the CONTRIBUTING file first.
Thanks to the contributors:
- Christian Neff (secondtruth)