This commit is contained in:
2021-03-25 18:28:41 +08:00
parent e89ce9f2f1
commit 823c1ded50
4 changed files with 140 additions and 103 deletions
+94 -1
View File
@@ -6,9 +6,11 @@ namespace Gii;
use Database\Connection; use Database\Connection;
use Exception;
use JetBrains\PhpStorm\ArrayShape; use JetBrains\PhpStorm\ArrayShape;
use ReflectionClass; use ReflectionClass;
use ReflectionException;
use Snowflake\Abstracts\Input; use Snowflake\Abstracts\Input;
/** /**
@@ -149,6 +151,97 @@ abstract class GiiBase
} }
/**
* @param ReflectionClass $class
* @return string
* @throws ReflectionException
*/
protected function getClassProperty(ReflectionClass $class): string
{
$html = '';
foreach ($class->getConstants() as $key => $val) {
if (is_numeric($val)) {
$html .= '
const ' . $key . ' = ' . $val . ';' . "\n";
} else {
$html .= '
const ' . $key . ' = \'' . $val . '\';' . "\n";
}
}
foreach ($class->getDefaultProperties() as $key => $val) {
$property = $class->getProperty($key);
if ($property->class != $class->getName()) continue;
if (is_array($val)) {
$val = '[\'' . implode('\', \'', $val) . '\']';
} else if (!is_numeric($val)) {
$val = '\'' . $val . '\'';
}
if ($property->isProtected()) {
$debug = 'protected';
} else if ($property->isPrivate()) {
$debug = 'private';
} else {
$debug = 'public';
}
if ($property->hasType()) {
$type = ' ' . $property->getType() . ' $' . $key . ' = ' . $val . ';' . "\n";
} else {
$type = ' $' . $key . ' = ' . $val . ';' . "\n";
}
if ($property->isStatic()) {
$html .= '
' . $debug . ' static' . $type;
} else {
if ($key == 'primary') {
continue;
}
$html .= '
' . $debug . $type;
}
}
return $html;
}
/**
* @param ReflectionClass $class
* @param array $filters
* @return string
* @throws Exception
*/
protected function getClassMethods(ReflectionClass $class, array $filters = []): string
{
$methods = $class->getMethods();
$classFileName = str_replace(APP_PATH, '', $class->getFileName());
$content = [];
if (!empty($methods)) foreach ($methods as $key => $val) {
if ($val->class != $class->getName()) continue;
if (in_array($val->name, $filters)) continue;
$over = "
" . $val->getDocComment() . "\n";
$attributes = $val->getAttributes();
if (!empty($attributes)) {
foreach ($attributes as $attribute) {
$explode = explode('\\', $attribute->getName());
$over .= " #[" . end($explode) . "('" . implode('\',\'', $attribute->getArguments()) . "')]
";
}
}
$func = $this->getFuncLineContent($class, $classFileName, $val->name) . "\n";
$content[] = $over . $func;
}
return implode($content);
}
/** /**
* @param $fields * @param $fields
* @return mixed 返回表主键 * @return mixed 返回表主键
@@ -189,7 +282,7 @@ abstract class GiiBase
* @param $className * @param $className
* @param $method * @param $method
* @return string * @return string
* @throws \Exception * @throws Exception
*/ */
public function getFuncLineContent(ReflectionClass $object, $className, $method): string public function getFuncLineContent(ReflectionClass $object, $className, $method): string
{ {
+15 -24
View File
@@ -44,22 +44,17 @@ class GiiController extends GiiBase
$class = ''; $class = '';
$controller = "$namespace\{$managerName}Controller"; $controller = "$namespace\{$managerName}Controller";
if (file_exists($path['path'] . '/' . $managerName . 'Controller.php')) {
$class = Snowflake::getDi()->getReflect($controller);
}
$controllerName = $managerName; $html = '<?php
$html = $class instanceof \ReflectionClass ? $this->getUseContent($class, $controller) : null;
if (empty($html)) {
$html .= "<?php
namespace {$namespace}; namespace {$namespace};
use Snowflake; ';
if (file_exists($path['path'] . '/' . $managerName . 'Controller.php')) {
$class = Snowflake::getDi()->getReflect($controller);
$import = $this->getImports($path['path'] . '/' . $managerName . 'Controller.php', $class);
}else{
$import = "use Snowflake;
use exception; use exception;
use Annotation\Target; use Annotation\Target;
use Snowflake\Core\Str; use Snowflake\Core\Str;
@@ -70,6 +65,11 @@ use HttpServer\Controller;
use {$model_namespace}\\{$managerName}; use {$model_namespace}\\{$managerName};
"; ";
} }
if (!empty($import)) {
$html .= $import;
}
$controllerName = $managerName;
$historyModel = "use {$model_namespace}\\{$managerName};"; $historyModel = "use {$model_namespace}\\{$managerName};";
if (!str_contains($html, $historyModel)) { if (!str_contains($html, $historyModel)) {
@@ -90,17 +90,8 @@ use {$model_namespace}\\{$managerName};
$funcNames = []; $funcNames = [];
if (is_object($class)) { if (is_object($class)) {
$methods = $class->getMethods(\ReflectionMethod::IS_PUBLIC); $html .= $this->getClassProperty($class);
$funcNames = array_column($methods, 'name'); $html .= $this->getClassMethods($class);
$classFileName = str_replace(APP_PATH, '', $class->getFileName());
if (!empty($methods)) foreach ($methods as $key => $val) {
if ($val->class != $class->getName()) continue;
$html .= "
" . $val->getDocComment() . "\n";
$html .= $this->getFuncLineContent($class, $classFileName, $val->name) . "\n";
}
} }
if (!$this->input->get('--controller-empty', false)) { if (!$this->input->get('--controller-empty', false)) {
$default = ['actionLoadParam', 'actionAdd', 'actionUpdate', 'actionDetail', 'actionDelete', 'actionBatchDelete', 'actionList']; $default = ['actionLoadParam', 'actionAdd', 'actionUpdate', 'actionDetail', 'actionDelete', 'actionBatchDelete', 'actionList'];
+29 -6
View File
@@ -19,10 +19,10 @@ class GiiInterceptor extends GiiBase
/** /**
* @return string[] * @return bool|array
* @throws Exception * @throws Exception
*/ */
public function generate(): array public function generate(): bool|array
{ {
$managerName = $this->input->get('name', null); $managerName = $this->input->get('name', null);
@@ -34,12 +34,24 @@ class GiiInterceptor extends GiiBase
namespace App\Http\Interceptor; namespace App\Http\Interceptor;
';
$file = APP_PATH . 'app/Http/Interceptor/' . $managerName . 'Interceptor.php';
if (file_exists($file)) {
try {
$class = new \ReflectionClass('App\\Http\\Interceptor\\' . $managerName . 'Interceptor');
$html .= $this->getImports($file, $class);
} catch (\Throwable $exception) {
return logger()->addError($exception);
}
} else {
$html .= '
use Closure; use Closure;
use HttpServer\Http\Request; use HttpServer\Http\Request;
use HttpServer\IInterface\Interceptor; use HttpServer\IInterface\Interceptor;
'; ';
}
$managerName = ucfirst($managerName); $managerName = ucfirst($managerName);
$html .= ' $html .= '
@@ -48,7 +60,17 @@ use HttpServer\IInterface\Interceptor;
* @package App\Http\Interceptor * @package App\Http\Interceptor
*/ */
class ' . $managerName . 'Interceptor implements Interceptor class ' . $managerName . 'Interceptor implements Interceptor
{ {';
if (isset($class)) {
$html .= $this->getClassProperty($class);
$html .= $this->getClassMethods($class);
$html .= '
}';
} else {
$html .= '
/** /**
* @param Request $request * @param Request $request
@@ -62,8 +84,9 @@ class ' . $managerName . 'Interceptor implements Interceptor
}'; }';
}
$file = APP_PATH . 'app/Http/Interceptor/' . $managerName . 'Interceptor.php';
if (file_exists($file)) { if (file_exists($file)) {
unlink($file); unlink($file);
} }
+2 -72
View File
@@ -49,7 +49,6 @@ class GiiModel extends GiiBase
$managerName = $this->classFileName; $managerName = $this->classFileName;
$namespace = rtrim($modelPath['namespace'], '\\'); $namespace = rtrim($modelPath['namespace'], '\\');
$classFileName = rtrim($modelPath['namespace'], '\\') . '\\' . $managerName;
$prefix = str_replace('_', '', $this->db->tablePrefix); $prefix = str_replace('_', '', $this->db->tablePrefix);
$managerName = str_replace(ucfirst($prefix), '', $managerName); $managerName = str_replace(ucfirst($prefix), '', $managerName);
@@ -106,49 +105,7 @@ use Database\ActiveRecord;
{'; {';
if (!empty($class)) { if (!empty($class)) {
foreach ($class->getConstants() as $key => $val) { $html .= $this->getClassProperty($class);
if (is_numeric($val)) {
$html .= '
const ' . $key . ' = ' . $val . ';' . "\n";
} else {
$html .= '
const ' . $key . ' = \'' . $val . '\';' . "\n";
}
}
foreach ($class->getDefaultProperties() as $key => $val) {
$property = $class->getProperty($key);
if ($property->class != $class->getName()) continue;
if (is_array($val)) {
$val = '[\'' . implode('\', \'', $val) . '\']';
} else if (!is_numeric($val)) {
$val = '\'' . $val . '\'';
}
if ($property->isProtected()) {
$debug = 'protected';
} else if ($property->isPrivate()) {
$debug = 'private';
} else {
$debug = 'public';
}
if ($property->hasType()) {
$type = ' ' . $property->getType() . ' $' . $key . ' = ' . $val . ';' . "\n";
} else {
$type = ' $' . $key . ' = ' . $val . ';' . "\n";
}
if ($property->isStatic()) {
$html .= '
' . $debug . ' static' . $type;
} else {
if ($key == 'primary') {
continue;
}
$html .= '
' . $debug . $type;
}
}
} }
$primary = $this->createPrimary($this->fields); $primary = $this->createPrimary($this->fields);
@@ -160,35 +117,8 @@ use Database\ActiveRecord;
$html .= $this->createRules($this->fields); $html .= $this->createRules($this->fields);
$out = ['rules', 'tableName', 'attributes'];
if (is_object($class)) { if (is_object($class)) {
$methods = $class->getMethods(); $html .= $this->getClassMethods($class, ['rules', 'tableName', 'attributes']);
$classFileName = str_replace(APP_PATH, '', $class->getFileName());
$content = [];
if (!empty($methods)) foreach ($methods as $key => $val) {
if ($val->class != $class->getName()) continue;
if (in_array($val->name, $out)) continue;
$over = "
" . $val->getDocComment() . "\n";
$attributes = $val->getAttributes();
if (!empty($attributes)) {
foreach ($attributes as $attribute) {
$explode = explode('\\', $attribute->getName());
$over .= " #[" . end($explode) . "('" . implode('\',\'', $attribute->getArguments()) . "')]
";
}
}
$func = $this->getFuncLineContent($class, $classFileName, $val->name) . "\n";
$content[] = $over . $func;
}
if (!empty($content)) {
$html .= implode($content);
}
} else { } else {
$html .= $this->createDatabaseSource(); $html .= $this->createDatabaseSource();
$other = $this->generate_json_function($html, $this->fields); $other = $this->generate_json_function($html, $this->fields);