crud增加默认存放目录

This commit is contained in:
liaofei 2023-04-04 17:34:19 +08:00
parent 127bf916dd
commit c26b48f928
12 changed files with 279 additions and 62 deletions

View File

@ -45,26 +45,30 @@ class AdminApiExceptionHandle extends Handle
public function report(Throwable $exception): void
{
if (!$this->isIgnoreReport($exception)) {
$data = [
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'message' => $this->getMessage($exception),
'code' => $this->getCode($exception),
];
try {
$data = [
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'message' => $this->getMessage($exception),
'code' => $this->getCode($exception),
];
//日志内容
$log = [
request()->adminId(), //管理员ID
request()->ip(), //客户ip
ceil(msectime() - (request()->time(true) * 1000)), //耗时(毫秒)
request()->rule()->getMethod(), //请求类型
str_replace("/", "", request()->rootUrl()), //应用
request()->baseUrl(), //路由
json_encode(request()->param(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),//请求参数
json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), //报错数据
//日志内容
$log = [
request()->adminId(), //管理员ID
request()->ip(), //客户ip
ceil(msectime() - (request()->time(true) * 1000)), //耗时(毫秒)
request()->rule()->getMethod(), //请求类型
str_replace("/", "", request()->rootUrl()), //应用
request()->baseUrl(), //路由
json_encode(request()->param(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),//请求参数
json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), //报错数据
];
Log::write(implode("|", $log), "error");
];
Log::write(implode("|", $log), "error");
} catch (\Throwable $e) {
Log::write($e->getMessage(), "error");
}
}
}

View File

@ -0,0 +1,35 @@
<?php
/**
* +----------------------------------------------------------------------
* | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
* +----------------------------------------------------------------------
* | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
* +----------------------------------------------------------------------
* | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
* +----------------------------------------------------------------------
* | Author: CRMEB Team <admin@crmeb.com>
* +----------------------------------------------------------------------
*/
use think\facade\Route;
/**
* crud 自动加载路由
* 自动加载crud目录下的所有路由文件
*/
Route::group(function () {
$path = $this->app->getRootPath() . 'app' . DS . 'adminapi' . DS . 'route' . DS . 'crud';
if (is_dir($path)) {
$files = scandir($path);
foreach ($files as $file) {
if ($file != '.' && $file != '..') {
include $path . DS . $file;
}
}
}
})->middleware([
\app\http\middleware\AllowOriginMiddleware::class,
\app\adminapi\middleware\AdminAuthTokenMiddleware::class,
\app\adminapi\middleware\AdminCheckRoleMiddleware::class,
\app\adminapi\middleware\AdminLogMiddleware::class
]);

View File

@ -32,6 +32,17 @@ class Controller extends Make
*/
protected $name = 'controller';
/**
* @return string
* @author 等风来
* @email 136327134@qq.com
* @date 2023/4/4
*/
protected function setBaseDir(): string
{
return 'app' . DS . 'adminapi' . DS . 'controller' . DS . 'crud';
}
/**
* @return mixed|void
* @author 等风来
@ -87,7 +98,7 @@ class Controller extends Make
$filePath = $this->getFilePathName($path, $this->value['nameCamel']);
return $this->makeFile($filePath, $contentStr);
return [$this->makeFile($filePath, $contentStr), $filePath];
}
/**
@ -100,7 +111,7 @@ class Controller extends Make
*/
protected function getStub(string $type = '')
{
$controllerPath = __DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'controller' . DIRECTORY_SEPARATOR;
$controllerPath = __DIR__ . DS . 'stubs' . DS . 'controller' . DS;
$stubs = [
'index' => $controllerPath . 'index.stub',
@ -129,6 +140,6 @@ class Controller extends Make
$path = ltrim(str_replace('\\', '/', $path), '/');
return $this->app->getBasePath() . $path . DIRECTORY_SEPARATOR . $name . '.' . $this->fileMime;
return $this->getBasePath($path) . $name . '.' . $this->fileMime;
}
}

View File

@ -23,6 +23,17 @@ class Dao extends Make
*/
protected $name = "dao";
/**
* @return string
* @author 等风来
* @email 136327134@qq.com
* @date 2023/4/4
*/
protected function setBaseDir(): string
{
return 'app' . DS . 'dao' . DS . 'crud';
}
/**
* 模板文件
* @param string $type
@ -33,6 +44,6 @@ class Dao extends Make
*/
protected function getStub(string $type = '')
{
return __DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'dao' . DIRECTORY_SEPARATOR . 'CrudDao.stub';
return __DIR__ . DS . 'stubs' . DS . 'dao' . DS . 'CrudDao.stub';
}
}

