定制 peroxide/container 二次开发

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

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

peroxide/container

Composer 安装命令:

composer require peroxide/container

包简介

Peroxide Container, a simple factory-driven PSR-11 Dependency Injection Container with zero dependencies and low functionalities.

README 文档

README

eng / pt-br

Peroxide/Container

A straightforward Dependency Injection container, designed for use with APIs, adhering to the PSR-11 standard. It boasts minimal functionality and operates independently, free from external dependencies.

Our filosophy

We are passionate about working with components that are as clean and simple as possible. Peroxide\Container is a fusion of inspiration drawn from libraries such as Laminas\ServiceManager, Pimple, and with a touch of PHP-DI.

The great advantage is that we have no external dependencies. All configuration is achieved through PHP code using array configuration files. All you need to do is ensure that your framework supports PSR-11, set up the configuration, and you're ready to begin your coding journey.

How to use it

Instaling

composer require peroxide/container

Starting your journey

Peroxide\Container is fully compliant with PSR-11, and it provides the following methods:

# From PSR-11
public function get(string $id): object;
public function has(string $id): bool;

# From our interface SetDependency
public function set(string $id, callable $factory): void;
public function setInvokableClass(string $id, string $invocableClass): void;

Create your configuration as array

<?php
use Peroxide\DependencyInjection\Container;

$config = [
    YourDependencyName::class => fn() => new YourDependencyName(),
    YourDependency::class     => YourDependencyFactoryClass::class,
    
    // should be invokable class
    ConcreteClass::class      => new ConcreteClassFactory(),
    
    // Or passing as reference string
    ConcreteClass::class      => ConcreteClassFactory::class
];

$container = new Container($config);

// how to get dependencies
$container->get(YourDependencyName::class);
$container->get(YourDependency::class);
$container->get(ConcreteClass::class);

Creating your Factory Class

use Psr\Container\ContainerInterface;
use Peroxide\DependencyInjection\Interfaces\ContainerFactory;

class ConcreteClassFactory implements ContainerFactory
{
    public function __invoke(ContainerInterface $container): ConcreteClass
    {
        // config your dependency injection here
        // you can compose your dependency
        // return new ParentDependency($container->get(DependencyChild::class));
        return new ConcreteClass();
    }
}

It is also possible to set dependencies separately, after obtaining your container instance:

use Peroxide\DependencyInjection\Container;

$container = new Container();

$container->set(DependencyPath::class, fn() => new DependencyInstance());

If the dependency doesn't exist, it will be created; otherwise, it will be replaced by the new factory.

More configurations

To handle dependency injection within the container, you can easily use arrow function to compose your dependencies.

$container = new Container([
    // Dependency parent with dependency child
    
    // all dependencies should be involved by a Closure(function() or fn()) 
    Dependency::class              => fn() => new Dependency(),
    
    ComponentThatHasAnotherDependency::class => function($container) { 
        return new ComponentThatHasAnotherDependency(
            $container->get(Dependency::class)
        );
    },

    // or simply
    ComponentThatHasAnotherDependency::class => fn($c) => new ComponentThatHasAnotherDependency($c->get(Dependency::class)),

    // more complex injections
    ComponentThatHasTwoDeps::class => fn($c) => new ComponentThatHasTwoDeps(
        $c->get(Dependency::class),
        $c->get(AnotherDependency::class),
    )
]);

You can also compose your configuration using the spread operator, as shown in the example:

use Peroxide\DependencyInjection\Container;
# on 'dependencies.php' config file
$config1 = [ ... ];
$config2 = [ ... ];
return [...$config1, ...$config2];

// -------------------

$config = require __DIR__ . '/dependencies.php';

$container = new Container($config);

How to deal with Singleton?

Just use the Singleton invocable class, here's an example:

use Peroxide\DependencyInjection\Container;
use Peroxide\DependencyInjection\Invokables\Singleton;

$container = new Container([
    // Dependency parent with dependency child
    Dependency::class       => new Singleton(fn() => new Dependency()),
    
    ParentDependency::class => new Singleton(
        fn($container) => new ParentDependency($container->get(Dependency::class))
    ),
    
    // Singleton passing factory as reference string
    ConcreteClass::class    => new Singleton(ConcreteClassFactory::class)
]);

The Peroxide\DependencyInjection\Invokables\Singleton class serves as a wrapper to indicate to our container that we want this class to not create a new instance every time it is retrieved.

The first parameter of Singleton constructor, only accepts callable class or closures.

Why can't I config parameters on container?

We believe that storing configuration values in the dependency container is unnecessary. Instead, each service should be configured using external environment data. By doing so, you can centralize your project's configuration.

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-09-16