gacela-project / container
A minimalistic container dependency resolver
Fund package maintenance!
chemaclass.com/sponsor
Requires
- php: >=8.1
- psr/container: >=1.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.59
- phpstan/phpstan: ^1.11
- phpunit/phpunit: ^10.5
- psalm/plugin-phpunit: ^0.18
- symfony/var-dumper: ^5.4
- vimeo/psalm: ^5.25
README
Installation
composer require gacela-project/container
Usage
Get an instance by class name.
You can define a map between an interface and the concrete class that you want to create (or use) when that interface is found during the process of auto-wiring via its constructor.
Container
This container will auto-wire all inner dependencies from that class. Depending on the type of dependency it will resolve it differently:
primitive types
: it will use its default valueclasses
: it will instantiate it, and if they have dependencies, they will be resolved recursively as well.interfaces
: they will be resolved according to the bindings injected via the Container's constructor.
$bindings = [ AbstractString::class => StringClass::class, ClassInterface::class => new ConcreteClass(/* args */), ComplexInterface::class => new class() implements Foo {/** logic */}, FromCallable::class => fn() => new StringClass('From callable'), ]; $container = new Container($bindings); $instance = $container->get(YourClass::class); ////////////////////////////////////////////// # Extra: you can resolve closures on-the-fly using the container bindings $resolved = $container->resolve(function (ComplexInterface $i) { // your custom logic return $i->...; });
Example
A usage example in the Gacela Framework: AbstractClassResolver