yiisoft/factory
Composer 安装命令:
composer require yiisoft/factory
包简介
Yii Factory
关键字:
README 文档
README
Yii Factory
This package provides abstract object factory allowing to create objects by given definition with dependencies resolved by a PSR-11 container.
Requirements
- PHP 8.0 - 8.5.
Installation
The package could be installed with Composer:
composer require yiisoft/factory
General usage
The factory is useful if you need to create objects using definition syntax and/or want to configure defaults for objects created.
$container = new PSR11DependencyInjectionContainer(); $factoryConfig = [ EngineInterface::class => [ 'class' => EngineMarkOne::class, '__construct()' => [ 'power' => 42, ], ] ]; $factory = new Factory($container, $factoryConfig); $one = $factory->create(EngineInterface::class); $two = $factory->create([ 'class' => EngineInterface::class, '__construct()' => [ 'power' => 146, ], ]);
In the code above we define factory config specifying that when we need EngineInterface, an instance of EngineMarkOne
will be created with power constructor argument equals to 42. We also specify that all the dependencies requested by
the object created should be resolved by PSR11DependencyInjectionContainer.
First call to create() uses default configuration of EngineInterface as is. Second call specifies custom
configuration for power constructor argument. In this case, configuration specified is merged with default
configuration overriding its keys when the key name is the same.
Tuning for production
By default, the factory validates definitions right when they are set. In production environment, it makes sense to
turn it off by passing false as a third constructor argument:
$factory = new Factory($container, $factoryConfig, false);
Strict factory
StrictFactory differs in that it processes only configured definitions.
When attempting to request an existing class that is not defined in the factory config,
a NotFoundException will be thrown.
$container = new PSR11DependencyInjectionContainer(); $factoryConfig = [ EngineInterface::class => [ 'class' => EngineMarkOne::class, '__construct()' => [ 'power' => 42, ], ] ]; $factory = new Factory($factoryConfig, $container); $engine = $factory->create(EngineInterface::class); // Throws `NotFoundException` $factory->create(EngineMarkOne::class);
Documentation
If you need help or have a question, the Yii Forum is a good place for that. You may also check out other Yii Community Resources.
License
The Yii Factory is free software. It is released under the terms of the BSD License.
Please see LICENSE for more information.
Maintained by Yii Software.
Support the project
Follow updates
yiisoft/factory 适用场景与选型建议
yiisoft/factory 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 532.17k 次下载、GitHub Stars 达 36, 最近一次更新时间为 2019 年 06 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「factory」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 yiisoft/factory 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yiisoft/factory 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 yiisoft/factory 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Testing object factory for PHP
Testing object factory for PHP
Laravel 5 Model Factory Generator.
A model factory package for Laravel 4 with expressive API for creating custom tailored dummy objects
Extensions for Laravel model factories
This library tries to create a simple promise factory standard while waiting for a psr.
统计信息
- 总下载量: 532.17k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 37
- 点击次数: 14
- 依赖项目数: 42
- 推荐数: 1
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2019-06-22