[已解决]webman依赖自动注入,配置后服务启动报错

9527

问题描述

环境:Webman v1.5.1

DI版本:composer require psr/container ^1.1.1 php-di/php-di ^6 doctrine/annotations ^1.14

程序代码

namespace app\controller;

use app\model\Test;
use support\Request;
use support\Db;
use support\Redis;

class IndexController
{
    protected array $key = [];
    protected Test $test;

    public function __construct(Test $test)
    {
        $this->key[] = "this is index _construct".PHP_EOL;
        $this->test = $test;
    }

    public function index(Request $request)
    {
        Redis::setEx('key', 60, randomChars());
        $key = Redis::get('key');
        return json(['key' => count($this->key), 'x' => $key]);
    }

报错信息

Workerman[start.php] start in DEBUG mode
--------------------------------------------------------- WORKERMAN ---------------------------------------------------------
Workerman version:4.1.9          PHP version:8.1.12           Event-Loop:\Workerman\Events\Event
---------------------------------------------------------- WORKERS ----------------------------------------------------------
proto   user            worker                                          listen                 processes    status
tcp     a         webman                                          http://0.0.0.0:8787    1             [OK]
tcp     a         monitor                                         none                   1             [OK]
tcp     a         plugin.webman.redis-queue.consumer              none                   1             [OK]
tcp     a         plugin.webman.redis-queue.consumer.inventory    none                   1             [OK]
tcp     a         plugin.commerce.order.waiting:cancel.toQueue    none                   1             [OK]
-----------------------------------------------------------------------------------------------------------------------------
Press Ctrl+C to stop. Start success.
DI\Definition\Exception\InvalidDefinition: Entry "process\Monitor" cannot be resolved: Parameter $monitorDir of __construct() has no value defined or guessable
Full definition:
Object (
    class = process\Monitor
    lazy = false
    __construct(
        $monitorDir = #UNDEFINED#
        $monitorExtensions = #UNDEFINED#
        $options = (default value) array (
)
    )
) in /api/vendor/php-di/php-di/src/Definition/Exception/InvalidDefinition.php:19
Stack trace:
#0 /api/vendor/php-di/php-di/src/Definition/Resolver/ObjectCreator.php(156): DI\Definition\Exception\InvalidDefinition::create(Object(DI\Definition\ObjectDefinition), 'Entry "process\\...')
#1 /api/vendor/php-di/php-di/src/Definition/Resolver/ObjectCreator.php(71): DI\Definition\Resolver\ObjectCreator->createInstance(Object(DI\Definition\ObjectDefinition), Array)
#2 /api/vendor/php-di/php-di/src/Definition/Resolver/ResolverDispatcher.php(71): DI\Definition\Resolver\ObjectCreator->resolve(Object(DI\Definition\ObjectDefinition), Array)
#3 /api/vendor/php-di/php-di/src/Container.php(390): DI\Definition\Resolver\ResolverDispatcher->resolve(Object(DI\Definition\ObjectDefinition), Array)
#4 /api/vendor/php-di/php-di/src/Container.php(199): DI\Container->resolveDefinition(Object(DI\Definition\ObjectDefinition), Array)
#5 /api/vendor/workerman/webman-framework/src/support/Container.php(46): DI\Container->make('process\\Monitor', Array)
#6 /api/support/helpers.php(468): support\Container::__callStatic('make', Array)
#7 /api/vendor/workerman/workerman/Worker.php(2488): {closure}(Object(Workerman\Worker))
#8 /api/vendor/workerman/workerman/Worker.php(1627): Workerman\Worker->run()
#9 /api/vendor/workerman/workerman/Worker.php(1423): Workerman\Worker::forkOneWorkerForLinux(Object(Workerman\Worker))
#10 /api/vendor/workerman/workerman/Worker.php(1397): Workerman\Worker::forkWorkersForLinux()
#11 /api/vendor/workerman/workerman/Worker.php(560): Workerman\Worker::forkWorkers()
#12 /api/vendor/workerman/webman-framework/src/support/App.php(131): Workerman\Worker::runAll()
#13 /api/start.php(8): support\App::run()
#14 {main}
worker[monitor:95878] exit with status 64000

截图

截图报错信息里报错文件相关代码

1261 1 0
1个回答

9527

经过观察,把process\Monitor的constructor参数默认值配置到 dependence.php 即可;
如下:

<?php
/**
 * This file is part of webman.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author    walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @link      http://www.workerman.net/
 * @license   http://www.opensource.org/licenses/mit-license.php MIT License
 */

return [
    process\Monitor::class => new process\Monitor(
        array_merge([
            app_path(),
            config_path(),
            base_path() . '/process',
            base_path() . '/support',
            base_path() . '/resource',
            base_path() . '/.env',
        ], glob(base_path() . '/plugin/*/app'), glob(base_path() . '/plugin/*/config'), glob(base_path() . '/plugin/*/api')),
        [
            'php', 'html', 'htm', 'env'
        ],
        [
            'enable_file_monitor' => !\Workerman\Worker::$daemonize && DIRECTORY_SEPARATOR === '/',
            'enable_memory_monitor' => DIRECTORY_SEPARATOR === '/',
        ]
    ),
];

原配置config/process 中monitor节点的配置不需要变更;

  • 暂无评论
年代过于久远,无法发表回答
×
🔝