gzhegow / di
Dependency-Injector
1.6.4
2021-11-01 19:41 UTC
Requires
- php: >=7.3
- psr/container: ~1.0
Requires (Dev)
- phpunit/phpunit: ~9.0
- symfony/var-dumper: ^5.3
- dev-master
- 1.6.4
- 1.6.3
- 1.6.2
- 1.6.1
- 1.6.0
- 1.5.8
- 1.5.7
- 1.5.6
- 1.5.5
- 1.5.4
- 1.5.3
- 1.5.2
- 1.5.1
- 1.5.1-beta4
- 1.5.1-beta3
- 1.5.1-beta2
- 1.5.1-beta1
- 1.5.0.2
- 1.5.0.1
- 1.5.0-beta4
- 1.5.0-beta3
- 1.5.0b
- 1.4.11
- 1.4.10
- 1.4.9
- 1.4.8
- 1.4.7
- 1.4.6
- 1.4.5
- 1.4.4
- 1.4.3
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.17
- 1.2.16
- 1.2.15
- 1.2.14
- 1.2.13
- 1.2.12
- 1.2.11
- 1.2.10
- 1.2.9
- 1.2.8
- 1.2.7
- 1.2.6
- 1.2.5
- 1.2.4
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.0
- 1.0.1
- 1.0.0
This package is auto-updated.
Last update: 2024-04-29 04:34:02 UTC
README
Микро-контейнер с инжектором зависимостей
<?php namespace Gzhegow\Di; use Gzhegow\Di\Tests\Classes\B; use Gzhegow\Di\Tests\Classes\BB; use Gzhegow\Di\Tests\Classes\BInterface; use Gzhegow\Di\Tests\Classes\BBInterface; use Gzhegow\Di\Tests\Classes\BDeferableProvider; use Gzhegow\Di\Tests\Classes\BBBootableProvider; // create instance $di = new Di(); // static singleton // $di = Di::getInstance(); // get // Di::setInstance($di); // replace // run register() for provider $di->register(BDeferableProvider::class); $di->register(new BBBootableProvider($di)); // copy `sources` (vendor) files/directories of to `targets` (application) $di->discover(); // update discovered config like in production $provider = $di->getProvider(BBBootableProvider::class); file_put_contents($provider->path('config'), '<?php return [ 3, 2, 1 ];'); // run boot() for all registered bootable providers $di->boot(); // get instances (this also run boot() for deferable providers) $b1 = $di->get(BInterface::class); $b2 = $di->get(BInterface::class); $bb = $di->get(BBInterface::class); // check instances $this->assertInstanceOf(B::class, $b1); $this->assertInstanceOf(B::class, $b2); $this->assertInstanceOf(BB::class, $bb); // check discovered config (we updated it above) $this->assertEquals([ 3, 2, 1 ], $bb->getConfig()); // check singleton $this->assertEquals($b1, $b2); // call method or function $fn = function (B $b, $hello, BBInterface $bb, $world) { return func_get_args(); }; $params = [ '$hello' => 1, '$world' => 2, // > keys could be `binds` // BInterface::class => $b1, // BBInterface::class => $bb, // > keys could be argument names prefixed with $ // '$b' => $b1, // '$bb' => $bb, // > keys could be argument positions (integers) // 0 => $b1, // 2 => $bb ]; $result = $di->call($fn, $params); // check arguments $this->assertEquals([ $b1, 1, $bb, 2 ], $result);