View File

@ -29,7 +29,7 @@ abstract class Make
{
/**
* 命令名称
* 名称
* @var string
*/
protected $name = '';
@ -64,10 +64,17 @@ abstract class Make
protected $adminTemplatePath;
/**
* 默认保存路径
* @var string
*/
protected $basePath;
/**
* 默认文件夹
* @var string
*/
protected $baseDir;
/**
* @var
*/
@ -83,11 +90,39 @@ abstract class Make
$this->app = $app;
$this->adminTemplatePath = dirname($this->app->getRootPath()) . DS . 'template' . DS . 'admin' . DS . 'src' . DS;
$this->basePath = $this->app->getRootPath();
$this->baseDir = $this->setBaseDir();
$this->authDrawVar();
$this->drawValueKeys();
$this->setDefaultValue();
}
/**
* @author 等风来
* @email 136327134@qq.com
* @date 2023/4/4
*/
protected function setBaseDir(): string
{
return 'crud';
}
/**
* @param string $path
* @return string
* @author 等风来
* @email 136327134@qq.com
* @date 2023/4/4
*/
protected function getBasePath(string $path = '')
{
//替换成本地路径格式
$path = str_replace('/', DS, $path);
//替换掉和基础目录相同的
$path = str_replace($this->baseDir, '', $path);
//多个斜杠的替换成一个
return str_replace(DS . DS, DS, $this->basePath . $this->baseDir . DS . ($path ? $path . DS : ''));
}
/**
* @param int $num
* @return string
@ -127,7 +162,7 @@ abstract class Make
[$nameData, $content] = $this->getStubContent($name);
$this->value['name'] = $nameData;
if (isset($this->value['nameCamel'])) {
if (isset($this->value['nameCamel']) && !$this->value['nameCamel']) {
$this->value['nameCamel'] = Str::studly($name);
}
if (isset($this->value['path'])) {
@ -138,7 +173,7 @@ abstract class Make
$filePath = $this->getFilePathName($path, $this->value['nameCamel']);
return $this->makeFile($filePath, $contentStr);
return [$this->makeFile($filePath, $contentStr), $filePath];
}
/**
@ -234,7 +269,7 @@ abstract class Make
$path = ltrim(str_replace('\\', '/', $path), '/');
return $this->basePath . $path . DIRECTORY_SEPARATOR . $name . ucwords($this->name) . '.' . $this->fileMime;
return $this->getBasePath($path) . $name . ucwords($this->name) . '.' . $this->fileMime;
}
/**
@ -326,13 +361,18 @@ abstract class Make
throw new CrudException($this->name . ':' . $pathname . ' already exists!');
}
if (!is_dir(dirname($pathname))) {
mkdir(dirname($pathname), 0755, true);
}
$content = str_replace('', '', $content);
if ($this->isMake) {
try {
if (!is_dir(dirname($pathname))) {
mkdir(dirname($pathname), 0755, true);
}
} catch (\Throwable $e) {
throw new CrudException('CRUD创建目录报错,无法创建:' . dirname($pathname));
}
try {
file_put_contents($pathname, $content);
} catch (\Throwable $e) {

View File

@ -29,6 +29,17 @@ class Model extends Make
*/
protected $name = "model";
/**
* @return string
* @author 等风来
* @email 136327134@qq.com
* @date 2023/4/4
*/
protected function setBaseDir(): string
{
return 'app' . DS . 'model' . DS . 'crud';
}
/**
* 模板文件
* @param string $type
@ -39,6 +50,6 @@ class Model extends Make
*/
protected function getStub(string $type = '')
{
return __DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'model' . DIRECTORY_SEPARATOR . 'CrudModel.stub';
return __DIR__ . DS . 'stubs' . DS . 'model' . DS . 'CrudModel.stub';
}
}

View File

