moderntribe / square1-container
square one 的依赖注入容器包装器
4.2.0
2022-08-17 21:54 UTC
Requires
- php: >=7.4
- php-di/php-di: ^6.0
- 4.x-dev
- 4.2.0
- dev-master / 4.1.x-dev
- 4.1.0
- 4.0.16
- 4.0.15
- 4.0.13
- 4.0.12
- 4.0.8
- 4.0.7
- 4.0.5
- 4.0.4
- 4.0.3
- 3.6.0
- 3.5.1
- 3.5.0
- 3.4.18
- 3.4.17
- 3.4.16
- 3.4.15
- 3.4.14
- 3.4.13
- 3.4.12
- 3.4.11
- 3.4.10
- 3.4.9
- 3.4.8
- 3.4.7
- 3.4.6
- v3.4.4
- v3.4.2
- v3.4.1
- v3.4.0
- v3.3.0
- v3.2.0
- v3.1.2
- v3.1.1
- v3.1.0
- v3.0.0
- 2.1.2
- v2.1.1
- v2.0.0
- 0.0.2
- 0.0.1
- dev-feature/monorepo
This package is auto-updated.
Last update: 2024-08-25 20:35:54 UTC
README
Tribe Libs 容器使用 PHP-DI 来帮助你构建 对象图,允许自动装配依赖注入。
可变容器
通常,你应该设计你的服务为 无状态。
然而,对于长时间运行的过程(如队列)或在必须从头创建一个全新对象及其所有依赖项的情况下,可以使用可变容器。
这开启了 makeFresh()
方法,它的工作方式与 PHP-DI 的 make() 方法 完全相同,但会清除容器中解析的条目,这意味着对象的所有依赖项也将从头开始重新创建。
注意:仅在必要时使用此功能,这不如默认容器性能好。
在定义器中
<?php declare(strict_types=1); namespace Tribe\Project\Feature; use Psr\Container\ContainerInterface; use Symfony\Contracts\HttpClient\HttpClientInterface; use Tribe\Explore_Navigation\Azure\Factories\Client_Factory; use Tribe\Libs\Container\Definer_Interface; use Tribe\Libs\Container\MutableContainer; use Tribe\Libs\Container\Container; class My_Feature_Definer implements Definer_Interface { public function define(): array { return [ // Define our abstract to the interface, placing our existing container inside the mutable container. MutableContainer::class => static fn ( ContainerInterface $c ) => ( new Container() )->wrap( $c ), ]; } }
然后你可以使用 makeFresh()
方法在每次调用时创建全新的对象
<?php declare(strict_types=1); namespace Tribe\Project\Feature; use Tribe\Libs\Container\Abstract_Subscriber; use Tribe\Libs\Container\MutableContainer; class My_Queue_Factory { protected MutableContainer $container; public function __construct( MutableContainer $container ) { $this->container = $container; } /** * Make a fresh object. * * @param string $class_name e.g. \Tribe\Project\Feature\SomeFeature::class * * @return mixed|string */ public function make( string $class_name ) { return $this->container->makeFresh( $class_name ); } }
然后调用你的工厂
<?php declare(strict_types=1); namespace Tribe\Project\Feature; use Tribe\Libs\Container\Abstract_Subscriber; use Tribe\Libs\Container\MutableContainer; class My_Feature_Subscriber extends Abstract_Subscriber { public function register(): void { add_action( 'init', function(): void { // Both of these will be completely separate instances, // including any dependencies SomeFeature::class has. var_dump( $this->container->get( MutableContainer::class )->make( '\Tribe\Project\Feature\SomeFeature::class' ) ); var_dump( $this->container->get( MutableContainer::class )->make( '\Tribe\Project\Feature\SomeFeature::class' ) ); // This will be the same instance as the last freshly made object var_dump( $this->container->get( SomeFeature::class ) ); }, 10, 0 ); } }