大家好!
我正在使用Webman框架進(jìn)行我的項(xiàng)目開發(fā),但我在設(shè)置依賴注入方面遇到了一些問題。我希望能夠得到一些指導(dǎo)或者示例,以便更好地利用Webman框架的依賴注入功能。
我已經(jīng)查閱了相關(guān)文檔,但我仍然感到有些困惑。如果有人能夠提供一些實(shí)際的例子,或者分享一下他們在項(xiàng)目中成功使用依賴注入的經(jīng)驗(yàn),我將不勝感激。
同時(shí),我也想分享一下我的 composer.json 文件,以便更好地理解我的項(xiàng)目結(jié)構(gòu)和依賴項(xiàng)。以下是我的 composer.json 內(nèi)容:
{
"name": "workerman/webman",
"type": "project",
"keywords": [
"high performance",
"http service"
],
"homepage": "http://wtbis.cn",
"license": "MIT",
"description": "High performance HTTP Service Framework.",
"authors": [
{
"name": "walkor",
"email": "walkor@workerman.net",
"homepage": "http://wtbis.cn",
"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"
}
}
}
非常感謝您的幫助!
配置文件:
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,
...
];
編寫你的業(yè)務(wù)代碼
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( '表報(bào)容器不存在');
}
// 構(gòu)造函數(shù)參數(shù)
$config = [
'author' => 'Tinywan',
'name' => '開源技術(shù)小棧'
];
$form = (new \Webman\Container())->make(\support\Container::get($container), [$config]);
// 開始下載
$form->download();
}