moderntribe/square1-container
Composer 安装命令:
composer require moderntribe/square1-container
包简介
A dependency injection container wrapper for square one
README 文档
README
The Tribe Libs Container uses PHP-DI to help you build object graphs, allowing for autowiring dependency injection.
Mutable Container
In general, you should design your services to be stateless.
However, for long-running processes (like queues) or in situations where you absolutely must create a fresh object, including all of its dependencies, you can use the Mutable Container.
This opens up the makeFresh() method, which works exactly like PHP-DI's make() method, but flushes the container's resolved entries, meaning the object's dependencies will also be recreated from scratch.
NOTE: Only use this when necessary, this is not as performant as the default container.
In a definer:
<?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 ), ]; } }
Then you can utilize the makeFresh() method to create purely fresh objects
each time it's called:
<?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 ); } }
Then call your factory:
<?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 ); } }
moderntribe/square1-container 适用场景与选型建议
moderntribe/square1-container 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 30.2k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 08 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 moderntribe/square1-container 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 moderntribe/square1-container 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 30.2k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 10
- 依赖项目数: 14
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0-only
- 更新时间: 2019-08-02