jh-admin/addon/servicer/event/Install.php

54 lines
2.2 KiB
PHP
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// +---------------------------------------------------------------------+
// | NiuCloud | [ WE CAN DO IT JUST NiuCloud ]  |
// +---------------------------------------------------------------------+
// | Copy right 2019-2029 www.niucloud.com  |
// +---------------------------------------------------------------------+
// | Author | NiuCloud <niucloud@outlook.com>  |
// +---------------------------------------------------------------------+
// | Repository | https://github.com/niucloud/framework.git  |
// +---------------------------------------------------------------------+
declare(strict_types=1);
namespace addon\servicer\event;
/**
* 应用安装
*/
class Install
{
/**
* 执行安装
*/
public function handle()
{
$source_file = './' . ADDON_PATH . 'servicer/' . 'config/source/Events.php';
$target_file = './' . ADDON_PATH . 'servicer/gateway/Applications/Service/Events.php';
// 读取配置文件
$fp = fopen($source_file, 'r');
$config = fread($fp, filesize($source_file));
fclose($fp);
// 替换内容
$database_config = config('database');
$config = str_replace('model_hostname', $database_config['connections']['mysql']['hostname'], $config);
$config = str_replace('model_database', $database_config['connections']['mysql']['database'], $config);
$config = str_replace('model_username', $database_config['connections']['mysql']['username'], $config);
$config = str_replace('model_password', $database_config['connections']['mysql']['password'], $config);
$config = str_replace('model_port', $database_config['connections']['mysql']['hostport'], $config);
$config = str_replace('model_prefix', $database_config['connections']['mysql']['prefix'], $config);
// 检测文件是否可写
$fp = fopen($target_file, 'w');
if ($fp == false) {
return error(-1, '写入配置失败,请检查' . $target_file . '是否可写入!');
}
fwrite($fp, $config);
fclose($fp);
return success();
}
}