hyperf-helper/dependency 问题修复 & 功能扩展

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

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

hyperf-helper/dependency

最新稳定版本:v1.1.1

Composer 安装命令:

composer require hyperf-helper/dependency

包简介

hyperf hylper dependency

README 文档

README

Badges

PHP Composer PHPUnit Release

Introduction

Easy, simple and elegant way to add dependencies in Hyperf

Start

composer require hyperf-helper/dependency

Annotation Params

name type default comment
identifier string '' Dependency identifier. if empty, default value is the annotation className
priority int 1 If there are multiple identical Dependency identifiers, the one with the highest priority will be selected

How to use

  1. We need to add dependencies collected from DependencyCollector to Container
<?php
# config/container.php

<?php
/**
 * Initialize a dependency injection container that implemented PSR-11 and return the container.
 */

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

use Hyperf\Di\Container;
use Hyperf\Di\Definition\DefinitionSourceFactory;
use Hyperf\Utils\ApplicationContext;

$container = new Container((new DefinitionSourceFactory(true))());

if (! $container instanceof \Psr\Container\ContainerInterface) {
    throw new RuntimeException('The dependency injection container is invalid.');
}

/*********     start      ********/
// Add this line between `new Container` and `setContainer()`
\HyperfHelper\Dependency\Annotation\Collector\DependencyCollector::walk([$container, 'define']);
/*********      end       ********/

return ApplicationContext::setContainer($container);
  1. Use Dependency to annotate the dependent class you want to define
declare(strict_types=1);

namespace App\Service;

use HyperfHelper\Dependency\Annotation\Dependency;

// add Dependency annotation
#[Dependency()]
class ExampleService implements ExampleServiceInterface {
    // anything
}
  1. Happy using Inject everywhere
<?php

declare(strict_types=1);

namespace App\Controller;

use Hyperf\Di\Annotation\Inject;
use App\Service\ExampleServiceInterface;


class FooController {

    #[Inject]
    protected ExampleServiceInterface $service;
}

统计信息

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

GitHub 信息

  • Stars: 6
  • Watchers: 3
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-05-06