m0rtis/simple-box 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

m0rtis/simple-box

Composer 安装命令:

composer require m0rtis/simple-box

包简介

Simple PSR-11 container with optional autowiring

README 文档

README

pipeline status coverage report

Simple and little PSR-11 dependency injection container with optional autowiring. This package contains only 4 files, 2 of which implement exception interfaces required by PSR-11.

Installing

The best way is to install this package via composer:

composer require m0rtis/simplebox

Usage

Basic

If you don't need autowiring you should to use m0rtis\SimpleBox\Container class in your application.

use m0rtis\SimpleBox\Container

$container = new Container([$arrayOrIteratorWithYourData]);

But if you want to use autowiring please feel free to instantiate m0rtis\SimpleBox\AutowiringContainer class which extends the basic container class.

use m0rtis\SimpleBox\AutowiringContainer

$container = new AutowiringContainer([$arrayOrIteratorWithYourData]);

If you are using autowiring container you can get any existent (class_exists === true) class even if you didn't put it to container before! Just request it by class name and AutowiringContainer will try make an instance for you. We recommend to check if container can instantiate an object for you using PSR-11 has method:

if ($autowiringContainer->has(RequiredClass::class) {
    $object = $autowiringContainer->get(RequiredClass::class);
}

Please note that SimpleBox container implements ArrayAccess interface. It means you can use SimpleBox container like an array:

if (isset($autowiringContainer[RequiredClass::class])) {
    $object = $autowiringContainer[RequiredClass::class];
}

Configuration

There is no any separate storage in SimpleBox for configuration. SimpleBox container looks for configuration by item id config. This item is expected to be an array or iterable. The configuration related to a particular class must be placed under the key with the name of this class or one of the interfaces that it implements. For example, we have a class:

class SomeAwesomeClass implements AwesomeInterface
{
    private $config;
    
    public function __construct (iterable $config)
    {
        $this->config = $config;
    }
}

As you can see it requires some config for instantiation (name of argument MUST be exactly config). So you should to place config for it this way:

$container = new AutowiringContainer([
    'config' => [
        SomeAwesomeClass::class => [
            'configKey' => 'configValue'
        ]
    ]
]);

or this way:

$container = new AutowiringContainer([
    'config' => [
        AwesomeInterface::class => [
            'configKey' => 'configValue'
        ]
    ]
]);

In both cases when you will query SomeAwesomeClass from autowiring container requested class will be instantiated with his own config:

$config = ['configKey => 'configValue']

SimpleBox container itself has the only config option now - return_shared.

This is a flag that indicates whether the container will return the same object with the same queries or create a new object each time. Default behavior is to return the same object. You can see example in our tests:

$result1 = $container->get(DependencyTwo::class);
$result2 = $container->get(DependencyTwo::class);

$this->assertSame($result1, $result2);

If you want to change default behavior you need to give to container's constructor array or another iterable with following structure:

[
    'config' => [
        'Psr\Container\ContainerInterface::class' => [
            'return_shared' => false
        ]
    ]
]

Also you can get a new object instead of the same as previously queried by using m0rtis\SimpleBox\Container::create(string $id) method.

How to define services

You can use several different ways to define your services in container:

  • Callback function that receives the container as argument and returns initialized service (Pimple like way):

    $container['serviceName'] = function (ContainerInterface $c) {
         return new AwesomeService($c->get('AwesomeServiceDependency'));
    };
    
  • Factory class name. Factory should be invokable (implement the magic method __invoke), has the word "factory" in its name and should not has any dependencies. The __invoke method should accept a Psr\Container\ContainerInterface as only argument:

    $container['serviceName'] = ServiceFactory::class;
    class ServiceFactory
    {
      public function __invoke(ContainerInterface $c)
      {
          return new AwesomeService($c->get('AwesomeServiceDependency'));
      }
    }
    
  • Any callable. Including static method name as a string.

NOTE: The exception is the invokable classes (implements __invoke magic method) without the word "factory" in its name. Such classes are callable too but SimpleBox container would not call them, just return as is. For example:

$service = $container->get(SomeInvokableClass::class); //$service instanceof SomeInvokableClass

$service = $container->get(ServiceFactory::class): //$service is the result returned by ServiceFactory::__invoke method

Autowiring

This chapter of README is under construction. But you can see tests as a simple example. And, of course, you can always ask me at the issues page or by e-mail.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Author

Anton Fomichev aka m0rtis - mail@m0rtis.ru

License

This project is licensed under the Apache 2.0 license - see the LICENSE file for details

m0rtis/simple-box 适用场景与选型建议

m0rtis/simple-box 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 28 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 04 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「dependency injection」 「container」 「di」 「Autowiring」 「PSR-11」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 m0rtis/simple-box 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2018-04-07