學習路之TP6 --重寫vendor目錄下的文件
- 一、新建命令文件:
- 二、復制修改:Server.php
- 三、新建服務類:WorkmanService.php
- 四、注冊服務
- 五、運行效果
有需求要重寫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