nacosvel / container-interop
Achieve compatibility and interoperability among different container objects.
Requires
- php: >=8.0
- psr/container: ^1.1.1|^2.0.1
This package is auto-updated.
Last update: 2024-11-22 13:14:24 UTC
README
Achieve compatibility and interoperability among different container objects.
The library discovers available PSR-11 container implementations by searching for a list of known classes that implement the relevant interfaces, and returns an instance of the first one found. It supports binding, instantiation, and allows executing specific callbacks or logic when a class or dependency is resolving (i.e., instantiated or injected).
Installation
You can install the package via Composer:
composer require nacosvel/container-interop
Usage
Default Method Names
If your container uses the standard method names (bind, make, resolving), you can simply call:
use Nacosvel\Container\Interop\Discover; Discover::container();
discovers available PSR-11 container implementations by searching for a list of known classes that implement the relevant interfaces
Custom Method Names
If your container uses different method names, specify them as follows:
use Nacosvel\Container\Interop\Discover; Discover::container( container: $container, bind: 'customBindMethod', make: 'customMakeMethod', resolving: 'customResolvingMethod' );
container
: The container instance you want to work with.bind
: The name of the method used for binding dependencies.make
: The name of the method used for creating instances.resolving
: The name of the method used for resolving dependencies.
Accessing the Application
Let's look at a simple example:
use Nacosvel\Container\Interop\Application; $application = Application::getInstance();
The application
function returns the Application instance:
$application = application();
Accessing the Container
Once configured, third-party packages can access the container using:
use Nacosvel\Container\Interop\Application; $container = Application::getInstance()->getContainer();
If needed, you may specify which Container
instance you would like to access:
$container = application()->getContainer();
Accessing the Service
You may pass a class or interface name to resolve service from the container:
$cache = application(Cache::class); $cache = application()->make(Cache::class);
This method ensures compatibility with various container implementations without worrying about their specific methods.
License
Nacosvel Container Interop is made available under the MIT License (MIT). Please see License File for more information.