2017 © Pedro Peláez
 

library dconstructor

dependency injection for lazy p

image

dconstructor/dconstructor

dependency injection for lazy p

  • Tuesday, May 8, 2018
  • by jonathankowalski
  • Repository
  • 1 Watchers
  • 0 Stars
  • 10 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Dconstructor Build Status Coverage Status

Dependency Injection for Lazy People

(all of us btw)

Everybody likes dependency injection (and if it is not your case, you should). However dependency injection sometimes leads us to write of useless code and that, everybody hates., (*1)

The purpose of Dconstructor is to free you from certain portions of code which do not serve in much, the happiness of developers as a matter of fact., (*2)

Indeed nowadays, we repeat many things, in the properties, in the signature of the constructor, in the body of the constructor. Repeat is null, time to dconstructor KISS, (*3)

Dconstructor

class UserManager
{
    /**
     * @var Mailer
     */
    private $mailer;

    public function register($email){
        //some code
        $this->mailer->send($email, "Hello !");
    }
}

class Mailer
{
    public function send($recipient, $message)
    {
        //some code
    }
}

Without DI

just take a simple example, (*4)

class UserManager
{
    public function register($email){
        //some code
        $mailer = new Mailer();
        $mailer->send($email, "Hello !");
    }
}

class Mailer
{
    public function send($recipient, $message)
    {
        //some code
    }
}

Classic DI

class UserManager
{
    /**
     * @var Mailer
     */
    private $mailer;

    public function __construct(Mailer $mailer) {
        $this->mailer = $mailer;
    }

    public function register($email){
        //some code
        $this->mailer->send($email, "Hello !");
    }
}

class Mailer
{
    public function send($recipient, $message)
    {
        //some code
    }
}

Installation

$ composer require dconstructor/dconstructor

Usage

Dconstructor is a simple DI container use it as usual, (*5)

$container = new Container();

$container->set('foo','bar');

Dconstructor will make injection for you, in our example above for instantiate a new UserManager just call it, (*6)

$container = new Container();

$userManager = $container->get('UserManager');

Singleton

U can choose to make a class a singleton use annotation for that., (*7)


/** * @Singleton */ class Mailer { //some code } $container = new Container(); $mailer = $container->get('Mailer'); $sameMailer = $container->get('Mailer');

The Versions

08/05 2018

dev-master

9999999-dev

dependency injection for lazy p

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jonathan Kowalski

dependency injection lazy

17/07 2017

1.0.0

1.0.0.0

dependency injection for lazy p

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jonathan Kowalski

dependency injection lazy

14/12 2016

1.0.x-dev

1.0.9999999.9999999-dev

dependency injection for lazy p

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jonathan Kowalski

dependency injection lazy