定制 scruwi/container 二次开发

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

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

scruwi/container

Composer 安装命令:

composer require scruwi/container

包简介

PSR-11 Container implementation

README 文档

README

SWUbanner Latest Stable Version Total Downloads License PHP Version Require codecov

It's my own implementation PSR-11 Container Interface.

Installation

composer require scruwi/container

Init

$container = new Container([
    new ClassResolver(['App\\Namespace1', 'App\\Namespace2']),
    new ParameterResolver(['param1' => true, 'param2' => 'string']),
    new DefinitionResolver([Definition1:class, Definition2:class]),
]);

Resolvers

You can implement your resolvers in a project if you need them. Look at the Interfaces namespace.

ClassResolver

Only classes from defined namespaces can be autowired.

ParameterResolver

Typical usage:

$rootParameters = ['param1' => true, 'param2' => 'string'];
$classSpecificParameters = [SpecificClass::class => ['param1' => false]];
$resolver = ParameterResolver($rootParameters, $classSpecificParameters);

Expected behaviour:

class SomeClass
{
    public function __construct(bool $param1) { /* $param1 === true */ }
}
class SpecificClass
{
    public function __construct(bool $param1) { /* $param1 === false */ }
}

Parameter with default value does not have to be resolved by the config:

class SpecificClass
{
    public function __construct(bool $param0 = 'default') { /* $param0 === 'default' */ }
}

You can create a specific resolver in your way and use it in your project. Just implement ParameterResolverInterface and specify it during container construct.

DefinitionResolver

Attach definitions to container.

Definition, there is no more than a factory that creates an object in a specific way. There is one specific ReflectionClassDefinition that constructs classes by their reflections. All autowired classes use that factory to create their object.

You should create as many definitions in your project as you need and specify them during container construct or attach them later by $container->addDefinition() method. Any definition must implement DefinitionInterface.

Typical definition class looks like this:

class ExampleDefinition implements DefinitionInterface
{
    public static function getId(): string
    {
        return ExampleClass::class;
    }

    public function __invoke(Container $container, ParameterResolver $resolver, BuildContext $context): object
    {
        return new ExampleClass();
    }
}

You can fetch some default parameters from the ParameterResolver:

    public function __invoke(Container $container, ParameterResolver $resolver, BuildContext $context): object
    {
        $param1 = $resolver->resolve('param1', ExampleClass::class);
        return new ExampleClass($param1);
    }

Also, you can find out from which context it was called. It may be useful for autowire interfaces:

class InterfaceDefinition implements DefinitionInterface
{
    public function getId(): string
    {
        return SomeInterface::class;
    }

    public function __invoke(Container $container, ParameterResolver $resolver, BuildContext $context): object
    {
        if ($context->getTargetClass() === ClassWithInterfaceDependency::class) {
            return new SomeClassImplementsInterface2();
        }

        return new SomeClassImplementsInterface1();
    }
}

Exceptions

  • NotFoundExceptionInterface - for an unknown entry
  • ContainerExceptionInterface - for all common exceptions in the container
  • CircularReferencesException - there is a special exception for circular references in the container

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-09-24

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固