mmdm/sim-container
Composer 安装命令:
composer require mmdm/sim-container
包简介
A simple yet nice dependency injection container
README 文档
README
A library for dependency injection management.
Features
- Auto wiring
- Singleton construction
- Factory construction
- Method injection
- Both method and array access
Install
composer
composer require mmdm/sim-container
Or you can simply download zip file from github and extract it, then put file to your project library and use it like other libraries.
Just add line below to autoload files:
require_once 'path_to_library/autoloader.php';
and you are good to go.
How to use
// to instance a container object $container = new Container(); // here you can define your object and use them
Available functions
Container
set($abstract, $concrete = null, ?string $method_name = null, array $method_parameters = []): Container
This method store a $concrete with alias of $abstract like class name or any other name.
Version >= 1.2.0:
Also you can pass the method to be inject as $method_name and
parameters of the method as $method_parameters. Parameters can be
the actual parameter or an abstract to get by container.
Note: You can pass $concrete as $abstract
Note: Parameters are a key value pair like below:
parameter_name => your assignment,
index_of_parameter like 0|1|2|... => your assignment,
// example
'class_type' => CustomClass::class,
1 => 'A parameter',
// store a class $container->set('log_cls', Logger::class); // or simply store with Logger::class string // alias will be the string from Logger::class $container->set(Logger::class); // or if you have some works to do and return a // new instance then use this $container->set('alias of class', function (Container $c) { // do anything you need // ... // return a new instance of a class return new SomeClass(); }); // method injection $container->set('log_cls', Logger::class, 'info'); // or with parameters $container->set('log_cls', Logger::class, 'info', [ 'handler' => Handler::class, 1 => '{level} - {other_parameter} - {message}', ]);
get($abstract, ?string $method_name = null, array $method_parameters = [])
This method return a $concrete that stored with alias of $abstract. If it was not exists, it will simply store it first and then resolve that.
Version >= 1.2.0:
Also you can pass the method to be inject as $method_name and
parameters of the method as $method_parameters. Parameters can be
the actual parameter or an abstract to get by container.
Note: If a stored $concrete has been resolved, it will not resolve again, just return previous resolved $concrete.
Note: Parameters are a key value pair like below:
parameter_name => your assignment,
index_of_parameter like 0|1|2|... => your assignment,
// example
'class_type' => CustomClass::class,
1 => 'A parameter',
// retrieve a class $container->get('log_cls'); // or $container->get(NotStoredClass::class);
make($abstract, ?string $method_name = null, array $method_parameters = [])
This method return a $concrete that stored with alias of $abstract. If it was not exists, it will simply store it first and then resolve that.
Parameters are same as get method
// retrieve new instance of a class $container->make('log_cls'); // or $container->make(NotStoredClass::class);
has($abstract, string $method_name = null): bool
This method check if a specific alias $abstract is stored or not. Also
you can pass the $method_name to check if specific $abstract's
method is registered.
// check existence of a $abstract $container->has('log_cls'); // or $container->has(NotStoredClass::class);
unset($abstract, string $method_name = null): Container
This method remove a stored $abstract. Also you can
pass the $method_name to remove specific $abstract's
method.
// remove an $abstract $container->unset('log_cls'); // or $container->unset(NotStoredClass::class);
Singleton accessing
If you need to access container in singleton manner, use
getInstance static method.
$container = Container::getInstance(); // then use all methods $container->get($abstract); //...
Container Inheritance
If you want to inherit Container (to customize maybe), you
can inherit like normal way but if you have multiple class to
inherit, then use ContainerTrait inside your class and inherit
other class as well.
Note: After a trait using, you should type hint Container
to YourOtherClass in closures.
class YourOtherClass extends AnotherClass { use ContainerTrait; // other codes } $instance = new YourOtherClass(); // the difference is in [YourOtherClass] instead of [Container] $instance->set($abstract, function (YourOtherClass $c) { // some code });
Array accessing
You can use array accessing instead of method accessing:
$container[$abstract] = $concreteinstead of$container->set($abstract, $concrete, ?string $method_name = null, array $method_parameters = []).
Accepts $offset as below:
[
'concrete' => $concrete,
'method' => [
'name' => method's name,
'parameters' => [
method_parameter1 => CustomClass::class or Specified name in container,
method_parameter2 => AnotherCustomClass::class or Specified name in container
].
],
]
OR
object(stdClass) {
'concrete' => $concrete,
'method' => [
'name' => method's name,
'parameters' => [
method_parameter1 => CustomClass::class or Specified name in container,
method_parameter2 => AnotherCustomClass::class or Specified name in container
].
],
}
OR
An encoded json that has above structure
OR
An optional $abstract variable and $concrete/$abstract value
$concrete = $container[$abstract]instead of$concrete = $container->get($abstract, ?string $method_name = null, array $method_parameters = []).
Accepts $offset as below:
[
'abstract' => $abstract,
'method' => [
'name' => method's name,
'parameters' => [
method_parameter1 => CustomClass::class or Specified name in container,
method_parameter2 => AnotherCustomClass::class or Specified name in container
].
],
]
OR
object(stdClass) {
'abstract' => $abstract,
'method' => [
'name' => method's name,
'parameters' => [
method_parameter1 => CustomClass::class or Specified name in container,
method_parameter2 => AnotherCustomClass::class or Specified name in container
].
],
}
OR
An encoded json that has above structure
OR
An $abstract variable
isset($container[$abstract])instead of$container->has($abstract, string $method_name = null).
Accepts $offset as below:
[
'abstract' => $abstract,
'method' => [
'name' => method's name,
],
]
OR
object(stdClass) {
'abstract' => $abstract,
'method' => [
'name' => method's name,
],
}
OR
An encoded json that has above structure
OR
An $abstract variable
unset($container[$abstract])instead of$container->unset($abstract, string $method_name = null).
Accepts $offset as below:
[
'abstract' => $abstract,
'method' => [
'name' => method's name,
],
]
OR
object(stdClass) {
'abstract' => $abstract,
'method' => [
'name' => method's name,
],
}
OR
An encoded json that has above structure
OR
An $abstract variable
License
Under MIT license.
mmdm/sim-container 适用场景与选型建议
mmdm/sim-container 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20 次下载、GitHub Stars 达 1, 最近一次更新时间为 2020 年 08 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 mmdm/sim-container 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mmdm/sim-container 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 20
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-08-02