league/container
PSR-11–compliant dependency injection container from The PHP League. Register services, factories and shared instances, then resolve dependencies with autowiring support. Modern PHP (8.3+) with full docs, tests, and MIT license.
Inflectors allow you to define the manipulation of an object of a specific type as the final step before it is returned by the container.
This is useful for example when you want to invoke a method on all objects that implement a specific interface.
Imagine that you have a LoggerAwareInterface and would like to invoke the method called setLogger passing in a logger every time a class is retrieved that implements this interface.
<?php declare(strict_types=1);
$container = new League\Container\Container;
$container->add('Acme\Logger');
$container->add('Acme\LoggerAwareClass'); // implements LoggerAwareInterface
$container->add('Acme\Other\LoggerAwareClass'); // implements LoggerAwareInterface
$container
->inflector('LoggerAwareInterface')
->invokeMethod('setLogger', ['Acme\Logger']) // Acme\Logger will be resolved via the container
;
Now instead of adding a method call to each class individually we can simply define an inflector to invoke the method for every class of that type.
How can I help you explore Laravel packages today?