定制 opxcore/container 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

opxcore/container

Composer 安装命令:

composer require opxcore/container

包简介

The OpxCore dependency injection container.

README 文档

README

Build Status Coverage Status Latest Stable Version Total Downloads License

Introduction

The dependency injection container is a powerful tool for managing class dependencies and performing dependency injection. Class dependencies are "injected" into the class via the constructor and resolved by the container.

Example:

class Controller
{
    protected Repository $repository;
    
    public function __construct(Repository $repository)
    {
        $this->repository = $repository;
    }
}

Calling $container->make(Controller::class) would be equal to new Controller(new Repository). This amazing feature resolves all dependency injections automatically with zero-config. For this example if Repository have its own dependency, it will be resolved the same.

Installing

composer require opxcore/container

Creating

You can create a container several ways:

$container = Container::setContainer(new Container);

or

$container = Container::getContainer();

In all of this cases Container::getContainer() will always return the same container instance.

If you want to create and handle a container (or several containers) by yourself just use $container = new Container and handle this container instance as you want.

Registering a binding with the container

Basic binding to container looks like:

$container->bind($abstract, $concrete, $parameters);

$abstract is a string containing class name or shorthand for a name to be resolved.

$concrete is a string containing class name or callable returning object for a name to be resolved to. Container instance and parameters will be passed to callable during resolving.

$parameters is an array or callable returning array with parameters used for resolving (see below). Default value is null means no parameters will be bound.

Examples:

Simple usage

$container->bind('logger', Logger::class);

// New instance of Logger with resolved dependencies will be retuened.
$logger = $container->make('logger');

Binding interfaces to its realizations

We have Controller class what depends on some RepositoryInterface and FileRepository implementing it:

class Controller
{
    protected RepositoryInterface $repository;
    
    public function __construct(RepositoryInterface $repository)
    {
        $this->repository = $repository;
    }
}
class FileRepository implements RepositoryInterface
{
    protected string $path;
    
    public function __construct(string $path)
    {
        $this->path = $path;
    }
}

Now we bind FileRepository::class to RepositoryInterface::class so when some class depends on RepositoryInterface it will be resolved to FileRepository and path argument will be passed into with specified value.

$container->bind(RepositoryInterface::class, FileRepository::class, ['path'=>'/data/storage']);

$controller = $container->make(Controller::class);

Binding parameters

You can bind parameters into the container fo resolving. It can be used as primitives binding or class binding:

// When FileRepository dependencies would be resolving, given value would be passed to `path` attribute.
$container->bindParameters(FileRepository::class, ['path' => '/data/storage']);

// When Controller would be resolved a FileRepository would be given as RepositoryInterface dependency.
$container->bindParameters(Controller::class, [RepositoryInterface::class => FileRepository::class]);

Singleton

The singleton method binds a class or interface into the container that should be resolved once. First time it will be resolved and stored in the container, so other times the same object instance will be returned on subsequent calls into the container.

$container->singleton(RepositoryInterface::class, FileRepository::class, ['path'=>'/data/storage']);

// Each time when RepositoryInterface needs to be resolved
// the same instance of FileRepository would be given.
$repository = $container->make(RepositoryInterface::class);

Alias

Alias is another name for resolving.

$container->singleton(RepositoryInterface::class, FileRepository::class, ['path'=>'/data/storage']);
$container->alias(RepositoryInterface::class, 'repo');

// This would return FileRepository instance.
$container->make('repo');

Instance

You can register any object or value into the container.

$container->instance('path', '/data/storage');
// '/data/storage' would be returned
$container->make('path');

$container->instance(RepositoryInterface::class, new FileRepository('/data/storage'));
// FileRepository would be returned
$container->make(RepositoryInterface::class);

Resolving

When resolving some name, you can pass parameters to make() function to attach it for resolving subject.

$container->bind(RepositoryInterface::class, FileRepository::class);
$container->make(RepositoryInterface::class, ['path'=>'/data/storage']);

Sequence of making subject

When resolving some subject, the container checks for registered aliases first, checks for registered instances and resolves using reflections.

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-01-12