初始化本地仓库
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
namespace core\generator\utils;
|
||||
|
||||
/**
|
||||
* 路径解析器
|
||||
* 负责处理文件路径的解析和生成
|
||||
*/
|
||||
class PathResolver
|
||||
{
|
||||
/**
|
||||
* 生成文件路径
|
||||
* @param string $basePath 基础路径
|
||||
* @param string $moduleName 模块名称
|
||||
* @param string $className 类名
|
||||
* @param string $fileType 文件类型
|
||||
* @param string $extension 文件扩展名
|
||||
* @param bool $isPlugin 是否是插件模式
|
||||
* @return string 文件路径
|
||||
*/
|
||||
public function generatePath(string $basePath, string $moduleName, string $className, string $fileType, string $extension = 'php', bool $isPlugin = false): string
|
||||
{
|
||||
// 前端文件类型
|
||||
$frontendFileTypes = ['api', 'api_model', 'view', 'view_schema', 'lang'];
|
||||
|
||||
// 根据文件类型处理模块名格式
|
||||
$processedModuleName = $moduleName;
|
||||
if (in_array($fileType, $frontendFileTypes)) {
|
||||
// 前端使用连字符格式
|
||||
$processedModuleName = str_replace('_', '-', $moduleName);
|
||||
} else {
|
||||
// 后端使用下划线格式
|
||||
$processedModuleName = str_replace('-', '_', $moduleName);
|
||||
}
|
||||
|
||||
// 确保 className 是大驼峰格式(处理下划线分隔的情况)
|
||||
$className = str_replace(' ', '', ucwords(str_replace('_', ' ', $className)));
|
||||
|
||||
// 插件模式下,service 路径不包含 admin 层级
|
||||
$servicePath = $isPlugin ? 'service/' : 'service/admin/';
|
||||
|
||||
$pathMap = [
|
||||
// 后端文件路径映射
|
||||
'controller' => 'controller/' . ($processedModuleName ? $processedModuleName . '/' : '') . $className . 'Controller.' . $extension,
|
||||
'model' => 'model/' . ($processedModuleName ? $processedModuleName . '/' : '') . $className . '.' . $extension,
|
||||
'service' => $servicePath . ($processedModuleName ? $processedModuleName . '/' : '') . $className . 'Service.' . $extension,
|
||||
'dao' => 'dao/' . ($processedModuleName ? $processedModuleName . '/' : '') . $className . 'Dao.' . $extension,
|
||||
'validate' => 'validate/' . ($processedModuleName ? $processedModuleName . '/' : '') . $className . 'Validate.' . $extension,
|
||||
'response' => 'schema/response/' . ($processedModuleName ? $processedModuleName . '/' : '') . $className . 'Response.' . $extension,
|
||||
'request_form' => 'schema/request/' . ($processedModuleName ? $processedModuleName . '/' : '') . $className . 'FormRequest.' . $extension,
|
||||
'request_query' => 'schema/request/' . ($processedModuleName ? $processedModuleName . '/' : '') . $className . 'QueryRequest.' . $extension,
|
||||
|
||||
// 前端文件路径映射
|
||||
'api' => 'api/' . ($processedModuleName ? $processedModuleName . '/' : '') . 'index.ts',
|
||||
'api_model' => 'api/' . ($processedModuleName ? $processedModuleName . '/' : '') . 'types.ts',
|
||||
'view' => 'views/' . ($processedModuleName ? $processedModuleName . '/' : '') . 'index.vue',
|
||||
'view_schema' => 'views/' . ($processedModuleName ? $processedModuleName . '/' : '') . 'schemas/index.tsx',
|
||||
'lang' => 'lang/zh-cn/' . $processedModuleName . '.json',
|
||||
];
|
||||
|
||||
$relativePath = $pathMap[$fileType] ?? $fileType . '/' . $className . '.' . $extension;
|
||||
return $basePath . DS . $relativePath;
|
||||
}
|
||||
|
||||
/**
|
||||
* 确保路径存在
|
||||
* @param string $path 路径
|
||||
* @return void
|
||||
*/
|
||||
public function ensurePathExists(string $path): void
|
||||
{
|
||||
$dirPath = dirname($path);
|
||||
|
||||
if (!is_dir($dirPath)) {
|
||||
mkdir($dirPath, 0777, true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取相对路径
|
||||
* @param string $fullPath 完整路径
|
||||
* @param string $basePath 基础路径
|
||||
* @return string 相对路径
|
||||
*/
|
||||
public function getRelativePath(string $fullPath, string $basePath): string
|
||||
{
|
||||
return str_replace($basePath . DS, '', $fullPath);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
namespace core\generator\utils;
|
||||
|
||||
// 定义目录分隔符常量
|
||||
defined('DS') or define('DS', DIRECTORY_SEPARATOR);
|
||||
|
||||
/**
|
||||
* 模板渲染器
|
||||
* 负责处理模板文件的渲染
|
||||
*/
|
||||
class TemplateRenderer
|
||||
{
|
||||
/**
|
||||
* @var string 模板目录路径
|
||||
*/
|
||||
private string $templateDir;
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->templateDir = base_path() . DS . 'core' . DS . 'generator' . DS . 'stubs';
|
||||
}
|
||||
|
||||
/**
|
||||
* 渲染模板
|
||||
*
|
||||
* @param string $templatePath 模板路径
|
||||
* @param array $data 模板数据
|
||||
*
|
||||
* @return string 渲染后的内容
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function render(string $templatePath, array $data): string
|
||||
{
|
||||
$templateFile = $this->templateDir . DS . $templatePath;
|
||||
|
||||
if (!file_exists($templateFile)) {
|
||||
throw new \Exception('Template file not found: ' . $templateFile);
|
||||
}
|
||||
|
||||
// 读取模板内容
|
||||
$content = file_get_contents($templateFile);
|
||||
|
||||
// 替换变量
|
||||
foreach ($data as $key => $value) {
|
||||
if (is_bool($value)) {
|
||||
// 对于布尔值,转换为 TypeScript 布尔字面量
|
||||
$boolValue = $value ? 'true' : 'false';
|
||||
// 支持 {key} 格式的占位符
|
||||
$content = str_replace('{' . $key . '}', $boolValue, $content);
|
||||
// 支持 {$key} 格式的占位符
|
||||
$content = str_replace('{$' . $key . '}', $boolValue, $content);
|
||||
} elseif (is_scalar($value)) {
|
||||
// 支持 {key} 格式的占位符
|
||||
$content = str_replace('{' . $key . '}', $value, $content);
|
||||
// 支持 {$key} 格式的占位符
|
||||
$content = str_replace('{$' . $key . '}', $value, $content);
|
||||
} elseif (is_array($value)) {
|
||||
// 处理数组类型的数据,转换为 TypeScript 对象数组
|
||||
$arrayString = $this->arrayToTypeScript($value);
|
||||
$content = str_replace('{{' . $key . '}}', $arrayString, $content);
|
||||
}
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 PHP 数组转换为 TypeScript 对象数组字符串
|
||||
* @param array $array PHP 数组
|
||||
* @return string TypeScript 对象数组字符串
|
||||
*/
|
||||
private function arrayToTypeScript(array $array): string
|
||||
{
|
||||
if (empty($array)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$items = [];
|
||||
foreach ($array as $item) {
|
||||
if (is_array($item)) {
|
||||
$items[] = $this->objectToTypeScript($item);
|
||||
}
|
||||
}
|
||||
|
||||
return implode(",\n ", $items);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 PHP 关联数组转换为 TypeScript 对象字符串
|
||||
* @param array $object PHP 关联数组
|
||||
* @return string TypeScript 对象字符串
|
||||
*/
|
||||
private function objectToTypeScript(array $object): string
|
||||
{
|
||||
$properties = [];
|
||||
foreach ($object as $key => $value) {
|
||||
if (is_bool($value)) {
|
||||
// 对于布尔值,转换为 TypeScript 布尔字面量
|
||||
$properties[] = $key . ': ' . ($value ? 'true' : 'false');
|
||||
} elseif (is_string($value)) {
|
||||
$properties[] = $key . ': \'' . addslashes($value) . '\'';
|
||||
} elseif (is_numeric($value)) {
|
||||
$properties[] = $key . ': ' . $value;
|
||||
} elseif (is_array($value)) {
|
||||
$properties[] = $key . ': ' . $this->objectToTypeScript($value);
|
||||
}
|
||||
}
|
||||
|
||||
return '{' . implode(', ', $properties) . '}';
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查模板是否存在
|
||||
* @param string $templatePath 模板路径
|
||||
* @return bool 是否存在
|
||||
*/
|
||||
public function templateExists(string $templatePath): bool
|
||||
{
|
||||
$templateFile = $this->templateDir . DS . $templatePath;
|
||||
return file_exists($templateFile);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user