opsbears / piccolo-dic-auryn
Requires
- php: ^7.0
- opsbears/piccolo: ^1.0
- rdlowrey/auryn: ^1.4
Requires (Dev)
Provides
- opsbears/piccolo-dic: 1.0.0
This package is not auto-updated.
Last update: 2020-08-19 06:36:58 UTC
README
This module uses the Auryn Dependency Injection Container to provide dependency injection services.
Installation
This module can be installed via composer:
composer require opsbears/piccolo-dic-auryn
Configuration
Sharing/reusing a class
To share a class or instance, you can use the share()
function:
$dic = new AurynDependencyInjectionContainer();
$dic->share(new MyService());
$dic->share(MyOtherService::class);
Creating an abstract class / interface alias
$dic = new AurynDependencyInjectionContainer();
$dic->alias(MyInterface::class, MyImplementation::class);
Setting / overriding class parameters
By default, Auryn will detect dependencies by their type hints. Sometimes this is not possible / advantageous, so you
can use the setClassParameters()
method to override them. Parameters that are not provided are attempted to be
looked up via reflection.
$dic = new AurynDependencyInjectionContainer();
$dic->setClassParameters(MyClass::class, ['someParameter' => 'someValue']);
Attention! This differs from the Auryn configuration! In Auryn, you normally prepend lieral values with a colon
(:
). In Piccolo you don't!
Usage
Creating a class
To create an instance of a class or interface (using aliases), use the make()
method:
$dic = new AurynDependencyInjectionContainer();
//...
$dic->make(MyClass::class)->doSomething();
Executing a method
Sometimes you will want to execute a method while automatically detecting the required parameters. This can be done
with the execute()
method.
$dic = new AurynDependencyInjectionContainer();
$myClass = new MyClass();
//...
$dic->execute([$myClass, 'myMethod'], ['additionalArgument1' => 'value']);