2017 © Pedro Peláez
 

library autowire

Autowire-able container

image

studiow/autowire

Autowire-able container

  • Wednesday, April 26, 2017
  • by studiowbe
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Container Autowire

This package provides a sort of autowiring wrapper for any psr/container-compatible container., (*1)

Installation

The (highly) recommended way to install studiow/autowire is by using Composer, (*2)

composer require studiow/autowire

How to use

In my application, I have a Configuration object. There is also an interface for objects that can use this Configuration., (*3)

class Configuration
{
    //...
}

interface HasConfiguration
{
    public function setConfiguration(Configuration $configuration);
    public function getConfiguration():Configuration
}

I like to use league/container as a DI container, so let's do just that:, (*4)

$container = new League\Container\Container();
$container->share(Configuration::class);

Now I would like to make sure that our Configuration object gets passed around to any object that needs it. One way to do this is by defining all classes in the container:, (*5)

class AClassWithConfiguration implements HasConfiguration{
    //...
}

$container->share(AClassWithConfiguration::class, function() use($container){
    $obj = new AClassWithConfiguration();
    $obj->setConfiguration($container->get(Configuration::class));
    return $obj;
});

This works great! Unfortunately, we'll need to this for any class that needs the Configuration object. Boring!, (*6)

Let's see what we can do if we wrap our Container in an autowire container:, (*7)

$container = new League\Container\Container();
$container->share(Configuration::class);

$awContainer = Studiow\Autowire\Container($container); 

This looks familiar: it's the same DI container as before, but it now gets wrapped into an autowire container., (*8)

Here's what we want the autowire container to do: if an object implements the HasConfiguration interface, use the setConfiguration method to pass on the Configuration object, (*9)

$awContainer->attach(HasConfiguration::class, function($item, $awContainer){
   $item->setConfiguration($awContainer->get(Configuration::class)); 
});

The attach method takes 2 arguments: the name of the interface, and a callback. The callback also has 2 arguments: the object we're dealing with, and the autowire container., (*10)

The callback will now automatically be executed when resolving objects from the container, (*11)

class AClassWithConfiguration implements HasConfiguration{
    //...
}

$container->share(AClassWithConfiguration::class});

$awContainer = Studiow\Autowire\Container($container); 
$obj = $awContainer->get(AClassWithConfiguration::class);

$obj->getConfiguration();
//Configuration object was automatically injected

If you want, you can bypass "add classes to container" too:, (*12)

$obj = new AClassWithConfiguration();
$obj = $awContainer->applyCallbacks($obj);
$obj->getConfiguration();
//Configuration object was injected

The Versions

26/04 2017

dev-master

9999999-dev

Autowire-able container

  Sources   Download

MIT

The Requires

 

The Development Requires

26/04 2017

v0.0.1

0.0.1.0

Autowire-able container

  Sources   Download

MIT

The Requires

 

The Development Requires