diff --git a/Gii/GiiBase.php b/Gii/GiiBase.php index 03929ac8..14c27b12 100644 --- a/Gii/GiiBase.php +++ b/Gii/GiiBase.php @@ -126,6 +126,29 @@ abstract class GiiBase } + /** + * @param string $fileName + * @param ReflectionClass $class + * @return string + */ + protected function getImports(string $fileName, ReflectionClass $class): string + { + $startLine = 1; + $array = []; + $fileOpen = fopen($fileName, 'r'); + while (($content = fgets($fileOpen)) !== false) { + if (str_starts_with($content, 'use ')) { + $array[] = $content; + } + if ($startLine == $class->getStartLine()) { + break; + } + ++$startLine; + } + return implode($array); + } + + /** * @param $fields * @return mixed 返回表主键 diff --git a/Gii/GiiModel.php b/Gii/GiiModel.php index eda5c19b..0a1d21d3 100644 --- a/Gii/GiiModel.php +++ b/Gii/GiiModel.php @@ -56,9 +56,18 @@ class GiiModel extends GiiBase if (file_exists($modelPath['path'] . '/' . $managerName . '.php')) { try { - $class = Snowflake::getDi()->getReflect("{$modelPath['namespace']}\{$managerName}"); + $className = str_replace('\\\\', '\\', "{$modelPath['namespace']}\\{$managerName}"); - $html = $this->getUseContent($class, $classFileName); + $class = Snowflake::getDi()->getReflect($className); + + $html = 'getImports($modelPath['path'] . '/' . $managerName . '.php', $class); + if (!empty($imports)) { + $html .= $imports . PHP_EOL. PHP_EOL; + } } catch (\Throwable $e) { logger()->error($e->getMessage()); } @@ -69,11 +78,13 @@ class GiiModel extends GiiBase $html = 'setCreateSql($this->tableName);