xynha / dicontainer
遵守PSR-11规范的PHP依赖注入容器
v0.4
2020-09-10 02:16 UTC
Requires
- php: ^7.1 || ^8.0
- psr/container: ^1.0
Requires (Dev)
- pattisahusiwa/coding-standard: ^0.3.0
- phpunit/phpunit: ^7.0
README
遵守PSR-11规范的PHP依赖注入容器。
目录
安装
使用 composer 安装库。
composer require xynha/dicontainer
使用
DiContainer
可以使用或不需要配置。所有不需要构造函数参数的类都可以直接创建。
// Without configuration $dic = new DiContainer(new DiRuleList); $obj = $this->dic->get('classname');
但是,对于复杂的初始化,您必须定义 DiContainer
规则并将它们传递给 DiRuleList
。
// Construct rule list $rlist = new DiRuleList(); // Add configuration from file $json = json_decode(file_get_contents('filename'), true); $rlist = $rlist->addRules($json); // Add configuration manually $rule['shared'] = true; $rlist = $rlist->addRule('classname', $rule); // Construct the container $dic = new DiContainer($rlist); $obj = $this->dic->get('classname');
DiContainer规则
JSON格式的 DiContainer
规则的一般格式
{ "rule_key" : { "config_keys": "config_value" }, "another_rule_key" : { "config_keys": "config_value" } }
其中
rule_key
,another_rule_key
是类或接口命名空间,而config_keys
是支持的规则键shared
instanceOf
constructParams
substitutions
getFrom
# shared
: boolean
(默认 false
)
- 可能的值
true
,如果构造的实例可以在执行期间在类之间共享,false
,否则。
- 示例
"classA" : { "shared" : true }, "classB" : { "shared" : false }
# instanceOf
: string|object
(默认 null
)
- 要覆盖在
rule_key
中定义的类或接口。 - 可能的值
string
:类命名空间object
:已构造的实例。
- 示例
"interfaceA" : { "instanceOf" : "classA" }
传递已构造实例的示例,$rlist = new DiRuleList(); $rule['interfaceA'] = ['instanceOf' => $object]; $rlist = $rlist->addRules($rule);
# constructParams
: array
(默认 null
)
constructParams
是构造函数参数值列表。- 示例
"classA" : { "constructParams" : [ "arg1", "arg2" ] }
- 构造参数也可以通过使用
CALL::OBJECT
和CALL::SCALAR
标识符从另一个类中检索。 CALL::SCALAR
用于检索array
和scalar
值CALL::OBJECT
用于检索object
值。CALL::OBJECT
和CALL::SCALAR
格式
"constructParams": [ ["CALL::SCALAR", ["callback"], ["array_of_callback_arguments"]], ["CALL::OBJECT", ["callback"], ["array_of_callback_arguments"]] ]
- 示例
class Config{ public function getConfig(string $key){ if ($key === 'db'){ return 'dbconfig'; } } } class DatabaseDriver{ public function __construct(string $config){} }
# 如果手动构造,
$config = new Config(); $driver = new DatabaseDriver($config->getConfig('db'));
# 使用 DiContainer
{ "DatabaseDriver" : { "constructParams" : [ ["CALL::SCALAR", ["Config","getConfig"], ["db"]] ] } }
$driver = $dic->get(DatabaseDriver::class);
- 可以通过使用
CALL::CONSTANT
将常量传递给构造函数参数,例如
{ "$pdo": { "instanceOf": "DatabaseDriver", "constructParams": [ [ ["CALL::CONSTANT", "PDO::ATTR_ERRMODE"], ["CALL::CONSTANT", "PDO::ERRMODE_EXCEPTION"] ] ] } }
# substitutions
: 关联 array
(默认 null
)
- 将构造函数参数中的接口替换为替换的类。
- 示例
"classA" : { "substitutions" : { "interfaceB" : "classB", "interfaceC" : "classC", } }
# getFrom
: array
(默认 null
)
- 从工厂或类中获取
rule_key
实例。 getFrom
格式"getFrom" : [ ["callback"], [ "array_of_callback_arguments" ] ]
- 示例
"classA" : { "getFrom" : [ ["FactoryClassA", "getClassA"], ["arg1","arg2"] ] }, "interfaceB" : { "getFrom" : [ ["FactoryClassB", "getClassB"], ["arg1",["array", "arguments"]] ] }
使用场景
请检查 tests/Data/config
中的单元测试配置文件和相应的测试。
变更日志
贡献
所有形式的贡献都受欢迎。您可以 报告问题,fork 存储库并提交拉取请求。
对于重大更改,请首先打开一个问题来讨论您想进行更改的内容。
请确保根据需要更新测试。
许可
根据 Apache-2.0 许可证 发布。有关详细信息,请参阅 LICENSE 文件。
Copyright 2020 Asis Pattisahusiwa
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://apache.ac.cn/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.