学习路之TP6 --重写vendor目录下的文件(服务覆盖command---优点:命令前后一致)
学习路之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