pine3ree/pine3ree-auto-resolve-factory 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

pine3ree/pine3ree-auto-resolve-factory

最新稳定版本:1.0.1

Composer 安装命令:

composer require pine3ree/pine3ree-auto-resolve-factory

包简介

A ioc factory that resolves dependencies using reflection

README 文档

README

Continuous Integration

This package provides an autowiring reflection-based factory, which operates using the pine3ree-params-resolver library

Example:

// file: src/My/App/Model/PostMapper.php

use My\App\Database\ConnectionInterface;
use My\App\Database\HydratorInterface;
//...

class PostMapper
{
    ConnectionInterface $db;
    HydratorInterface $hydrator;

    public function __construct(ConnectionInterface $db, HydratorInterface $hydrator)
    {
        $this->db = $db;
        $this->hydrator = $hydrator;
    }

    //...
}

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

// file: test.php

use My\App\Model\PostMapper;
use Psr\Container\ContainerInterface;
use pine3ree\Container\Factory\AutoResolveFactory;

$container = include("config/container.php");

$factory = new AutoResolveFactory(); // We need just one instance of it

// All dependencies of PostMapper are resolved by the factory if they are found
// in the container, i.e. if:
// - $container->get(ConnectionInterface::class) returns a ConnectionInterface object
// - $container->get(HydratorInterface::class) returns a HydratorInterface object

$postMapper = $factory($container, PostMapper::class);

If the container configuration for the service PostMapper::class instructs the container to use the auto-resolve-factory, you can just fetch the service instance from the container:

$postMapper = $container->get(PostMapper::class);

The requested service constructor's arguments are resolved in the following way:

  1. If the argument is a type-hinted dependency with a fully-qualified-class/interface name the factory tries to load a dependency registered in the container with that class-string as identifier. If no such dependency is found, the factory will try the default provided value, if any, then the null value if the argument is nullable, and eventually it will try to instantiate the class directly. An exception is thrown on failure.

  2. If the argument is not type-hinted or is type-hinted with a builtin type the factory will try to load a service or a parameter value registered in the container with the parameter name as identifier. If not found, the factory will try the default provided value, if any, then the null value if the argument is nullable, otherwise an exception is thrown.

Example configuration for Mezzio

// file: config/dependencies.php

//...
use My\App\Database\ConnectionInterface;
use My\App\Database\PdoConnection;
use My\App\Database\PdoConnectionFactory;
use My\App\Database\HydratorInterface;
use My\App\Model\PostMapper;
use pine3ree\Container\Factory\AutoResolveFactory;
//...

return [
    //...
    'invokables' => [
        //...
        HydratorInterface::class => HydratorInterface::class,
        //...
    ],
    'aliases' => [
        //...
        ConnectionInterface::class => PdoConnection::class,
        //...
    ],
    'factories' => [
        //...
        PdoConnection::class => PdoConnectionFactory::class,
        PostMapper::class => AutoResolveFactory::class, // *
        PostListHandler::class => AutoResolveFactory::class, // *
        //...
    ],
    //...
];

// file: my/App/Handler/PostListHandler.php ------------------------------------

use My\App\Model\PostMapper;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

class PostListHandler implements RequestHandlerInterface
{
    public function __construct(PostMapper $postMapper): ResponseInterface
    {
        $this->postMapper = $postMapper;
    }

    public function handle(ServerRequestInterface $request): ResponseInterface
    {
        // use $this->postMapper and $request to build and return a response
    }
}

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2025-05-28