diff --git a/Gii/GiiBase.php b/Gii/GiiBase.php index 14c27b12..3b165a66 100644 --- a/Gii/GiiBase.php +++ b/Gii/GiiBase.php @@ -6,9 +6,11 @@ namespace Gii; use Database\Connection; +use Exception; use JetBrains\PhpStorm\ArrayShape; use ReflectionClass; +use ReflectionException; 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 * @return mixed 返回表主键 @@ -189,7 +282,7 @@ abstract class GiiBase * @param $className * @param $method * @return string - * @throws \Exception + * @throws Exception */ public function getFuncLineContent(ReflectionClass $object, $className, $method): string { diff --git a/Gii/GiiController.php b/Gii/GiiController.php index 0a212684..30f56e1e 100644 --- a/Gii/GiiController.php +++ b/Gii/GiiController.php @@ -44,22 +44,17 @@ class GiiController extends GiiBase $class = ''; $controller = "$namespace\{$managerName}Controller"; - if (file_exists($path['path'] . '/' . $managerName . 'Controller.php')) { - $class = Snowflake::getDi()->getReflect($controller); - } - $controllerName = $managerName; - - $html = $class instanceof \ReflectionClass ? $this->getUseContent($class, $controller) : null; - - - if (empty($html)) { - - - $html .= "getReflect($controller); + + $import = $this->getImports($path['path'] . '/' . $managerName . 'Controller.php', $class); + }else{ + $import = "use Snowflake; use exception; use Annotation\Target; use Snowflake\Core\Str; @@ -70,6 +65,11 @@ use HttpServer\Controller; use {$model_namespace}\\{$managerName}; "; } + if (!empty($import)) { + $html .= $import; + } + + $controllerName = $managerName; $historyModel = "use {$model_namespace}\\{$managerName};"; if (!str_contains($html, $historyModel)) { @@ -90,17 +90,8 @@ use {$model_namespace}\\{$managerName}; $funcNames = []; if (is_object($class)) { - $methods = $class->getMethods(\ReflectionMethod::IS_PUBLIC); - $funcNames = array_column($methods, 'name'); - - $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"; - } + $html .= $this->getClassProperty($class); + $html .= $this->getClassMethods($class); } if (!$this->input->get('--controller-empty', false)) { $default = ['actionLoadParam', 'actionAdd', 'actionUpdate', 'actionDetail', 'actionDelete', 'actionBatchDelete', 'actionList']; diff --git a/Gii/GiiInterceptor.php b/Gii/GiiInterceptor.php index b54726c4..4c89d6b3 100644 --- a/Gii/GiiInterceptor.php +++ b/Gii/GiiInterceptor.php @@ -19,10 +19,10 @@ class GiiInterceptor extends GiiBase /** - * @return string[] + * @return bool|array * @throws Exception */ - public function generate(): array + public function generate(): bool|array { $managerName = $this->input->get('name', null); @@ -34,12 +34,24 @@ class GiiInterceptor extends GiiBase 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 HttpServer\Http\Request; use HttpServer\IInterface\Interceptor; - - '; + } + $managerName = ucfirst($managerName); $html .= ' @@ -48,7 +60,17 @@ use HttpServer\IInterface\Interceptor; * @package App\Http\Interceptor */ class ' . $managerName . 'Interceptor implements Interceptor -{ +{'; + + if (isset($class)) { + $html .= $this->getClassProperty($class); + $html .= $this->getClassMethods($class); + + $html .= ' + +}'; + } else { + $html .= ' /** * @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)) { unlink($file); } diff --git a/Gii/GiiModel.php b/Gii/GiiModel.php index 1a241f78..9278c24d 100644 --- a/Gii/GiiModel.php +++ b/Gii/GiiModel.php @@ -49,7 +49,6 @@ class GiiModel extends GiiBase $managerName = $this->classFileName; $namespace = rtrim($modelPath['namespace'], '\\'); - $classFileName = rtrim($modelPath['namespace'], '\\') . '\\' . $managerName; $prefix = str_replace('_', '', $this->db->tablePrefix); $managerName = str_replace(ucfirst($prefix), '', $managerName); @@ -106,49 +105,7 @@ use Database\ActiveRecord; {'; if (!empty($class)) { - 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; - } - - } + $html .= $this->getClassProperty($class); } $primary = $this->createPrimary($this->fields); @@ -160,35 +117,8 @@ use Database\ActiveRecord; $html .= $this->createRules($this->fields); - $out = ['rules', 'tableName', 'attributes']; if (is_object($class)) { - $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, $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); - } + $html .= $this->getClassMethods($class, ['rules', 'tableName', 'attributes']); } else { $html .= $this->createDatabaseSource(); $other = $this->generate_json_function($html, $this->fields);