danmurf / dependencyinjection
A lightweight, configurable, auto-wire dependency injection container.
dev-master
2020-08-02 10:41 UTC
Requires
- psr/container: ^1.0
Requires (Dev)
- leanphp/phpspec-code-coverage: ^4.2
- phpspec/phpspec: ^4.2
This package is auto-updated.
Last update: 2025-03-10 08:27:51 UTC
README
Dependency Injection Container
This is a lightweight dependency injection container, with various service location strategies, including autowiring.
To install, run:
composer require danmurf/dependencyinjection
Registered services
<?php use danmurf\DependencyInjection\ContainerFactory; class MyClass { public function myAction() { // Create a container somewhere in your kernel or bootstrap process $container = ContainerFactory::createRegistrationOnlyContainer(); // Create your services and register them with the container $myService = new MyService(); $container->register($myService, 'my.service.id'); // Access your services elsewhere in your application by their registered id... $container->has('my.service.id'); // true $myService = $container->get('my.service.id'); // ... or FQCN $myService = $container->get(MyService::class); } }
Configurable services
Have the container locate services based on services definitions.
$config = [ 'my.service.id' => [ 'class' => MyService::class, 'arguments' => [ [ 'type' => 'scalar', 'value' => 123, ], [ 'type' => 'service', 'value' => 'another.service.id', ], ], ], ]; $container = ContainerFactory::createConfigurableContainer($config); // The service will be located and instantiated using the defined config. $myService = $container->get('my.service.id');
Bind services to interfaces
Define which service you'd like to use for specific interfaces.
$config = [ MyInterface::class => [ 'service' => 'my.service.id' ], 'my.service.id' => [ 'class' => MyService::class, 'arguments' => [ [ 'type' => 'scalar', 'value' => 123, ], [ 'type' => 'service', 'value' => 'another.service.id', ], ], ], ]; $container = ContainerFactory::createConfigurableContainer($config); // The service bound to the interface will be returned $myService = $container->get(MyInterface::class);
Autowired services
Quickly create and use services by allowing the container to infer the config.
// This service location strategy still requires config for services with scalar constructor arguments $container = ContainerFactory::createAutoWireContainer([]); // Use the FQCN to get your autowired service from the container $myService = $container->get(MyService::class); // No config required!
License
This work is provided under the MIT License. See LICENSE.md for details.