修改登陆事件
This commit is contained in:
140
extend/catcher/generate/factory/Controller.php
Normal file
140
extend/catcher/generate/factory/Controller.php
Normal file
@@ -0,0 +1,140 @@
|
||||
<?php
|
||||
namespace JaguarJack\Generator\Factory;
|
||||
|
||||
use catcher\exceptions\FailedException;
|
||||
use JaguarJack\Generator\Template\Controller as Template;
|
||||
use think\helper\Str;
|
||||
|
||||
class Controller extends Factory
|
||||
{
|
||||
protected $methods = [];
|
||||
|
||||
/**
|
||||
*
|
||||
* @time 2020年04月27日
|
||||
* @param $params
|
||||
* @return bool|string|string[]
|
||||
*/
|
||||
public function done($params)
|
||||
{
|
||||
if (!$params['controller']) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$template = new Template();
|
||||
// parse controller
|
||||
[$className, $namespace] = $this->parseFilename($params['controller']);
|
||||
if (!$className) {
|
||||
throw new FailedException('未填写控制器名称');
|
||||
}
|
||||
|
||||
// parse model
|
||||
[$model, $modelNamespace] = $this->parseFilename($params['model']);
|
||||
$content = $template->header() .
|
||||
$template->nameSpace($namespace) .
|
||||
str_replace('{USE}', $model ? 'use ' . $params['model'] .';' : '', $template->uses()) .
|
||||
$template->createClass($className);
|
||||
|
||||
|
||||
$content = str_replace('{CONTENT}', ($model ? $template->construct($model) : '') . rtrim($this->content($params, $template), "\r\n"), $content);
|
||||
|
||||
// 写入成功之后
|
||||
if (file_put_contents($this->getGeneratePath($params['controller']), $content)) {
|
||||
(new Route())->controller($params['controller'])
|
||||
->restful($params['restful'])
|
||||
->methods($this->parseOtherMethods($params['other_function']))
|
||||
->done();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* content
|
||||
*
|
||||
* @time 2020年04月27日
|
||||
* @param $params
|
||||
* @param $template
|
||||
* @return string
|
||||
*/
|
||||
protected function content($params, $template)
|
||||
{
|
||||
$content = '';
|
||||
|
||||
if ($params['restful']) {
|
||||
$methods = $this->restful();
|
||||
$this->methods = array_merge($this->methods, $methods);
|
||||
foreach ($methods as $method) {
|
||||
$content .= $template->{$method[0]}();
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($params['other_function'])) {
|
||||
$others = $this->parseOtherMethods($params['other_function']);
|
||||
$this->methods = array_merge($this->methods, $others);
|
||||
foreach ($others as $other) {
|
||||
$content .= $template->otherFunction($other[0], $other[1]);
|
||||
}
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
/**
|
||||
* parse filename
|
||||
*
|
||||
* @time 2020年04月27日
|
||||
* @param $filename
|
||||
* @return array
|
||||
*/
|
||||
public function parseFilename($filename)
|
||||
{
|
||||
$namespace = explode('\\', $filename);
|
||||
|
||||
$className = ucfirst(array_pop($namespace));
|
||||
|
||||
$namespace = implode('\\', $namespace);
|
||||
|
||||
return [$className, $namespace];
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* parse $method
|
||||
* class_method/http_method
|
||||
* @time 2020年04月27日
|
||||
* @param $methods
|
||||
* @return false|string[]
|
||||
*/
|
||||
public function parseOtherMethods($methods)
|
||||
{
|
||||
$_methods = [];
|
||||
|
||||
foreach ($methods as $method) {
|
||||
if (Str::contains($method, '/')) {
|
||||
$_methods[] = explode('/', $method);
|
||||
} else {
|
||||
// 默认使用 Get 方式
|
||||
$_methods[] = [$method, 'get'];
|
||||
}
|
||||
}
|
||||
|
||||
return $_methods;
|
||||
}
|
||||
|
||||
/**
|
||||
* restful 路由
|
||||
*
|
||||
* @time 2020年04月27日
|
||||
* @return \string[][]
|
||||
*/
|
||||
public function restful()
|
||||
{
|
||||
return [
|
||||
['index', 'get'],
|
||||
['save', 'post'],
|
||||
['read', 'get'],
|
||||
['update', 'put'],
|
||||
['delete', 'delete'],
|
||||
];
|
||||
}
|
||||
|
||||
}
|
66
extend/catcher/generate/factory/Factory.php
Normal file
66
extend/catcher/generate/factory/Factory.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
namespace JaguarJack\Generator\Factory;
|
||||
|
||||
use catcher\CatchAdmin;
|
||||
|
||||
abstract class Factory
|
||||
{
|
||||
abstract public function done($param);
|
||||
|
||||
/**
|
||||
* parse psr4 path
|
||||
*
|
||||
* @time 2020年04月27日
|
||||
* @return mixed
|
||||
*/
|
||||
public function parsePsr4()
|
||||
{
|
||||
$composer = \json_decode(file_get_contents(root_path() . 'composer.json'), true);
|
||||
|
||||
return $composer['autoload']['psr-4'];
|
||||
}
|
||||
|
||||
/**
|
||||
* get generate path
|
||||
*
|
||||
* @time 2020年04月27日
|
||||
* @param $filePath
|
||||
* @return string
|
||||
*/
|
||||
protected function getGeneratePath($filePath)
|
||||
{
|
||||
$path = explode('\\', $filePath);
|
||||
|
||||
$projectRootNamespace = array_shift($path);
|
||||
|
||||
$filename = array_pop($path);
|
||||
|
||||
$psr4 = $this->parsePsr4();
|
||||
|
||||
$filePath = root_path() . $psr4[$projectRootNamespace.'\\'] . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $path);
|
||||
|
||||
CatchAdmin::makeDirectory($filePath);
|
||||
|
||||
return $filePath . DIRECTORY_SEPARATOR . $filename . '.php';
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取模块地址
|
||||
*
|
||||
* @time 2020年04月28日
|
||||
* @param $filePath
|
||||
* @return string
|
||||
*/
|
||||
public function getModulePath($filePath)
|
||||
{
|
||||
$path = explode('\\', $filePath);
|
||||
|
||||
$projectRootNamespace = array_shift($path);
|
||||
|
||||
$module = array_shift($path);
|
||||
|
||||
$psr4 = $this->parsePsr4();
|
||||
|
||||
return root_path() . $psr4[$projectRootNamespace.'\\'] . DIRECTORY_SEPARATOR. $module . DIRECTORY_SEPARATOR;
|
||||
}
|
||||
}
|
1
extend/catcher/generate/factory/Model.php
Normal file
1
extend/catcher/generate/factory/Model.php
Normal file
@@ -0,0 +1 @@
|
||||
<?php
|
1
extend/catcher/generate/factory/Request.php
Normal file
1
extend/catcher/generate/factory/Request.php
Normal file
@@ -0,0 +1 @@
|
||||
<?php
|
1
extend/catcher/generate/factory/Respository.php
Normal file
1
extend/catcher/generate/factory/Respository.php
Normal file
@@ -0,0 +1 @@
|
||||
<?php
|
81
extend/catcher/generate/factory/Route.php
Normal file
81
extend/catcher/generate/factory/Route.php
Normal file
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
namespace JaguarJack\Generator\Factory;
|
||||
|
||||
use JaguarJack\Generator\Template\Content;
|
||||
|
||||
class Route extends Factory
|
||||
{
|
||||
use Content;
|
||||
|
||||
protected $controllerName;
|
||||
|
||||
protected $controller;
|
||||
|
||||
protected $restful;
|
||||
|
||||
protected $methods = [];
|
||||
|
||||
public function done($params = [])
|
||||
{
|
||||
$route = [];
|
||||
|
||||
if ($this->restful) {
|
||||
$route[] = sprintf("\$router->resource('%s', '\%s')", $this->controllerName, $this->controller);
|
||||
}
|
||||
|
||||
if (!empty($this->methods)) {
|
||||
foreach ($this->methods as $method) {
|
||||
$route[] = sprintf("\$router->%s('%s/%s', '\%s@%s')", $method[1], $this->controllerName, $method[0], $this->controller, $method[0] );
|
||||
}
|
||||
}
|
||||
|
||||
return file_put_contents($this->getModulePath($this->controller) . DIRECTORY_SEPARATOR . 'route.php',
|
||||
$this->header() . implode(';'. PHP_EOL , $route) . ';');
|
||||
}
|
||||
|
||||
/**
|
||||
* set class
|
||||
*
|
||||
* @time 2020年04月28日
|
||||
* @param $class
|
||||
* @return $this
|
||||
*/
|
||||
public function controller($class)
|
||||
{
|
||||
$this->controller = $class;
|
||||
|
||||
$class = explode('\\', $class);
|
||||
|
||||
$this->controllerName = lcfirst(array_pop($class));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* set restful
|
||||
*
|
||||
* @time 2020年04月28日
|
||||
* @param $restful
|
||||
* @return $this
|
||||
*/
|
||||
public function restful($restful)
|
||||
{
|
||||
$this->restful = $restful;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* set methods
|
||||
*
|
||||
* @time 2020年04月28日
|
||||
* @param $methods
|
||||
* @return $this
|
||||
*/
|
||||
public function methods($methods)
|
||||
{
|
||||
$this->methods = $methods;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
151
extend/catcher/generate/factory/SQL.php
Normal file
151
extend/catcher/generate/factory/SQL.php
Normal file
@@ -0,0 +1,151 @@
|
||||
<?php
|
||||
namespace JaguarJack\Generator\Factory;
|
||||
|
||||
|
||||
class SQL
|
||||
{
|
||||
protected $index = '';
|
||||
|
||||
|
||||
public function done($params)
|
||||
{
|
||||
if ($params['controller']['table'] ?? false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$table = \config('database.connections.mysql.prefix') . $params['controller']['table'];
|
||||
|
||||
$extra = $params['model']['extra'];
|
||||
|
||||
// 主键
|
||||
$createSql = $this->primaryKey($extra['primary_key']);
|
||||
// 字段
|
||||
$ifHaveNotFields = true;
|
||||
foreach ($params['model']['data'] as $sql) {
|
||||
if (!$sql['field'] || !$sql['type']) {
|
||||
continue;
|
||||
}
|
||||
$ifHaveNotFields = false;
|
||||
$createSql .= $this->parseSQL($sql);
|
||||
}
|
||||
// 如果没有设置数据库字段
|
||||
if ($ifHaveNotFields) {
|
||||
return false;
|
||||
}
|
||||
// 创建时间
|
||||
if ($extra['created_at'] ?? false) {
|
||||
$createSql .= $this->parseCreatedAt();
|
||||
}
|
||||
// 软删除
|
||||
if ($extra['soft_delete'] ?? false) {
|
||||
$createSql .= $this->parseDeletedAt();
|
||||
}
|
||||
// 索引
|
||||
if ($this->index) {
|
||||
$createSql .= $this->index;
|
||||
}
|
||||
// 创建表 SQL
|
||||
return $this->createTable($table, $createSql, $extra['engine'], 'utf8mb4', $extra['comment']);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* parse sql
|
||||
*
|
||||
* @time 2020年04月27日
|
||||
* @param $sql
|
||||
* @return string
|
||||
*/
|
||||
protected function parseSQL($sql)
|
||||
{
|
||||
|
||||
// 解析索引
|
||||
if ($sql['index']) {
|
||||
$this->parseIndex($sql['index'], $sql['field']);
|
||||
}
|
||||
|
||||
return implode(' ', [
|
||||
sprintf('`%s`', $sql['field']),
|
||||
$sql['type'],
|
||||
$sql['length'] ? sprintf('(%s)', $sql['length']) : '',
|
||||
$sql['unsigned'] ? 'unsigned' : '',
|
||||
$sql['default'] ? 'default ' . $sql['default']: '',
|
||||
$sql['nullable'] ? 'not null' : '',
|
||||
$sql['comment'] ? sprintf('comment \'%s\'', $sql['comment']) : ''
|
||||
]) . ','. PHP_EOL;
|
||||
}
|
||||
|
||||
/**
|
||||
* parse primary key
|
||||
*
|
||||
* @time 2020年04月27日
|
||||
* @param $id
|
||||
* @return string
|
||||
*/
|
||||
protected function primaryKey($id)
|
||||
{
|
||||
return sprintf('`%s`', $id) . ' int unsigned not null auto_increment primary key,'. PHP_EOL;
|
||||
}
|
||||
|
||||
/**
|
||||
* parse created_at & updated_at
|
||||
*
|
||||
* @time 2020年04月27日
|
||||
* @return string
|
||||
*/
|
||||
protected function parseCreatedAt()
|
||||
{
|
||||
return sprintf('`created_at` int unsigned not null default 0 comment \'%s\',', '创建时间') . PHP_EOL .
|
||||
sprintf('`updated_at` int unsigned not null default 0 comment \'%s\',', '更新时间') . PHP_EOL;
|
||||
}
|
||||
|
||||
/**
|
||||
* parse deleted_at
|
||||
*
|
||||
* @time 2020年04月27日
|
||||
* @return string
|
||||
*/
|
||||
protected function parseDeletedAt()
|
||||
{
|
||||
return sprintf('`deleted_at` int unsigned not null default 0 comment \'%s\',', '软删除') . PHP_EOL;
|
||||
}
|
||||
|
||||
/**
|
||||
* created table
|
||||
*
|
||||
* @time 2020年04月27日
|
||||
* @param $table
|
||||
* @param $sql
|
||||
* @param string $engine
|
||||
* @param string $charset
|
||||
* @param string $comment
|
||||
* @return string
|
||||
*/
|
||||
protected function createTable($table, $sql, $engine='InnoDB', $charset = 'utf8mb4', $comment = '')
|
||||
{
|
||||
return sprintf('create table `%s`(' . PHP_EOL.
|
||||
'%s)'.PHP_EOL .
|
||||
'engine=%s default charset=%s comment=\'%s\'', $table, $sql, $engine, $charset, $comment);
|
||||
}
|
||||
|
||||
/**
|
||||
* parse index
|
||||
*
|
||||
* @time 2020年04月27日
|
||||
* @param $index
|
||||
* @param $field
|
||||
* @return void
|
||||
*/
|
||||
protected function parseIndex($index, $field)
|
||||
{
|
||||
if ($index == 'unique') {
|
||||
$this->index .= "unique index unique_ . $field($field)";
|
||||
} elseif ($index == 'index') {
|
||||
$this->index .= "index($field)";
|
||||
} elseif ($index == 'fulltext') {
|
||||
$this->index .= "fulltext key fulltext_ .$field($field)";
|
||||
} elseif ($index == 'spatial') {
|
||||
$this->index .= "spatial index spatial_.$field($field)";
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user