wikimedia/object-factory
Composer 安装命令:
composer require wikimedia/object-factory
包简介
Construct objects from configuration instructions
README 文档
README
Construct objects from configuration instructions.
It can be used statically, or as a service wrapping a PSR-11 service container for lazy instantiation of objects with dependency injection.
Specification array
Contents of the specification array are as follows:
'factory' => callable,
'class' => string,
The specification array must contain either a 'class' key with string value that specifies the class name to instantiate or a 'factory' key with a callable (is_callable() === true). If both are passed, 'factory' takes precedence but an InvalidArgumentException will be thrown if the resulting object is not an instance of the named class.
'args' => array,
'closure_expansion' => bool, // default true
'spec_is_arg' => bool, // default false
'services' => (string|null)[], // default empty
'optional_services' => (string|null)[], // default empty
The 'args' key, if provided, specifies arguments to pass to the constructor/callable. Values in 'args' which are Closure instances will be expanded by invoking them with no arguments before passing the resulting value on to the constructor/callable. This can be used to pass live objects to the constructor/callable. This behavior can be suppressed by adding closure_expansion => false to the specification.
If 'spec_is_arg' => true is in the specification, 'args' is ignored. The entire spec array is passed to the constructor/callable instead.
If 'services' is supplied and non-empty (and a service container is available),
the named services are requested from the PSR-11 service container and
prepended before 'args'. null values in 'services' are passed to the constructor
unchanged.
Optional services declared via 'optional_services' are handled the same,
except that if the service is not available from the service container
null is passed as a parameter instead. Optional services are appended
directly after the normal required services
If any extra arguments are passed in the options to getObjectFromSpec() or
createObject(), these are prepended before the 'services' and 'args'.
These are passed using the extraArgs key, and any Closure instances that
are included this way are never expanded, regardless of whether the main
argument list is expanded.
'calls' => array
The specification may also contain a 'calls' key that describes method calls to make on the newly created object before returning it. This pattern is often known as "setter injection". The value of this key is expected to be an associative array with method names as keys and argument lists as values. The argument list will be expanded (or not) in the same way as the 'args' key for the main object.
Note these calls are not passed the extra arguments.
Installation
$ composer require wikimedia/object-factory
Usage
<?php $specs = [ // Simple constructor based injection 'testDB' => [ 'class' => PDO::class, 'args' => [ 'mysql:dbname=testdb;host=127.0.0.1', 'dbuser', 'dbpass', ], ], ]; $db = ObjectFactory::getObjectFromSpec( $specs['testDB'] ):
License
Wikimedia ObjectFactory is licensed under the GNU General Public License,
version 2 and any later version (GPL-2.0-or-later). See the
COPYING file for more details.
wikimedia/object-factory 适用场景与选型建议
wikimedia/object-factory 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 658.5k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2018 年 01 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 wikimedia/object-factory 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 wikimedia/object-factory 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 658.5k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 21
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0-or-later
- 更新时间: 2018-01-26