承接 gdetassigny/simple-container 相关项目开发

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

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

gdetassigny/simple-container

Composer 安装命令:

composer require gdetassigny/simple-container

包简介

A simple implementation of the standard container interface, with support for autowiring

README 文档

README

Build Status Coverage Status

This is a simple implementation of the standard container interface, with support for autowiring.

This means that this package will attempt to figure out all your basic dependencies for you. It also supports manually defining dependencies in a YAML file, or even adding service providers for more complex dependencies.

Installation

This package requires at least PHP 7.2.

Install it using composer:

composer require gdetassigny/simple-container

How to use it

Basic usage

Setting up a basic container is quite simple:

use GabrielDeTassigny\SimpleContainer\ContainerProvider;

$container = (new ContainerProvider())->getContainer();

That's it! You can now request your dependencies from the container, thanks to autowiring.

$container->get(Foo\Bar::class); // returns an instance of Foo\Bar

Autowiring

The container will be able to instantiate simple services which have straightforward dependencies in their constructors:

The below examples can all be autowired:

$container = (new ContainerProvider())->getContainer();

class NoConstructor {}

$container->get(NoConstructor::class);

class ConstructorNoParam {
    public function __construct() {}
}

$container->get(ConstructorNoParam::class);

class ConstructorWithClassParam {
    public function __construct(stdClass $param) {}
}

// Note: $param will be also instantiate and injected in the constructor
$container->get(ConstructorWithClassParam::class);

class ConstructorWithDefaultParam {
    public function __construct(?string $test = null) {}
}

// Note: parameters with default values will be set to these values when autowired
$container->get(ConstructorWithDefaultParam::class);

Some cases cannot be autowired:

$container = (new ContainerProvider())->getContainer();

class ConstructorWithPrimitiveParam {
    public function __construct(string $test) {}
}

// Fails! Autowiring cannot figure out the value of the $test parameter.
// Consider using a service provider
$container->get(ConstructorWithPrimitiveParam::class);

interface FooInterface {}
class Bar implements FooInterface {}

// Fails! Autowiring does not know which concrete class you want
// Consider using either a YAML Config or a service provider 
$container->get(FooInterface::class);

YAML Config

Simple container also supports adding a YAML config to manually define dependencies.

This can be useful to define a concrete class implementation of an interface. Or you may want to define a different name for your service than its class name.

dependencies:
  Foo\Bar:
    name: Foo\BarInterface
  
  Foo\Baz:
    name: some-id
    dependencies:
      - Foo\BarInterface

Simply pass the path of the YAML file to the container provider:

use GabrielDeTassigny\SimpleContainer\ContainerProvider;

$container = (new ContainerProvider('/path/to/config.yaml'))->getContainer();

$container->get(Foo\BarInterface::class); // returns an instance of Foo\Bar
$container->get('some-id'); // returns an instance of Foo\Baz

Service Provider

If a service requires a more complex setup, you may want to look at using a service provider.

use GabrielDeTassigny\SimpleContainer\ServiceProvider;
use GabrielDeTassigny\SimpleContainer\ContainerProvider;

class FooServiceProvider implements ServiceProvider
{
    public function getService(): object
    {
        return new Foo('some-string');
    }
}

$containerProvider = new ContainerProvider();
$containerProvider->registerService(Foo::class, new FooServiceProvider());

$container = $containerProvider->getContainer();

$container->get(Foo::class); // returns the instance of Foo defined in FooServiceProvider

Ordering

When called, the container will look for your service using all 3 above options in that order:

  • service provider
  • YAML config
  • autowiring

Why use this instead of another container package?

To be honest? Maybe you shouldn't. I built this mostly out of interest to understand how containers work under the hood. There are a lot of more evolved PHP containers out there.

However, I tried to make this package as straightforward to use as possible. If having a container up and running very quickly appeals to you then feel free to give this a go!

Furthermore, as it respects the standard container interface, you can always try it for a bit and later switch to another container package without too much hassle.

gdetassigny/simple-container 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-10-08