xp-forge / inject

Dependency injection for the XP Framework
0 stars 0 forks source link

Array binding #13

Closed thekid closed 8 years ago

thekid commented 8 years ago

This pull request adds the ability to bind arrays.

Example

Imagine we have a monitoring infrastructure and want to write monitoring information to more than one sink.

interface Monitoring
|- class Icinga implements Monitoring
`- class UpdateStatusWebsite implements Monitoring

Next, we want to define a composite to emit monitoring events to both sinks. We define it as follows:

class ToAllOf implements Monitoring {
  private $sinks;

  /**
   * Constructor
   *
   * @param  com.example.monitoring.Monitoring[] $sinks
   */
  #[@inject]
  public function __construct($sinks) {
    $this->sinks= $sinks;
  }

  // ...
}

Unfortunately, binding an array is currently not supported and will yield an error.

With this pull request, the binding code can be written as follows:

$inject->bind('com.example.monitoring.Monitoring[]', [Icinga::class, UpdateStatusWebsite::class]);
$inject->bind('com.example.monitoring.Monitoring', ToAllOf::class);

Inspired by https://github.com/stubbles/stubbles-ioc/blob/master/docs/list_bindings.md