2017 © Pedro Peláez
 

library pimple-di

Integrates DI functionality with Pimple.

image

okeyaki/pimple-di

Integrates DI functionality with Pimple.

  • Wednesday, December 20, 2017
  • by okeyaki
  • Repository
  • 0 Watchers
  • 0 Stars
  • 22 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Pimple DI

Pimple DI integrates DI functionality with Pimple., (*1)

Features

  • Integrating DI (constructor Injection) functionality with Pimple.
  • Easy to use.

Installation

Add okeyaki/pimple to your composer.json:, (*2)

$ composer require okeyaki/pimple-di

Usage

At first, create a sub-class of Pimple\Container and mixin Okeyaki\Pimple\DiTrait:, (*3)

class Container extends \Pimple\Container
{
    use \Okeyaki\Pimple\DiTrait;
}

Then, create an instance of the class:, (*4)

$container = new Container();

Constructor Injection

Pimple DI resolves dependencies automatically by the classes of constructor parameters:, (*5)

class Foo
{
}

class Bar
{
    private $foo;

    public function __construct(Foo $foo)
    {
        $this->foo = $foo;
    }

    public function foo()
    {
        return $this->foo;
    }
}

$foo = $container[Foo::class];

$bar = $contaienr[Bar::class];
$bar->foo();

Class Binding

You can bind classes to an existing ID:, (*6)

class Foo
{
}

$container['foo'] = function () {
    return new Foo();
};

$container->bind(Foo::class, 'foo');

$foo = $container[Foo::class];

This is useful on using Silex proprietary or third-party providers., (*7)

Instanciation

You can instanciate a class and resolve its dependencies automatically:, (*8)

class Foo
{
}

class Bar
{
    private $foo;

    public function __construct(Foo $foo)
    {
        $this->foo = $foo;

        $this->name = $name;
    }

    public function foo()
    {
        return $this->foo;
    }
}

$bar = $container->make(Bar::class);
$bar->foo();

If a constructor has unresolvable parameters:, (*9)

class Bar
{
    private $foo;

    private $baz;

    public function __construct(Foo $foo, $baz)
    {
        $this->foo = $foo;

        $this->baz = $baz;
    }

    public function foo()
    {
        return $this->foo;
    }

    public function baz()
    {
        return $this->baz;
    }
}

$bar = $container->make(Bar::class, [
    'baz' => 'baz',
]);

$bar->foo();
$bar->baz();

Examples

Integrating with Silex

class App extends \Silex\Application
{
    use \Okeyaki\Pimple\DiTrait;
}

The Versions

20/12 2017

dev-master

9999999-dev

Integrates DI functionality with Pimple.

  Sources   Download

MIT

The Requires

 

The Development Requires

20/12 2017

1.0.1

1.0.1.0

Integrates DI functionality with Pimple.

  Sources   Download

MIT

The Requires

 

The Development Requires

29/06 2017

1.0.0

1.0.0.0

Integrates DI functionality with Pimple.

  Sources   Download

MIT

The Requires

 

The Development Requires