ModelCommand.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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)
  132. {
  133. $builder = $this->getSchemaBuilder($option->getPool());
  134. $table = Str::replaceFirst($option->getPrefix(), '', $table);
  135. $columns = $this->formatColumns($builder->getColumnTypeListing($table));
  136. $project = new Project();
  137. $class = $option->getTableMapping()[$table] ?? Str::studly(Str::singular($table));
  138. $class = $project->namespace($option->getPath()) . $class;
  139. $path = BASE_PATH . '/' . $project->path($class);
  140. if (! file_exists($path)) {
  141. $this->mkdir($path);
  142. file_put_contents($path, $this->buildClass($table, $class, $option));
  143. }
  144. $columns = $this->getColumns($class, $columns, $option->isForceCasts());
  145. $traverser = new NodeTraverser();
  146. $traverser->addVisitor(make(ModelUpdateVisitor::class, [
  147. 'class' => $class,
  148. 'columns' => $columns,
  149. 'option' => $option,
  150. ]));
  151. $traverser->addVisitor(make(ModelRewriteConnectionVisitor::class, [$class, $option->getPool()]));
  152. $data = make(ModelData::class, ['class' => $class, 'columns' => $columns]);
  153. foreach ($option->getVisitors() as $visitorClass) {
  154. $traverser->addVisitor(make($visitorClass, [$option, $data]));
  155. }
  156. $traverser->addVisitor(new CloningVisitor());
  157. $originStmts = $this->astParser->parse(file_get_contents($path));
  158. $originTokens = $this->lexer->getTokens();
  159. $newStmts = $traverser->traverse($originStmts);
  160. $code = $this->printer->printFormatPreserving($newStmts, $originStmts, $originTokens);
  161. file_put_contents($path, $code);
  162. $this->output->writeln(sprintf('<info>Model %s was created.</info>', $class));
  163. if ($option->isWithIde()) {
  164. $this->generateIDE($code, $option, $data);
  165. }
  166. }
  167. protected function generateIDE(string $code, ModelOption $option, ModelData $data)
  168. {
  169. $stmts = $this->astParser->parse($code);
  170. $traverser = new NodeTraverser();
  171. $traverser->addVisitor(make(GenerateModelIDEVisitor::class, [$option, $data]));
  172. $stmts = $traverser->traverse($stmts);
  173. $code = $this->printer->prettyPrintFile($stmts);
  174. $class = str_replace('\\', '_', $data->getClass());
  175. $path = BASE_PATH . '/runtime/ide/' . $class . '.php';
  176. $this->mkdir($path);
  177. file_put_contents($path, $code);
  178. $this->output->writeln(sprintf('<info>Model IDE %s was created.</info>', $data->getClass()));
  179. }
  180. protected function mkdir(string $path): void
  181. {
  182. $dir = dirname($path);
  183. if (! is_dir($dir)) {
  184. @mkdir($dir, 0755, true);
  185. }
  186. }
  187. /**
  188. * Format column's key to lower case.
  189. */
  190. protected function formatColumns(array $columns): array
  191. {
  192. return array_map(function ($item) {
  193. return array_change_key_case($item, CASE_LOWER);
  194. }, $columns);
  195. }
  196. protected function getColumns($className, $columns, $forceCasts): array
  197. {
  198. /** @var Model $model */
  199. $model = new $className();
  200. $dates = $model->getDates();
  201. $casts = [];
  202. if (! $forceCasts) {
  203. $casts = $model->getCasts();
  204. }
  205. foreach ($dates as $date) {
  206. if (! isset($casts[$date])) {
  207. $casts[$date] = 'datetime';
  208. }
  209. }
  210. foreach ($columns as $key => $value) {
  211. $columns[$key]['cast'] = $casts[$value['column_name']] ?? null;
  212. }
  213. return $columns;
  214. }
  215. protected function getOption(string $name, string $key, string $pool = 'default', $default = null)
  216. {
  217. $result = $this->input->getOption($name);
  218. $nonInput = null;
  219. if (in_array($name, ['force-casts', 'refresh-fillable', 'with-comments', 'with-ide'])) {
  220. $nonInput = false;
  221. }
  222. if (in_array($name, ['table-mapping', 'ignore-tables', 'visitors'])) {
  223. $nonInput = [];
  224. }
  225. if ($result === $nonInput) {
  226. $result = $this->config->get("databases.{$pool}.{$key}", $default);
  227. }
  228. return $result;
  229. }
  230. /**
  231. * Build the class with the given name.
  232. */
  233. protected function buildClass(string $table, string $name, ModelOption $option): string
  234. {
  235. $stub = file_get_contents(__DIR__ . '/stubs/Model.stub');
  236. return $this->replaceNamespace($stub, $name)
  237. ->replaceInheritance($stub, $option->getInheritance())
  238. ->replaceConnection($stub, $option->getPool())
  239. ->replaceUses($stub, $option->getUses())
  240. ->replaceClass($stub, $name)
  241. ->replaceTable($stub, $table);
  242. }
  243. /**
  244. * Replace the namespace for the given stub.
  245. */
  246. protected function replaceNamespace(string &$stub, string $name): self
  247. {
  248. $stub = str_replace(
  249. ['%NAMESPACE%'],
  250. [$this->getNamespace($name)],
  251. $stub
  252. );
  253. return $this;
  254. }
  255. /**
  256. * Get the full namespace for a given class, without the class name.
  257. */
  258. protected function getNamespace(string $name): string
  259. {
  260. return trim(implode('\\', array_slice(explode('\\', $name), 0, -1)), '\\');
  261. }
  262. protected function replaceInheritance(string &$stub, string $inheritance): self
  263. {
  264. $stub = str_replace(
  265. ['%INHERITANCE%'],
  266. [$inheritance],
  267. $stub
  268. );
  269. return $this;
  270. }
  271. protected function replaceConnection(string &$stub, string $connection): self
  272. {
  273. $stub = str_replace(
  274. ['%CONNECTION%'],
  275. [$connection],
  276. $stub
  277. );
  278. return $this;
  279. }
  280. protected function replaceUses(string &$stub, string $uses): self
  281. {
  282. $uses = $uses ? "use {$uses};" : '';
  283. $stub = str_replace(
  284. ['%USES%'],
  285. [$uses],
  286. $stub
  287. );
  288. return $this;
  289. }
  290. /**
  291. * Replace the class name for the given stub.
  292. */
  293. protected function replaceClass(string &$stub, string $name): self
  294. {
  295. $class = str_replace($this->getNamespace($name) . '\\', '', $name);
  296. $stub = str_replace('%CLASS%', $class, $stub);
  297. return $this;
  298. }
  299. /**
  300. * Replace the table name for the given stub.
  301. */
  302. protected function replaceTable(string $stub, string $table): string
  303. {
  304. return str_replace('%TABLE%', $table, $stub);
  305. }
  306. /**
  307. * Get the destination class path.
  308. */
  309. protected function getPath(string $name): string
  310. {
  311. return BASE_PATH . '/' . str_replace('\\', '/', $name) . '.php';
  312. }
  313. }