flamecore / container
Simple and lightweight Dependency Injection Container
Fund package maintenance!
Liberapay
Requires
- php: >=7.2
- psr/container: 1.*
Requires (Dev)
- phpunit/phpunit: 8.*
- scrutinizer/ocular: 1.6.*
This package is auto-updated.
Last update: 2024-11-13 08:11:45 UTC
README
This library provides a simple and lightweight Dependency Injection Container.
Features
-
Interfaces: Extended container interfaces compatible with PSR-11
-
ModifiableContainerInterface
exposes methods to write and remove container entries. -
DefinableContainerInterface
exposes methods to define container entries. -
FactoryContainerInterface
exposes a method to create new object instances from a factory entry. -
LockableContainerInterface
exposes methods to lock and unlock container entries.
-
-
Classes: Advanced and feature-complete container implementations based on the interfaces
-
Container
implements common features like defining, writing, instantiating and removing container entries. -
LockableContainer
extends the above container to enable locking and unlocking of container entries.
-
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 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 object
You can also get a fallback value if an entry is not set:
$value3 = $container->getOr('foobar', function () { return new Fallback(...); }); // Returns Acme\MyApplication\Fallback object
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 7.2 installed on your system.
Contributors
If you want to contribute, please see the CONTRIBUTING file first.
Thanks to the contributors:
- Christian Neff (secondtruth)