A very simple DIC, supporting autowiring (NOT production ready)

v0.0.1 2019-12-16 12:07 UTC

This package is auto-updated.

Last update: 2024-04-16 22:06:38 UTC


README

A very simple DIC, supporting autowiring. For fun only.

Use

<?php

require_once(__DIR__.'/vendor/autoload.php');

$dic = new \Dyc\Dic();
$dic->set(\Foo\Bar::class, function(\Dyc\Dic $dic) {
    return new \Foo\Bar($dic->get(\Bar\Baz::class));
});

$bar = $dic->get(\Foo\Bar::class);

Autowiring

We recommend using haydenpierce/class-finder, to get a list of all FQCN in your project.

<?php

require_once(__DIR__.'/vendor/autoload.php');

$dic = new \Dyc\Dic();
$classes = \HaydenPierce\ClassFinder\ClassFinder::getClassesInNamespace('Foo');
$dic->autowire($classes);

$bar = $dic->get(\Foo\Bar::class);

If one service requires an interface or a scalar, you will need to rewrite the whole definition for this one:

<?php

require_once(__DIR__.'/vendor/autoload.php');

$dic = new \Dyc\Dic();
$classes = \HaydenPierce\ClassFinder\ClassFinder::getClassesInNamespace('Foo');
$dic->autowire($classes);
$dic->set(\Some\Scalar\Dependent\Service::class, function(\Dyc\Dic $dic) {
    return new \Some\Scalar\Dependent\Service($dic->get(\Foo\Bar::class), 'some api key');
});

$service = $dic->get(\Some\Scalar\Dependent\Service::class);