chubbyphp/chubbyphp-container

一个简单的PSR-11容器实现。

2.2.0 2023-11-27 20:24 UTC

This package is auto-updated.

Last update: 2024-09-10 20:07:31 UTC


README

CI Coverage Status Mutation testing badge Latest Stable Version Total Downloads Monthly Downloads

bugs code_smells coverage duplicated_lines_density ncloc sqale_rating alert_status reliability_rating security_rating sqale_index vulnerabilities

描述

一个最小的依赖注入容器(DIC),实现了PSR-11。 DI Container Benchmark

chubbyphp/chubbyphp-laminas-config 有一个 laminas 服务管理器适配器。

需求

安装

通过 Composer 安装 chubbyphp/chubbyphp-container

composer require chubbyphp/chubbyphp-container "^2.2"

使用方法

有两个PSR-11实现

  • Chubbyphp\Container\Container 原型(每次获取都会返回一个新的实例)和共享服务
  • Chubbyphp\Container\MinimalContainer 共享服务

MinimalContainer / Container

工厂

<?php

use App\Service\MyService;
use Chubbyphp\Container\MinimalContainer;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

$container = new MinimalContainer();
$container->factories([
    MyService::class => static function (ContainerInterface $container): MyService {
        return new MyService($container->get(LoggerInterface::class));
    },
]);

工厂

<?php

use App\Service\MyService;
use Chubbyphp\Container\MinimalContainer;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

$container = new MinimalContainer();

// new
$container->factory(MyService::class, static function (ContainerInterface $container): MyService {
    return new MyService($container->get(LoggerInterface::class));
});

// existing (replace)
$container->factory(MyService::class, static function (ContainerInterface $container): MyService {
    return new MyService($container->get(LoggerInterface::class));
});

// existing (extend)
$container->factory(
    MyService::class,
    static function (ContainerInterface $container, callable $previous): MyService {
        $myService = $previous($container);
        $myService->setLogger($container->get(LoggerInterface::class));

        return $myService;
    }
);

带参数的工厂

<?php

use Chubbyphp\Container\MinimalContainer;
use Chubbyphp\Container\Parameter;

$container = new MinimalContainer();
$container->factory('key', new Parameter('value'));

获取

<?php

use App\Service\MyService;
use Chubbyphp\Container\MinimalContainer;

$container = new MinimalContainer();

$myService = $container->get(MyService::class);

存在

<?php

use App\Service\MyService;
use Chubbyphp\Container\MinimalContainer;

$container = new MinimalContainer();
$container->has(MyService::class);

容器

MinimalContainer 的所有方法以及以下

原型工厂

每次获取都会返回一个新的实例

<?php

use App\Service\MyService;
use Chubbyphp\Container\Container;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

$container = new Container();
$container->prototypeFactories([
    MyService::class => static function (ContainerInterface $container): MyService {
        return new MyService($container->get(LoggerInterface::class));
    },
]);

原型工厂

每次获取都会返回一个新的实例

<?php

use App\Service\MyService;
use Chubbyphp\Container\Container;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

$container = new Container();

// new
$container->prototypeFactory(
    MyService::class,
    static function (ContainerInterface $container): MyService {
        return new MyService($container->get(LoggerInterface::class));
    }
);

// existing (replace)
$container->prototypeFactory(
    MyService::class,
    static function (ContainerInterface $container): MyService {
        return new MyService($container->get(LoggerInterface::class));
    }
);

// existing (extend)
$container->prototypeFactory(
    MyService::class,
    static function (ContainerInterface $container, callable $previous): MyService {
        $myService = $previous($container);
        $myService->setLogger($container->get(LoggerInterface::class));

        return $myService;
    }
);

迁移

版权

2024 Dominik Zogg