版权声明:转载请注明来源 https://blog.csdn.net/u013702678/article/details/82721804
虽然这篇文章的标题是TcpServer的解析,但是从解析第一篇文件可以知道,TcpServer的很多方法都是继承自BaseServer,所以我们重点介绍BaseServer中的方法。
//加载app root目录下的bootstrap.php和provider/$name.provider文件
//setRequire的参数file就是定义在应用.deploy配置文件中的root参数所指的信息,类似提供的demo中其定义为../example/provider/src/BootStrap.php,也就是应用自身加载相关的Bootstrap文件。
public function setRequire($file)
{
$this->rootFile = $file;
$this->requireFile = $this->rootFile;
$this->logger->info("bootstrapFile = {$this->requireFile}");
if (!file_exists($this->requireFile))//判断文件是否存在
{
$this->logger->error("bootstrapFile :{$this->requireFile} is not exists");
exit(1);
}
//加载appname.provider,也就是用于定义Provider对外接口的文件信息
$appConfigPath = dirname($this->requireFile).DIRECTORY_SEPARATOR.'provider'.DIRECTORY_SEPARATOR.$this->processName.'.provider';
if(!file_exists($appConfigPath))//判断文件是否存在
{
$this->logger->error("provider file :{$appConfigPath} is not exists");
exit(1);
}
//加载应用provider文件信息
$this->loadConfig($appConfigPath);
$this->logger->info("provider file :{$appConfigPath}");
}
下面的loadConfig用于加载各种配置文件信息,其入参为配置文件路径,之前文章中提到的配置文件都是通过loadConfig来加载,调用逻辑如下:
//app/conf目录下的$name.deploy文件
$server->loadConfig($config);
//全局fsof.ini文件
$server->loadConfig(FSOFConfigManager::getFSOFIni());
public function loadConfig($config = array())
{
if (is_string($config))
{
if (! file_exists($config))//判断文件是否存在,其实在外面已经判断过一次了
{
$this->logger->error("profiles {$config} can not be loaded");
exit(1);
}
// Load the configuration file into an array
$config = parse_ini_file($config, true);//调用PHP的系统函数解析文件,这个Provider文件其实是ini文件,可以直接用parse_ini_file来解析
}
if (is_array($config))//应用的provider文件包含多个参数信息,解析完应该是个数组
{
$this->config = array_merge($this->config, $config);//Provider文件的配置信息合并到类属性config中。
}
return true;
}
应用的provider文件位于Provider应用目录下,类似框架提供的demo中,provider文件路径为:dubbo-php-framework-master/demo/demo-provider/provider/demo-provider.provider,内容如下所示:
[service_properties]
owner = author
version = 1.0.0
group = default
[service_providers]
com.fenqile.example.DemoService[service] = DemoServiceImpl
com.fenqile.example.DemoService[version] = 1.0.0
com.fenqile.example.DemoService[group] = default
//设置swoole的日志文件路径信息
public function setSwooleLogFile($cmd)
{
if($cmd == 'start' || $cmd == 'restart' || $cmd == 'extstart')
{
$this->logger->info($this->processName.'.deploy:{'
."\"server\":".json_encode($this->config['server']) .','
."\"setting\":".json_encode($this->config['setting']).'}');
$this->logger->info('fsof.ini:{'."\"fsof_setting\":".json_encode($this->config['fsof_setting']).'}');
}
if(isset($this->config['server']['swoole_log_path']) && !empty($this->config['server']['swoole_log_path'])){
$this->setting['log_file'] = $this->config['server']['swoole_log_path'];
}else{
$this->setting['log_file'] = '/var/fsof/provider/'.$this->processName.'_swoole.log';
}
}