Inflectors

What are inflectors ?

Certain classes are used often by others.

If we needed to bind them using the container manually that would be really time-wasting and that's why use inflectors to make that job for us.

Inflectors are automatic binder that inject a dependency to a class using a predefined method when the class in question is implementing a class.

By default, we have the following inflectors available :

How to add my own inflectors ?

Registering inflectors is possible at the level from Service Providers.

For that the service provider will have to first implement the interface LaunchpadCore\Container\HasInflectorInterface and the trait LaunchpadCore\Container\InflectorServiceProviderTrait.

This will provide access to a new method `register_inflector` which will be able to register inflectors inside the `define` method:
class Provider extends AbstractServiceProvider implements HasInflectorInterface {
   use InflectorServiceProviderTrait; 
   
   public function define() {
    $this->register_inflector(MyInterface::class);
   }
} 

Inflector structure

An inflector is always split into two parts:

  • An interface to recognize classes that we want to execute logic on.

  • An action to make on that class as injecting a property using a special method.

For that we can pass the interface to the register_inflector method and then:

  • If we want to call a method we can use the method add_method:

class Provider extends AbstractServiceProvider implements HasInflectorInterface {
   use InflectorServiceProviderTrait; 
   
   public function define() {
    $this->register_inflector(MyInterface::class)
        ->add_method('my_method', [
            10,
            'second_parameter'
        ]);
   }
} 

-If we want to inject a property we can use the method add_property:

class Provider extends AbstractServiceProvider implements HasInflectorInterface {
   use InflectorServiceProviderTrait; 
   
   public function define() {
    $this->register_inflector(MyInterface::class)
        ->add_property('my_property', false);
   }
} 

Last updated