关于在Webman框架中设置依赖注入的问题

Toky

大家好!

我正在使用Webman框架进行我的项目开发,但我在设置依赖注入方面遇到了一些问题。我希望能够得到一些指导或者示例,以便更好地利用Webman框架的依赖注入功能。

我已经查阅了相关文档,但我仍然感到有些困惑。如果有人能够提供一些实际的例子,或者分享一下他们在项目中成功使用依赖注入的经验,我将不胜感激。

同时,我也想分享一下我的 composer.json 文件,以便更好地理解我的项目结构和依赖项。以下是我的 composer.json 内容:

{
  "name": "workerman/webman",
  "type": "project",
  "keywords": [
    "high performance",
    "http service"
  ],
  "homepage": "http://www.workerman.net",
  "license": "MIT",
  "description": "High performance HTTP Service Framework.",
  "authors": [
    {
      "name": "walkor",
      "email": "walkor@workerman.net",
      "homepage": "http://www.workerman.net",
      "role": "Developer"
    }
  ],
  "support": {
    "email": "walkor@workerman.net",
    "issues": "https://github.com/walkor/webman/issues",
    "forum": "http://wenda.workerman.net/",
    "wiki": "http://workerman.net/doc/webman",
    "source": "https://github.com/walkor/webman"
  },
  "require": {
    "php": ">=7.2",
    "workerman/webman-framework": "1.3.14",
    "workerman/gateway-worker": "^3.0",
    "workerman/crontab": "^1.0",
    "webman/redis-queue": "^1.0",
    "monolog/monolog": "^2.0",
    "vlucas/phpdotenv": ">=4.1,<6.0",
    "symfony/translation": "^5.3",
    "illuminate/validation": "^8.29",
    "illuminate/console": "^8.61",
    "illuminate/events": "^8.29",
    "illuminate/database": "^8.29",
    "illuminate/contracts": "^8.39",
    "illuminate/auth": "^8.40",
    "illuminate/pagination": "^8.52",
    "robmorgan/phinx": "^0.12.5",
    "php-di/php-di": "^6.3",
    "doctrine/cache": "^1.10",
    "doctrine/annotations": "^1.12",
    "sentry/sdk": "3.2.0",
    "php-http/curl-client": "^2.2",
    "respect/validation": "^2.2",
    "ramsey/uuid": "^4.2",
    "your-app-rocks/eloquent-uuid": "^2.5",
    "league/csv": "^9.7",
    "phpmailer/phpmailer": "^6.5",
    "phpoffice/phpspreadsheet": "^1.19",
    "aws/aws-sdk-php": "^3.209",
    "antecedent/patchwork": "^2.1",
    "onelogin/php-saml": "1.0.0",
    "silber/bouncer": "1.0.0",
    "fakerphp/faker": "^1.23"
  },
  "suggest": {
    "ext-event": "For better performance. "
  },
  "autoload": {
    "psr-4": {
      "app\\": "app/",
      "support\\": "support"
    },
    "autoload-dev": {
      "psr-4": {
        "Test\\": "./test/"
      }
    },
    "files": [
      "./support/helpers.php"
    ]
  },
  "require-dev": {
    "phpunit/phpunit": "^9.5",
    "mockery/mockery": "^1.5"
  },
  "config": {
    "gitlab-token": {
      "git.company.mg": "xxxx"
    }
  }
}

非常感谢您的帮助!

933 1 1
1个回答

Tinywan

直接按照官方的配置使用就行了

配置文件:config/container.php

<?php
/**
 * @author Tinywan(ShaoBo Wan)
 * @date 2022/11/9 16:57
 */
declare(strict_types=1);

$builder = new \DI\ContainerBuilder();
$builder->addDefinitions(config('dependence', []));
$builder->useAutowiring(true);
return $builder->build();

依赖文件:config/dependence.php

<?php
/**
 * @author Tinywan(ShaoBo Wan)
 * @date 2023/11/9 16:57
 */
declare(strict_types=1);

return [
    // form download
    'form1' => app\common\form\Form1::class,
    ...
];

编写你的业务代码Form1.php

<?php
/**
 * @author Tinywan(ShaoBo Wan)
 * @date 2023/11/9 16:57
 */
declare(strict_types=1);

class Form1
{
    /**
     * constructor.
     * @param array $config
     * @throws NotFoundException
     */
    public function __construct(array $config = [])
    {
        if (empty($config)) {
            throw new NotFoundException('Form1 is not found');
        }
    }

    /**
     * @desc: form download handle
     * @author Tinywan(ShaoBo Wan)
     */
    public function download()
    {
        echo 'form download handle ...';
    }
}

使用容器

/**
 * @desc: 使用容器
 * @author Tinywan(ShaoBo Wan)
 */
public function test()
{
    $container = 'form1';
    if (!\support\Container::has($container)) {
        throw new NotFoundException( '表报容器不存在');
    }

    // 构造函数参数
    $config = [
        'author' => 'Tinywan',
        'name' => '开源技术小栈'
    ];
    $form = (new \Webman\Container())->make(\support\Container::get($container), [$config]);
    // 开始下载
    $form->download();
}
  • Tinywan 2024-01-24

    方便的话,可以打赏个红包!嘻嘻!

  • nitron 2024-01-24

    我记得这是个外国人,用英文的,你看他的这个帖子里的语言,带点机翻的感觉,不过你的代码他应该能看明白

  • Tinywan 2024-01-25

    这个真没看出来,那我改成英文的

  • webman2024 16天前
    /**
     * @Inject
     * @var TestServices
     */
    private $testServices;这个根本不行,有人解决的吗
×
🔝