chubbyphp/chubbyphp-laminas-config-factory

1.3.0 2023-11-27 20:46 UTC

This package is auto-updated.

Last update: 2024-09-10 20:00:19 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

描述

这是一个用于 laminas/laminas-servicemanager 及任何其他能够处理其配置的依赖注入容器(如 chubbyphp/chubbyphp-container 通过 chubbyphp/chubbyphp-laminas-config 以及许多其他容器(Aura.Di, Pimple, Auryn, Symfony, PHP-DI))的抽象服务工厂。

这个抽象服务工厂的原型是由 @DASPRiDdasprid/container-interop-doctrine 中提出的,后来转交给 roave/psr-container-doctrine

对概念的微小调整以及将其作为所有服务工厂的基础的可能性使我决定将其作为一个独立的仓库发布。

要求

安装

通过 Composer 安装 chubbyphp/chubbyphp-laminas-config-factory

composer require chubbyphp/chubbyphp-laminas-config-factory "^1.3"

使用方法

<?php

declare(strict_types=1);

namespace MyProject\Factory;

use Chubbyphp\Laminas\Config\Factory\AbstractFactory;
use MyProject\Service\ServiceA;
use MyProject\Service\ServiceB;
use MyProject\Service\ServiceC;
use Psr\Container\ContainerInterface;

final class ServiceAFactory extends AbstractFactory
{
    public function __invoke(ContainerInterface $container): ServiceA
    {
        return new ServiceA(
            $this->resolveConfig($container->get('config')['serviceA'] ?? []),
            $this->resolveDependency($container, ServiceB::class, ServiceBFactory::class),
            $this->resolveDependency($container, ServiceC::class, ServiceCFactory::class)
        );
    }
}

/** @var ContainerInterface $container */
$container = ...;

// without name
$serviceA = (new ServiceAFactory())($container);

// with name
$serviceA = [ServiceAFactory::class, 'default']($container);

版权

2024 Dominik Zogg