2017 © Pedro Peláez
 

library di

The gears dependency injection container.

image

gears/di

The gears dependency injection container.

  • Tuesday, January 27, 2015
  • by brad-jones
  • Repository
  • 1 Watchers
  • 1 Stars
  • 19,571 Installations
  • PHP
  • 9 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 17 % Grown

The README.md

The Di Gear

Build Status Latest Stable Version Total Downloads License, (*1)

So we have all heard of this thing called Dependency Injection. Its been around for ages in other enterprise enviroments like Java, .Net, etc. But it's sort of new to the PHP world., (*2)

Most will understand the general concept but if not check out:, (*3)

  • http://www.phptherightway.com/#dependency_injection

How to Install

Installation via composer is easy:, (*4)

composer require gears/di:*

How to Use

Firstly this container is like Pimple but it is not Pimple. There are some unique differences so pay attention., (*5)

Basic example:, (*6)

// lets import the class
use Gears\Di\Container;

// create a new container object
$container = new Container();

// define some services
$container['session_storage'] = function ()
{
    return new SessionStorage('SESSION_ID');
};

$container['session'] = function ()
{
    return new Session($this['session_storage']);
};

// get the session object
$session = $container['session'];

// define factory service
$container['session_factory'] = $container->factory(function()
{
    return new Session($this['session_storage']);
});

// define container parameters / attributes
$container['cookie_name'] = 'SESSION_ID';

// protecting parameters
$container['random_func'] = $container->protect(function()
{
    return rand();
});

For someone that has previously used the Pimple container you should note that instead of the container being passed in via a function parameter. It is available via the $this variable. This is because we bind the closure to the container., (*7)

Using a Service Provider:, (*8)

use Gears\Di\Container;
use Gears\Di\ServiceProviderInterface;

class FooProvider implements ServiceProviderInterface
{
    public function register(Container $c)
    {
        $c['FooService'] = function(){ return new Foo(); };
    }
}

$container = new Container();
$container->register(new FooProvider());
$container['FooService']->bar();

Object Syntax:, (*9)

// you can also use the container like this.
$container = new Container();

$container->session_storage = function ()
{
    return new SessionStorage('SESSION_ID');
};

$container->session = function ()
{
    return new Session($this->session_storage);
};

$session = $container->session;

Extending the Container:, (*10)

class Mail extends Container
{
    // note how we prefix the word inject.
    // this tells us that the property is injectable
    protected $injectTo;

    // private properties however will always be
    // private and can not be injected directly.
    private $sendMailPath;

    // so if you tried to inject fooBar it will fail
    private $injectFooBar;

    // from a naming standpoint I think it is best if you name the injectable
    // properties such that it tells you the type that should be injected.
    // however this isn't enforced.
    protected $injectMessage;

    protected $injectTransportService;

    // this is where we can define default services for our container.
    protected function setDefaults()
    {
        // notice how we set them without the word inject
        $this->to = 'brad@bjc.id.au';

        // I could have defined this above directly on the property
        // but I would rather keep everything consistent.
        $this->sendMailPath = '/bin/sendmail';

        $this->message = function()
        {
            return new Message('Hello World');
        };

        // take note of the camel case property name vs the definition above.
        $this->transportService = function()
        {
            return new SendMailTransport($this->sendMailPath);
        };

        // you can use factory and protect too
        // note you don't have to explicitly define a class property.
        // but just note that both abc and xyz are public properties.
        $this->abc = $this->factory(function(){ return new Abc(); });
        $this->xyz = $this->protect(function($a,$b){ return $a+$b; });
    }

    public function send()
    {
        $this->message->setTo($this->to);
        return $this->transportService->send($this->message);
    }
}

$mail = new Mail();
$mail->send(); // sends an email to me saying Hello World

$mail = new Mail();
$mail->to = 'foo@example.com';
$mail->ip = '127.0.0.1';
$mail->message = function(){ return new Message('bar'); };
$mail->transportService = function(){ return new SmtpTransport($this->ip); };
$mail->send(); // sends an email to foo@example.com via 127.0.0.1 saying bar

// the above could be re written as
$mail = new Mail
([
    'to' => 'foo@example.com',
    'ip' => '127.0.0.1',
    'message' => function(){ return new Message('bar'); },
    'transportService' => function(){ return new SmtpTransport($this->ip); },
]);
$mail->send();

Credits

This is definitely inspired by Fabien's Pimple Di Container. http://pimple.sensiolabs.org/, (*11)


Developed by Brad Jones - brad@bjc.id.au, (*12)

The Versions

27/01 2015

dev-master

9999999-dev https://github.com/phpgearbox/di

The gears dependency injection container.

  Sources   Download

MIT

The Development Requires

container ioc dependency di injection

21/11 2014

v0.2.1

0.2.1.0 https://github.com/phpgearbox/di

The gears dependency injection container.

  Sources   Download

MIT

The Development Requires

container ioc dependency di injection

03/10 2014

v0.2.0

0.2.0.0 https://github.com/phpgearbox/di

The gears dependency injection container.

  Sources   Download

MIT

The Development Requires

container ioc dependency di injection

02/10 2014

v0.1.0

0.1.0.0 https://github.com/phpgearbox/di

The gears dependency injection container.

  Sources   Download

MIT

The Development Requires

container ioc dependency di injection