2017 © Pedro Peláez
 

library di

Annotation based simple DI & AOP at PHP.

image

ranyuen/di

Annotation based simple DI & AOP at PHP.

  • Sunday, July 5, 2015
  • by ne_sachirou
  • Repository
  • 5 Watchers
  • 4 Stars
  • 187 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 2 Open issues
  • 8 Versions
  • 1 % Grown

The README.md

test, (*1)

Ranyuen/Di

Annotation based simple DI (Dependency Injection) & AOP (Aspect Oriented Programming) at PHP., (*2)

Features

  1. Compatible with Pimple 3.
  2. Zero configuration. Injection through reflection and annotations. It's easy!
  3. AOP support.

Install

composer require ranyuen/di

Support PHP >=7.4., (*3)

DI Example

Ranyuen/Di just extends Pimple. So we can use this same as Pimple 3., (*4)

id = $id;
    }
}

$c = new Container();
$c['id'] = 'Sample ID.';
$c['momonga'] = function ($c) { return new Momonga($c['id']); };
$c['factory'] = $c->factory(function ($c) { return new Momonga(); });

var_dump('Sample ID.' === $c['id']);
var_dump($c['momonga'] instanceof Momonga);
var_dump($c['momonga'] === $c['momonga']);
var_dump('Sample ID.' === $c['momonga']->id);
var_dump($c['factory'] instanceof Momonga);
var_dump($c['factory'] !== $c['factory']);
?>

Basic Ray.Di and PHP-DI style @Inject annotations example. Inject to constructor & properties., (*5)

benri = $momonga;
        $this->id = $id;
    }
}

// We can pass additional args.
$yuraru = $c->newInstance('Yuraru', ['Sample ID.']);

var_dump($yuraru->benri instanceof Momonga);
var_dump('Sample ID.' === $yuraru->id);
var_dump($yuraru->momonga instanceof Momonga);
?>

Inject to properties., (*6)

inject($gardea);

var_dump($gardea->momonga instanceof Momonga);
?>

Detect with type hinting through bind method., (*7)

bind('Momonga', 'momonga', function ($c) { return new Momonga(); });

class Benri
{
    /**
     * @Inject
     * @var Momonga
     */
    public $benri;

    public $momonga;

    public function __construct(Momonga $benri)
    {
        $this->momonga = $benri;
    }
}

$benri = $c->newInstance('Benri');

var_dump($benri->benri instanceof Momonga);
var_dump($benri->momonga instanceof Momonga);
?>

Assign services with another names by @Named annotation., (*8)

benri = $benri;
    }
}

$musasabi = $c->newInstance('Musasabi');

var_dump($musasabi->benri instanceof Momonga);
var_dump($musasabi->musasabi instanceof Momonga);
?>

We can use every methods that are defined at Pimple: factory, protect, extend and raw. Below is binding example with factory., (*9)

bind('Momonga', 'momonga', function ($c) { return new Momonga(); });
$c->bind('Momonga', 'factory', $c->factory(function ($c) { return new Momonga(); }));

class MomongaFactory
{
    /** @Inject */
    public $momonga;

    /** @Inject */
    public $factory;
}

$momonga = $c->newInstance('MomongaFactory');

var_dump($momonga->momonga instanceof Momonga);
var_dump($momonga->momonga === $c['momonga']);
var_dump($momonga->factory instanceof Momonga);
var_dump($momonga->factory !== $c['factory']);
?>

Take out DI instance by static access. We call this "Facade", like Laravel framwork., (*10)

facade('Station', 'building');
var_dump('rocket' === Station::launch());
?>

AOP Example

Basic AOP example., (*11)

wrap('Monday', ['tuesday'], function ($invocation, $args) {
    $day = $args[0];

    return $invocation($day + 1);
});
$c->wrap('Monday', ['/day$/'], function ($invocation, $args) {
    $day = $args[0];

    return $invocation($day * 7);
});
$monday = $c->newInstance('Monday');
var_dump(1 * 7     === $monday->sunday());
var_dump(2 * 7 + 1 === $monday->tuesday());
?>

Is there no annotation for AOP? Yes we can!, (*12)

protect(function ($invocation, $args) {
    $day = $args[0];

    return $invocation($day + 4);
});
$c['advice.monday'] = $c->protect(function ($invocation, $args) {
    $day = $args[0];

    return $invocation($day * 7);
});
$tuesday = $c->newInstance('Tuesday');
var_dump(5 * 7 + 4 + 3 === $tuesday->wednesday(5));
?>

The Versions

05/07 2015

0.2.3.x-dev

0.2.3.9999999-dev

Annotation based simple DI & AOP at PHP.

  Sources   Download

GPL-3.0+

The Requires

 

The Development Requires

by Sachirou Inoue

dependency injection container annotations di aop aspect method invocation code gen

18/04 2015

dev-master

9999999-dev

Annotation based simple DI & AOP at PHP.

  Sources   Download

GPL-3.0+

The Requires

 

The Development Requires

by Sachirou Inoue

dependency injection container annotations di aop aspect method invocation code gen

18/04 2015

0.2.2

0.2.2.0

Annotation based simple DI & AOP at PHP.

  Sources   Download

GPL-3.0+

The Requires

 

The Development Requires

by Sachirou Inoue

dependency injection container annotations di aop aspect method invocation code gen

30/03 2015

0.2.1

0.2.1.0

Annotation based simple DI & AOP at PHP.

  Sources   Download

GPL-3.0+

The Requires

 

The Development Requires

by Sachirou Inoue

dependency injection container annotations di aop aspect method invocation code gen

10/12 2014

0.2.0

0.2.0.0

Annotation based simple DI & AOP at PHP.

  Sources   Download

GPLv3

The Requires

 

The Development Requires

by Sachirou Inoue

19/11 2014

0.1.0

0.1.0.0

Annotation based simple DI (Dependency Injection) & AOP (Aspect Oriented Programming).

  Sources   Download

GPLv3

The Requires

 

The Development Requires

by Sachirou Inoue

di

02/11 2014

0.0.2

0.0.2.0

Simple Ray.Di style DI (Dependency Injector) extending Pimple.

  Sources   Download

GPLv3

The Requires

 

The Development Requires

by Sachirou Inoue

di

29/10 2014

0.0.1

0.0.1.0

Simple DI (Dependency Injector) extending Pimple.

  Sources   Download

GPLv3

The Requires

 

The Development Requires

by Sachirou Inoue