服务提供者

Testing Is Documentation

tests/Di/ProviderTest.php服务提供者 - 图1

IOC 容器是整个框架最核心的部分,负责服务的管理和解耦。

服务提供者将服务注入到 IOC 容器中,通常来说服务会依赖一些配置和调用其它服务等完成组装,还有有一定复杂度。

我们可以为服务定义一组配套的服务提供者,可以免去配置服务的成本,开发起来很愉悦。

Uses

  1. <?php
  2. use Leevel\Di\Container;
  3. use Leevel\Di\IContainer;
  4. use Leevel\Di\Provider;

基本使用方法

服务提供者通过 register 完成服务注册。

fixture 定义

Tests\Di\PrividerTest

  1. namespace Tests\Di;
  2. class PrividerTest extends Provider
  3. {
  4. public function register(): void
  5. {
  6. $this->container->singleton('foo', function ($container) {
  7. return new PrividerService1($container);
  8. });
  9. }
  10. public function bootstrap(): void
  11. {
  12. $_SERVER['test.privider'] = 'bootstrap';
  13. }
  14. public static function providers(): array
  15. {
  16. return [
  17. 'foo' => [
  18. 'bar',
  19. 'hello',
  20. ],
  21. ];
  22. }
  23. }

Tests\Di\PrividerService1

  1. namespace Tests\Di;
  2. class PrividerService1
  3. {
  4. public function __construct(IContainer $container)
  5. {
  6. }
  7. public function hello()
  8. {
  9. return 'world';
  10. }
  11. }
  1. public function testBaseUse(): void
  2. {
  3. $test = new PrividerTest($container = new Container());
  4. $this->assertInstanceof(IContainer::class, $test->container());
  5. $this->assertInstanceof(Container::class, $test->container());
  6. $test->register();
  7. $this->assertSame('world', $container->make('foo')->hello());
  8. $this->assertSame('world', $container->make('bar')->hello());
  9. $this->assertSame('world', $container->make('hello')->hello());
  10. $this->assertFalse($test->isDeferred());
  11. }

延迟服务提供者

fixture 定义

Tests\Di\PrividerTest2

  1. namespace Tests\Di;
  2. class PrividerTest2 extends Provider
  3. {
  4. public function register(): void
  5. {
  6. $this->container->singleton('world', function ($container) {
  7. return new PrividerService2();
  8. });
  9. }
  10. public static function providers(): array
  11. {
  12. return [
  13. 'world' => 'hello',
  14. ];
  15. }
  16. public static function isDeferred(): bool
  17. {
  18. return true;
  19. }
  20. }

Tests\Di\PrividerService2

  1. namespace Tests\Di;
  2. class PrividerService2
  3. {
  4. public function foo()
  5. {
  6. return 'bar';
  7. }
  8. }
  1. public function testDeferred(): void
  2. {
  3. $test = new PrividerTest2($container = new Container());
  4. $this->assertInstanceof(IContainer::class, $test->container());
  5. $this->assertInstanceof(Container::class, $test->container());
  6. $test->register();
  7. $this->assertSame('bar', $container->make('world')->foo());
  8. $this->assertSame('hello', $container->make('hello'));
  9. $container->alias($test->providers());
  10. $this->assertSame('bar', $container->make('hello')->foo());
  11. $this->assertTrue($test->isDeferred());
  12. }

bootstrap 服务注册后的引导程序

  1. public function testBootstrap(): void
  2. {
  3. $test = new PrividerTest(new Container());
  4. $this->assertInstanceof(IContainer::class, $test->container());
  5. $this->assertInstanceof(Container::class, $test->container());
  6. if (isset($_SERVER['test.privider'])) {
  7. unset($_SERVER['test.privider']);
  8. }
  9. $test->bootstrap();
  10. $this->assertSame('bootstrap', $_SERVER['test.privider']);
  11. if (isset($_SERVER['test.privider'])) {
  12. unset($_SERVER['test.privider']);
  13. }
  14. }