有需求要重写vendor\topthink\think-worker\src\command\Server.php 以实现修改代码

一、新建命令文件:

app\command\Server.php

php think make:command Server 

二、复制修改:Server.php

复制vendor\topthink\think-worker\src\command\Server.php 内容到app\command\Server.php
注意更改命名空间:namespace app\command;

三、新建服务类:WorkmanService.php

php think make:service WorkmanService

修改继承类:class WorkmanService extends \think\worker\Service

<?php
declare (strict_types = 1);

namespace app\service;

class WorkmanService extends \think\worker\Service
{
    /**
     * 注册服务
     *
     * @return mixed
     */
    public function register()
    {
        parent::register();
    	$this->commands([
            'worker:server'  => '\\app\\command\\Server',
        ]);
    }
}

要被重写的文件:vendor\topthink\think-worker\src\Service.php

namespace think\worker;

use think\Service as BaseService;

class Service extends BaseService
{
    public function register()
    {
        $this->commands([
            'worker'         => '\\think\\worker\\command\\Worker',
            'worker:server'  => '\\think\\worker\\command\\Server',
            'worker:gateway' => '\\think\\worker\\command\\GatewayWorker',
        ]);
    }
}

四、注册服务

app\service.php增加

app\service\WorkmanService::class,

在这里插入图片描述

五、运行效果

php think worker:server
在这里插入图片描述

Logo

一起探索未来云端世界的核心,云原生技术专区带您领略创新、高效和可扩展的云计算解决方案,引领您在数字化时代的成功之路。

更多推荐