ModelCommand.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Database\Commands;
  12. use Hyperf\CodeParser\Project;
  13. use Hyperf\Command\Command;
  14. use Hyperf\Contract\ConfigInterface;
  15. use Hyperf\Database\Commands\Ast\GenerateModelIDEVisitor;
  16. use Hyperf\Database\Commands\Ast\ModelRewriteConnectionVisitor;
  17. use Hyperf\Database\Commands\Ast\ModelUpdateVisitor;
  18. use Hyperf\Database\ConnectionResolverInterface;
  19. use Hyperf\Database\Model\Model;
  20. use Hyperf\Database\Schema\Builder;
  21. use Hyperf\Stringable\Str;
  22. use PhpParser\Lexer;
  23. use PhpParser\Lexer\Emulative;
  24. use PhpParser\NodeTraverser;
  25. use PhpParser\NodeVisitor\CloningVisitor;
  26. use PhpParser\Parser;
  27. use PhpParser\ParserFactory;
  28. use PhpParser\PrettyPrinter\Standard;
  29. use PhpParser\PrettyPrinterAbstract;
  30. use Psr\Container\ContainerInterface;
  31. use Symfony\Component\Console\Input\InputArgument;
  32. use Symfony\Component\Console\Input\InputInterface;
  33. use Symfony\Component\Console\Input\InputOption;
  34. use Symfony\Component\Console\Output\OutputInterface;
  35. use function Hyperf\Support\make;
  36. class ModelCommand extends Command
  37. {
  38. protected ?ConnectionResolverInterface $resolver = null;
  39. protected ?ConfigInterface $config = null;
  40. protected ?Lexer $lexer = null;
  41. protected ?Parser $astParser = null;
  42. protected ?PrettyPrinterAbstract $printer = null;
  43. public function __construct(protected ContainerInterface $container)
  44. {
  45. parent::__construct('gen:model');
  46. $this->setDescription('Create new model classes.');
  47. }
  48. public function run(InputInterface $input, OutputInterface $output): int
  49. {
  50. $this->resolver = $this->container->get(ConnectionResolverInterface::class);
  51. $this->config = $this->container->get(ConfigInterface::class);
  52. $this->lexer = new Emulative([
  53. 'usedAttributes' => [
  54. 'comments',
  55. 'startLine', 'endLine',
  56. 'startTokenPos', 'endTokenPos',
  57. ],
  58. ]);
  59. $this->astParser = (new ParserFactory())->create(ParserFactory::ONLY_PHP7, $this->lexer);
  60. $this->printer = new Standard();
  61. return parent::run($input, $output);
  62. }
  63. public function handle()
  64. {
  65. $table = $this->input->getArgument('table');
  66. $pool = $this->input->getOption('pool');
  67. $option = new ModelOption();
  68. $option->setPool($pool)
  69. ->setPath($this->getOption('path', 'commands.gen:model.path', $pool, 'app/Model'))
  70. ->setPrefix($this->getOption('prefix', 'prefix', $pool, ''))
  71. ->setInheritance($this->getOption('inheritance', 'commands.gen:model.inheritance', $pool, 'Model'))
  72. ->setUses($this->getOption('uses', 'commands.gen:model.uses', $pool, 'Hyperf\DbConnection\Model\Model'))
  73. ->setForceCasts($this->getOption('force-casts', 'commands.gen:model.force_casts', $pool, false))
  74. ->setRefreshFillable($this->getOption('refresh-fillable', 'commands.gen:model.refresh_fillable', $pool, false))
  75. ->setTableMapping($this->getOption('table-mapping', 'commands.gen:model.table_mapping', $pool, []))
  76. ->setIgnoreTables($this->getOption('ignore-tables', 'commands.gen:model.ignore_tables', $pool, []))
  77. ->setWithComments($this->getOption('with-comments', 'commands.gen:model.with_comments', $pool, false))
  78. ->setWithIde($this->getOption('with-ide', 'commands.gen:model.with_ide', $pool, false))
  79. ->setVisitors($this->getOption('visitors', 'commands.gen:model.visitors', $pool, []))
  80. ->setPropertyCase($this->getOption('property-case', 'commands.gen:model.property_case', $pool));
  81. if ($table) {
  82. $this->createModel($table, $option);
  83. } else {
  84. $this->createModels($option);
  85. }
  86. }
  87. protected function configure()
  88. {
  89. $this->addArgument('table', InputArgument::OPTIONAL, 'Which table you want to associated with the Model.');
  90. $this->addOption('pool', 'p', InputOption::VALUE_OPTIONAL, 'Which connection pool you want the Model use.', 'default');
  91. $this->addOption('path', null, InputOption::VALUE_OPTIONAL, 'The path that you want the Model file to be generated.');
  92. $this->addOption('force-casts', 'F', InputOption::VALUE_NONE, 'Whether force generate the casts for model.');
  93. $this->addOption('prefix', 'P', InputOption::VALUE_OPTIONAL, 'What prefix that you want the Model set.');
  94. $this->addOption('inheritance', 'i', InputOption::VALUE_OPTIONAL, 'The inheritance that you want the Model extends.');
  95. $this->addOption('uses', 'U', InputOption::VALUE_OPTIONAL, 'The default class uses of the Model.');
  96. $this->addOption('refresh-fillable', 'R', InputOption::VALUE_NONE, 'Whether generate fillable argument for model.');
  97. $this->addOption('table-mapping', 'M', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Table mappings for model.');
  98. $this->addOption('ignore-tables', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Ignore tables for creating models.');
  99. $this->addOption('with-comments', null, InputOption::VALUE_NONE, 'Whether generate the property comments for model.');
  100. $this->addOption('with-ide', null, InputOption::VALUE_NONE, 'Whether generate the ide file for model.');
  101. $this->addOption('visitors', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Custom visitors for ast traverser.');
  102. $this->addOption('property-case', null, InputOption::VALUE_OPTIONAL, 'Which property case you want use, 0: snake case, 1: camel case.');
  103. }
  104. protected function getSchemaBuilder(string $poolName): Builder
  105. {
  106. $connection = $this->resolver->connection($poolName);
  107. return $connection->getSchemaBuilder();
  108. }
  109. protected function createModels(ModelOption $option)
  110. {
  111. $builder = $this->getSchemaBuilder($option->getPool());
  112. $tables = [];
  113. foreach ($builder->getAllTables() as $row) {
  114. $row = (array) $row;
  115. $table = reset($row);
  116. if (! $this->isIgnoreTable($table, $option)) {
  117. $tables[] = $table;
  118. }
  119. }
  120. foreach ($tables as $table) {
  121. $this->createModel($table, $option);
  122. }
  123. }
  124. protected function isIgnoreTable(string $table, ModelOption $option): bool
  125. {
  126. if (in_array($table, $option->getIgnoreTables())) {
  127. return true;
  128. }
  129. return $table === $this->config->get('databases.migrations', 'migrations');
  130. }
  131. protected function createModel(string $table, ModelOption $option): void
  132. {
  133. $builder = $this->getSchemaBuilder($option->getPool());
  134. $table = Str::replaceFirst($option->getPrefix(), '', $table);
  135. $pureTable = Str::after($table, '.');
  136. $databaseName = Str::contains($table, '.') ? Str::before($table, '.') : null;
  137. $driver = $this->resolver->connection($option->getPool())->getConfig('driver');
  138. $columns = match ($driver) {
  139. 'pgsql' => $this->formatColumns($builder->getColumnTypeListing($table, $databaseName)),
  140. default => $this->formatColumns($builder->getColumnTypeListing($pureTable, $databaseName)),
  141. };
  142. if (empty($columns)) {
  143. $this->output?->error(
  144. sprintf('Query columns are empty, maybe the table `%s` does not exist. You can check it in the database.', $table)
  145. );
  146. }
  147. $project = new Project();
  148. $class = $option->getTableMapping()[$table] ?? Str::studly(Str::singular($pureTable));
  149. $class = $project->namespace($option->getPath()) . $class;
  150. $path = BASE_PATH . '/' . $project->path($class);
  151. if (! file_exists($path)) {
  152. $this->mkdir($path);
  153. file_put_contents($path, $this->buildClass($table, $class, $option));
  154. }
  155. $columns = $this->getColumns($class, $columns, $option->isForceCasts());
  156. $traverser = new NodeTraverser();
  157. $traverser->addVisitor(make(ModelUpdateVisitor::class, [
  158. 'class' => $class,
  159. 'columns' => $columns,
  160. 'option' => $option,
  161. ]));
  162. $traverser->addVisitor(make(ModelRewriteConnectionVisitor::class, [$class, $option->getPool()]));
  163. $data = make(ModelData::class, ['class' => $class, 'columns' => $columns]);
  164. foreach ($option->getVisitors() as $visitorClass) {
  165. $traverser->addVisitor(make($visitorClass, [$option, $data]));
  166. }
  167. $traverser->addVisitor(new CloningVisitor());
  168. $originStmts = $this->astParser->parse(file_get_contents($path));
  169. $originTokens = $this->lexer->getTokens();
  170. $newStmts = $traverser->traverse($originStmts);
  171. $code = $this->printer->printFormatPreserving($newStmts, $originStmts, $originTokens);
  172. file_put_contents($path, $code);
  173. $this->output->writeln(sprintf('<info>Model %s was created.</info>', $class));
  174. if ($option->isWithIde()) {
  175. $this->generateIDE($code, $option, $data);
  176. }
  177. }
  178. protected function generateIDE(string $code, ModelOption $option, ModelData $data)
  179. {
  180. $stmts = $this->astParser->parse($code);
  181. $traverser = new NodeTraverser();
  182. $traverser->addVisitor(make(GenerateModelIDEVisitor::class, [$option, $data]));
  183. $stmts = $traverser->traverse($stmts);
  184. $code = $this->printer->prettyPrintFile($stmts);
  185. $class = str_replace('\\', '_', $data->getClass());
  186. $path = BASE_PATH . '/runtime/ide/' . $class . '.php';
  187. $this->mkdir($path);
  188. file_put_contents($path, $code);
  189. $this->output->writeln(sprintf('<info>Model IDE %s was created.</info>', $data->getClass()));
  190. }
  191. protected function mkdir(string $path): void
  192. {
  193. $dir = dirname($path);
  194. if (! is_dir($dir)) {
  195. @mkdir($dir, 0755, true);
  196. }
  197. }
  198. /**
  199. * Format column's key to lower case.
  200. */
  201. protected function formatColumns(array $columns): array
  202. {
  203. return array_map(function ($item) {
  204. return array_change_key_case($item, CASE_LOWER);
  205. }, $columns);
  206. }
  207. protected function getColumns($className, $columns, $forceCasts): array
  208. {
  209. /** @var Model $model */
  210. $model = new $className();
  211. $dates = $model->getDates();
  212. $casts = [];
  213. if (! $forceCasts) {
  214. $casts = $model->getCasts();
  215. }
  216. foreach ($dates as $date) {
  217. if (! isset($casts[$date])) {
  218. $casts[$date] = 'datetime';
  219. }
  220. }
  221. foreach ($columns as $key => $value) {
  222. $columns[$key]['cast'] = $casts[$value['column_name']] ?? null;
  223. }
  224. return $columns;
  225. }
  226. protected function getOption(string $name, string $key, string $pool = 'default', $default = null)
  227. {
  228. $result = $this->input->getOption($name);
  229. $nonInput = null;
  230. if (in_array($name, ['force-casts', 'refresh-fillable', 'with-comments', 'with-ide'])) {
  231. $nonInput = false;
  232. }
  233. if (in_array($name, ['table-mapping', 'ignore-tables', 'visitors'])) {
  234. $nonInput = [];
  235. }
  236. if ($result === $nonInput) {
  237. $result = $this->config->get("databases.{$pool}.{$key}", $default);
  238. }
  239. return $result;
  240. }
  241. /**
  242. * Build the class with the given name.
  243. */
  244. protected function buildClass(string $table, string $name, ModelOption $option): string
  245. {
  246. $stub = file_get_contents(__DIR__ . '/stubs/Model.stub');
  247. return $this->replaceNamespace($stub, $name)
  248. ->replaceInheritance($stub, $option->getInheritance())
  249. ->replaceConnection($stub, $option->getPool())
  250. ->replaceUses($stub, $option->getUses())
  251. ->replaceClass($stub, $name)
  252. ->replaceTable($stub, $table);
  253. }
  254. /**
  255. * Replace the namespace for the given stub.
  256. */
  257. protected function replaceNamespace(string &$stub, string $name): self
  258. {
  259. $stub = str_replace(
  260. ['%NAMESPACE%'],
  261. [$this->getNamespace($name)],
  262. $stub
  263. );
  264. return $this;
  265. }
  266. /**
  267. * Get the full namespace for a given class, without the class name.
  268. */
  269. protected function getNamespace(string $name): string
  270. {
  271. return trim(implode('\\', array_slice(explode('\\', $name), 0, -1)), '\\');
  272. }
  273. protected function replaceInheritance(string &$stub, string $inheritance): self
  274. {
  275. $stub = str_replace(
  276. ['%INHERITANCE%'],
  277. [$inheritance],
  278. $stub
  279. );
  280. return $this;
  281. }
  282. protected function replaceConnection(string &$stub, string $connection): self
  283. {
  284. $stub = str_replace(
  285. ['%CONNECTION%'],
  286. [$connection],
  287. $stub
  288. );
  289. return $this;
  290. }
  291. protected function replaceUses(string &$stub, string $uses): self
  292. {
  293. $uses = $uses ? "use {$uses};" : '';
  294. $stub = str_replace(
  295. ['%USES%'],
  296. [$uses],
  297. $stub
  298. );
  299. return $this;
  300. }
  301. /**
  302. * Replace the class name for the given stub.
  303. */
  304. protected function replaceClass(string &$stub, string $name): self
  305. {
  306. $class = str_replace($this->getNamespace($name) . '\\', '', $name);
  307. $stub = str_replace('%CLASS%', $class, $stub);
  308. return $this;
  309. }
  310. /**
  311. * Replace the table name for the given stub.
  312. */
  313. protected function replaceTable(string $stub, string $table): string
  314. {
  315. return str_replace('%TABLE%', $table, $stub);
  316. }
  317. /**
  318. * Get the destination class path.
  319. */
  320. protected function getPath(string $name): string
  321. {
  322. return BASE_PATH . '/' . str_replace('\\', '/', $name) . '.php';
  323. }
  324. }