承接 jitesoft/container 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jitesoft/container

Composer 安装命令:

composer require jitesoft/container

包简介

PSR-11 container with constructor injection.

README 文档

README

pipeline status coverage report Back project Maintainability

Container

IoC container with constructor dependency injection.

Bindings

The container binds a key to a value where the value can be any type or a class name.

If it is a object, primitive or any type of instance, it will store the instance as a static object, that is: the same object will be returned at each get call. While if a class name is passed the container will try to create an instance of the class on each get call. If it can not create an instance, a NotFoundException or ContainerException will be thrown.

The binding can be done at creation by passing an associative array into the constructor, or by using the set method. To re-bind, the rebind method - accepting a key and a value - can be used.

The container implements the PSR-11 Container interface. Further, the container implements the ArrayAccess interface, enabling fetching by using the array index syntax as: $container[Interface::class].

Usage

The container implements the following interfaces:

interface ContainerInterface {
  public function get($abstract): mixed;
  public function has($abstract): bool;
}

interface ArrayAccess {
  public function offsetExists($offset);
  public function offsetGet($offset);
  public function offsetSet($offset, $value);
  public function offsetUnset($offset);
}

The ArrayAccess interface allows for getting, setting and un-setting bindings through the array index syntax:

$c['Abstract'] = 'Concrete';
echo $c['Abstract']; // 'Concrete'
unset($c['Abstract'];

Furthermore, the class implements the following public methods:

class Container {
  public function __construct(?array $bindings);
  public function clear();
  public function set(string $abstract, mixed $concrete, ?bool $singleton = false): bool;
  public function rebind(string $abstract, mixed $concrete, ?bool $singleton = false): void;
  public function unset(string $abstract): void;
  
  public function singleton(string $abstract, mixed $concrete): void;
}

The constructor of the class takes an optional bindings array. The array expected to be an associative array containing the abstract as key and concrete as value. If wanted, the concrete could be another associative array with a class or func key containing the class or callable to resolve to/with and a singleton key with a boolean value.
If the singleton key is true, the container will only ever create a single instance of the resolved value or only run the resolve function once.

Example:

$container = new Container([

  // With objects:
  InterfaceA::class => $objectInheritingInterfaceA,
  InterfaceB::class => [
    'class' => $objectInheritingInterfaceB,
    'singleton' => true
  ],

  // With classes:
  InterfaceA::class => ClassA::class,
  InterfaceB::class => [
    'class' => ClassB::class,
    'singleton' => true
  ],

  // Or with functions:
  InterfaceA::class => static fn (InterfaceB $ib) => new ClassA($ib),
  InterfaceB::class => [
    'class' => static function(InterfaceC $c) {
      return new ClassB($c);
    },
    'singleton' => true
  ],

]);

$container->get(InterfaceA::class); // Will be a new object of the ClassA class.
$container->get(InterfaceB::class); // On first call, it will be resolved to a ClassB class.
$container->get(InterfaceB::class); // On all other calls, the object will be the same as the first call.

Alternatively to the array type singleton binding, the interface will create a singleton binding with the singleton method.

Rebinding can be done in runtime with the $container->rebind($a, $c, $singleton); method.
This will unset the earlier binding and create a new.

To remove all the current bindings, the $container->clear(); method can be used, which will empty the inner list of entries. Observe that this will not clear up the currently resolved instances of objects stored in your classes, but rather just remove all the entries from the container.

All the methods implemented in the class (with an exception in the has and clear methods) will throw exceptions on errors.
The following two exceptions are used:

Jitesoft\Exceptions\Psr\Container\ContainerException implements ContainerExceptionInterface;
Jitesoft\Exceptions\Psr\Container\NotFoundException  implements NotFoundExceptionInterface;

So when checking for exceptions, one could use either the underlying JitesoftException class, the specific classes or the interfaces.
Observe though that the NotFoundException inherits from the ContainerException so in the cases where both can be returned and you want to catch the specific exceptions, catch the NotFoundException before the ContainerException.

Dependency injection

The container will, in the cases where it is able to, inject dependencies into the constructor when resolving the object.
There are some requirements before it will be able to do this though:

  1. The parameter need to be typehinted.
  2. The parameter need to be possible to resolve in the container or be possible to creat without constructor.

If the container can not resolve the parameter, it will throw an exception, but following the above two requirements, this should not happen.

class ClassA {}

class ClassB {
  __constructor(ClassA $b) { }
}

$container->set(ClassB::class, ClassB::class);
$container->get(ClassB::class); // Will throw a ContainerException due to class A not being bound.

$container->set(ClassA::class, ClassA::class);
$container->get(ClassB::class); // Will not throw any exception. ClassA is resolved and pushed into the constructor of ClassB.

Not only classes

The container does not only resolve bindings, but can be used to store other values too.
If the passed concrete is not a class name, it will use it as a single value and not resolve. So passing a string or number as the concrete will make the get method return the value.

$container->set('Something', 123);
$container->get('Something'); // 123

License

MIT

jitesoft/container 适用场景与选型建议

jitesoft/container 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 632 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 12 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 jitesoft/container 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 jitesoft/container 我们能提供哪些服务?
定制开发 / 二次开发

基于 jitesoft/container 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 632
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 7
  • 依赖项目数: 7
  • 推荐数: 1

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-12-22