2017 © Pedro Peláez
 

library disclosure

PHP5 dependency injection framework

image

monomelodies/disclosure

PHP5 dependency injection framework

  • Saturday, April 9, 2016
  • by monomelodies
  • Repository
  • 1 Watchers
  • 0 Stars
  • 750 Installations
  • PHP
  • 1 Dependents
  • 1 Suggesters
  • 0 Forks
  • 0 Open issues
  • 14 Versions
  • 1 % Grown

The README.md

Disclosure

PHP5 dependency injection and service locator framework. Most existing DI or Inversion of Control (IoC) solutions depend on extensive configuration files to define dependencies. This sucks; Disclosure is better and simpler (we think)., (*1)

As of version 2.0, Disclosure is fully compatible with the (upcoming) PSR recommendation for Container objects. A copy of the as-yet unrealeased PSR\Container interface is included in the package (until they finally realease it...)., (*2)

Installation

composer require monomelodies/disclosure

Manual installation

  1. Get or clonse the code;
  2. Register /path/to/disclosure/src for the namespace Disclosure\\ in your PSR-4 autoloader;
  3. Register /path/to/disclosure/psr for the namespace Psr\\Container\\ in your PSR-4 autoloader

Usage

Add your dependencies to a Container object somewhere. It often makes sense to do this in a central file (e.g. src/dependencies.php), but it's also perfectly fine to do it alongside your class definitions., (*3)

<?php

use Disclosre\Container;

$container = new Container;
$container->register(function (&$foo, &$bar) {
    $foo = new Foo;
    $bar = new Bar;
});

The container will now assosiate the foo key with an object of instance Foo. The naming of the key is irrelevant; just remember that they must be unique., (*4)

Tell your classes what they should depend on using the inject method supplied by the Injector trait:, (*5)

<?php

use Disclosure\Injector;

class MyClass
{
    use Injector;

    public function __construct()
    {
        $this->inject(function ($foo, $bar) {});
        // Or, alternatively:
        $this->inject('foo', 'bar');
    }
}

class Foo
{
}

$myInstance = new MyClass;
var_dump($myInstance->foo instanceof Foo); // true

inject accepts a random number of arguments, where each argument is their a string with a depedency name, or a callable with dependency names as arguments. Which style you use is up to your own preference., (*6)

Whoah! Why not simply do $this->foo = new MyDependency;?

There's plenty of reasons for using a Container instead of the new keyword all over the place, but the main ones are:, (*7)

  • Hard-coding instances makes it hard to inject mocks during unit tests (you could use class_alias for that, but seriously).
  • It causes tight coupling between classes, which is a Bad Thing(tm).
  • It makes it easier to inject objects as a Service locator (i.e., one instance of an object instead of a new one each time).

No, in the above example it doesn't add much, but see the complete documentation for real-world, practical examples of why dependency injection is generally a good idea., (*8)

The Versions

09/04 2016

dev-develop

dev-develop

PHP5 dependency injection framework

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php dependency injection

30/01 2016

dev-master

9999999-dev

PHP5 dependency injection framework

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php dependency injection

30/01 2016

2.0.2

2.0.2.0

PHP5 dependency injection framework

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php dependency injection

25/01 2016

2.0.1

2.0.1.0

PHP5 dependency injection framework

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php dependency injection

25/01 2016

2.0.0

2.0.0.0

PHP5 dependency injection framework

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php dependency injection

24/01 2016

1.1.1

1.1.1.0

PHP5 dependency injection framework

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php dependency injection

22/01 2016

1.1.0

1.1.0.0

PHP5 dependency injection framework

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php dependency injection

04/06 2015

1.0.0

1.0.0.0

PHP5 dependency injection framework

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php dependency injection

29/03 2015

0.2.2

0.2.2.0

PHP5 dependency injection framework

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Marijn Ophorst

php dependency injection

20/01 2015

0.2.0

0.2.0.0

PHP5 dependency injection framework

  Sources   Download

MIT

The Development Requires

by Marijn Ophorst

php dependency injection

19/01 2015

0.1.0

0.1.0.0

PHP5 dependency injection framework

  Sources   Download

MIT

The Development Requires

by Marijn Ophorst

php dependency injection

16/01 2015

0.0.3

0.0.3.0

PHP5 dependency injection framework

  Sources   Download

MIT

The Development Requires

by Marijn Ophorst

php dependency injection

16/01 2015

0.0.2

0.0.2.0

PHP5 dependency injection framework

  Sources   Download

MIT

The Development Requires

by Marijn Ophorst

php dependency injection

16/01 2015

0.0.1

0.0.1.0

PHP5 dependency injection framework

  Sources   Download

MIT

The Development Requires

by Marijn Ophorst

php dependency injection