@ -23,6 +23,17 @@ class Route extends Make
*/
protected $name = 'route';
/**
* @return string
* @author 等风来
* @email 136327134@qq.com
* @date 2023/4/4
*/
protected function setBaseDir(): string
{
return 'app' . DS . 'adminapi' . DS . 'route' . DS . 'crud';
}
/**
* @param string $name
* @param string $path
@ -71,7 +82,7 @@ class Route extends Make
$filePath = $this->getFilePathName($path, $this->value['nameCamel']);
return $this->makeFile($filePath, $contentStr);
return [$this->makeFile($filePath, $contentStr), $filePath];
}
/**
@ -84,7 +95,7 @@ class Route extends Make
*/
protected function getStub(string $type = 'route')
{
$routePath = __DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'route' . DIRECTORY_SEPARATOR;
$routePath = __DIR__ . DS . 'stubs' . DS . 'route' . DS;
$stubs = [
'index' => $routePath . 'index.stub',

View File

@ -23,6 +23,17 @@ class Service extends Make
*/
protected $name = "services";
/**
* @return string
* @author 等风来
* @email 136327134@qq.com
* @date 2023/4/4
*/
protected function setBaseDir(): string
{
return 'app' . DS . 'services' . DS . 'crud';
}
/**
* @param string $name
* @param string $path
@ -81,7 +92,7 @@ class Service extends Make
$filePath = $this->getFilePathName($path, $this->value['nameCamel']);
return $this->makeFile($filePath, $contentStr);
return [$this->makeFile($filePath, $contentStr), $filePath];
}
/**
@ -129,7 +140,7 @@ class Service extends Make
*/
protected function getStub(string $type = 'services')
{
$servicePath = __DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'service' . DIRECTORY_SEPARATOR;
$servicePath = __DIR__ . DS . 'stubs' . DS . 'service' . DS;
$stubs = [
'index' => $servicePath . 'CrudListIndex.stub',

View File

@ -27,6 +27,17 @@ class Validate extends Make
*/
protected $name = 'validate';
/**
* @return string
* @author 等风来
* @email 136327134@qq.com
* @date 2023/4/4
*/
protected function setBaseDir(): string
{
return 'app' . DS . 'adminapi' . DS . 'validate' . DS . 'crud';
}
/**
* 模板文件配置
* @param string $type
@ -34,6 +45,6 @@ class Validate extends Make
*/
protected function getStub(string $type = '')
{
return __DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'validate' . DIRECTORY_SEPARATOR . 'CrudValidate.stub';
return __DIR__ . DS . 'stubs' . DS . 'validate' . DS . 'CrudValidate.stub';
}
}

View File

@ -31,6 +31,26 @@ class ViewApi extends Make
*/
protected $name = 'api';
/**
* @return string
* @author 等风来
* @email 136327134@qq.com
* @date 2023/4/4
*/
protected function setBaseDir(): string
{
return 'api' . DS . 'crud';
}
/**
* @param string $name
* @param string $path
* @param array $options
* @return array
* @author 等风来
* @email 136327134@qq.com
* @date 2023/4/4
*/
public function handle(string $name, string $path, array $options = [])
{
@ -49,9 +69,11 @@ class ViewApi extends Make
$contentJs .= file_get_contents($this->getStub($item)) . "\n";
}
$nameCamel = Str::studly($name);
if ($contentJs) {
$var = ['{%name%}', '{%route%}', '{%nameCamel%}'];
$value = [$name, $route, Str::studly($name)];
$value = [$name, $route, $nameCamel];
$contentJs = str_replace($var, $value, $contentJs);
}
@ -64,7 +86,7 @@ class ViewApi extends Make
$this->basePath = $this->adminTemplatePath;
$this->fileMime = 'js';
$filePath = $this->getFilePathName($path, $this->value['nameCamel']);
$filePath = $this->getFilePathName($path, Str::camel($name));
$content = $this->makeFile($filePath, $contentStr);
@ -72,6 +94,21 @@ class ViewApi extends Make
}
/**
* @param string $path
* @param string $name
* @return string
* @author 等风来
* @email 136327134@qq.com
* @date 2023/4/4
*/
protected function getFilePathName(string $path, string $name): string
{
$path = ltrim(str_replace('\\', '/', $path), '/');
return $this->getBasePath($path) . $name . '.' . $this->fileMime;
}
/**
* 模板文件配置
* @param string $type
@ -79,15 +116,15 @@ class ViewApi extends Make
*/
protected function getStub(string $type = 'api')
{
$servicePath = __DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'view' . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR;
$servicePath = __DIR__ . DS . 'stubs' . DS . 'view' . DS . 'api' . DS;
$stubs = [
'index' => $servicePath . 'getCrudListApi.stub',
'create' => $servicePath . 'CrudUpdate.stub',
'save' => $servicePath . 'CrudSave.stub',
'edit' => $servicePath . 'GetCrudForm.stub',
'update' => $servicePath . 'CrudUpdate.stub',
'api' => $servicePath . 'CrudService.stub',
'create' => $servicePath . 'crudUpdateApi.stub',
'save' => $servicePath . 'crudSaveApi.stub',
'edit' => $servicePath . 'getCrudEditApi.stub',
'update' => $servicePath . 'CrudUpdateApi.stub',
'api' => $servicePath . 'crud.stub',
];
return $type ? $stubs[$type] : $stubs;

View File

@ -30,6 +30,17 @@ class ViewPages extends Make
*/
protected $name = 'pages';
/**
* @return string
* @author 等风来
* @email 136327134@qq.com
* @date 2023/4/4
*/
protected function setBaseDir(): string
{
return 'pages' . DS . 'crud';
}
/**
* @param string $name
* @param string $path
@ -50,11 +61,26 @@ class ViewPages extends Make
$this->value['auth'] = Str::snake($name);
$this->value['content-vue'] = "\n" . implode(',', $columnStr);
$this->value['pathApiJs'] = $options['pathApiJs'] ?? '';
$this->basePath = $this->adminTemplatePath;
$this->value['nameCamel'] = Str::snake($name, '-');
$this->basePath = $this->adminTemplatePath . 'pages';
$this->fileMime = 'vue';
return parent::handle($name, $path, $options); // TODO: Change the autogenerated stub
}
/**
* @param string $path
* @param string $name
* @return string
* @author 等风来
* @email 136327134@qq.com
* @date 2023/4/4
*/
protected function getFilePathName(string $path, string $name): string
{
$path = ltrim(str_replace('\\', '/', $path), '/');
return $this->getBasePath($path) . $name . '.' . $this->fileMime;
}
/**
* @param string $type
* @return string
@ -64,8 +90,8 @@ class ViewPages extends Make
*/
protected function getStub(string $type = '')
{
return __DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'view' .
DIRECTORY_SEPARATOR . 'pages' . DIRECTORY_SEPARATOR . 'crud' .
DIRECTORY_SEPARATOR . 'index.stub';
return __DIR__ . DS . 'stubs' . DS . 'view' .
DS . 'pages' . DS . 'crud' .
DS . 'index.stub';
}
}

View File

@ -30,10 +30,29 @@ class ViewRouter extends Make
*/
protected $name = 'router';
/**
* @return string
* @author 等风来
* @email 136327134@qq.com
* @date 2023/4/4
*/
protected function setBaseDir(): string
{
return 'router' . DS . 'modules' . DS . 'crud';
}
/**
* @param string $name
* @param string $path
* @param array $options
* @return false|mixed|void
* @author 等风来
* @email 136327134@qq.com
* @date 2023/4/4
*/
public function handle(string $name, string $path, array $options = [])
{
$this->basePath = $this->adminTemplatePath;
$this->basePath = $this->adminTemplatePath . 'router' . DS . 'modules' . DS . 'crud';
$this->fileMime = 'js';
[$nameData, $content] = $this->getStubContent($name);
@ -58,12 +77,7 @@ class ViewRouter extends Make
$makeContent = $this->makeFile($filePath, $contentStr);
dump($filePath);
$pathInfo = pathinfo($filePath);
dump($pathInfo);
$router = '';
return $makeContent;
return [$makeContent, $filePath];
}
/**
@ -76,11 +90,8 @@ class ViewRouter extends Make
*/
protected function getFilePathName(string $path, string $name): string
{
$path = str_replace(['app\\', 'app/'], '', $path);
$path = ltrim(str_replace('\\', '/', $path), '/');
return $this->basePath . $path . DIRECTORY_SEPARATOR . $name . '.' . $this->fileMime;
return $this->getBasePath($path) . $name . '.' . $this->fileMime;
}
/**
@ -92,8 +103,6 @@ class ViewRouter extends Make
*/
protected function getStub(string $type = '')
{
return __DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'view' .
DIRECTORY_SEPARATOR . 'router' . DIRECTORY_SEPARATOR . 'modules' .
DIRECTORY_SEPARATOR . 'crud.stub';
return __DIR__ . DS . 'stubs' . DS . 'view' . DS . 'router' . DS . 'modules' . DS . 'crud.stub';
}
}