From e90a2f228008082244ff36b863fe78fb27a30efe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Thu, 29 Aug 2019 10:22:18 +0800 Subject: [PATCH 1/5] Added option no-fillable for db:model. --- .../src/Commands/Ast/ModelUpdateVisitor.php | 14 +++++++++-- src/database/src/Commands/ModelCommand.php | 11 ++++++--- src/database/src/Commands/ModelOption.php | 23 +++++++++++++++++++ 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/src/database/src/Commands/Ast/ModelUpdateVisitor.php b/src/database/src/Commands/Ast/ModelUpdateVisitor.php index a0a06e2df..048c246b6 100644 --- a/src/database/src/Commands/Ast/ModelUpdateVisitor.php +++ b/src/database/src/Commands/Ast/ModelUpdateVisitor.php @@ -12,24 +12,34 @@ declare(strict_types=1); namespace Hyperf\Database\Commands\Ast; +use Hyperf\Database\Commands\ModelOption; use PhpParser\Comment\Doc; use PhpParser\Node; use PhpParser\NodeVisitorAbstract; class ModelUpdateVisitor extends NodeVisitorAbstract { + /** + * @var array + */ protected $columns = []; - public function __construct($columns = []) + /** + * @var ModelOption + */ + protected $option; + + public function __construct($columns = [], ModelOption $option) { $this->columns = $columns; + $this->option = $option; } public function leaveNode(Node $node) { switch ($node) { case $node instanceof Node\Stmt\PropertyProperty: - if ($node->name == 'fillable') { + if ($node->name == 'fillable' && ! $this->option->isNoFillable()) { $node = $this->rewriteFillable($node); } elseif ($node->name == 'casts') { $node = $this->rewriteCasts($node); diff --git a/src/database/src/Commands/ModelCommand.php b/src/database/src/Commands/ModelCommand.php index 79a5a0263..1a628988a 100644 --- a/src/database/src/Commands/ModelCommand.php +++ b/src/database/src/Commands/ModelCommand.php @@ -93,7 +93,8 @@ class ModelCommand extends Command ->setPrefix($this->getOption('prefix', 'prefix', $pool, '')) ->setInheritance($this->getOption('inheritance', 'commands.db:model.inheritance', $pool, 'Model')) ->setUses($this->getOption('uses', 'commands.db:model.uses', $pool, 'Hyperf\DbConnection\Model\Model')) - ->setForceCasts($this->getOption('force-casts', 'commands.db:model.force_casts', $pool, false)); + ->setForceCasts($this->getOption('force-casts', 'commands.db:model.force_casts', $pool, false)) + ->setNoFillable($this->getOption('no-fillable', 'commands.db:model.no_fillable', $pool, false)); if ($table) { $this->createModel($table, $option); @@ -112,6 +113,7 @@ class ModelCommand extends Command $this->addOption('prefix', 'P', InputOption::VALUE_OPTIONAL, 'What prefix that you want the Model set.'); $this->addOption('inheritance', 'i', InputOption::VALUE_OPTIONAL, 'The inheritance that you want the Model extends.'); $this->addOption('uses', 'U', InputOption::VALUE_OPTIONAL, 'The default class uses of the Model.'); + $this->addOption('no-fillable', null, InputOption::VALUE_NONE, 'Whether generate fillable argement for model.'); } protected function getSchemaBuilder(string $poolName): MySqlBuilder @@ -158,7 +160,10 @@ class ModelCommand extends Command $stms = $this->astParser->parse(file_get_contents($path)); $traverser = new NodeTraverser(); - $visitor = make(ModelUpdateVisitor::class, ['columns' => $columns]); + $visitor = make(ModelUpdateVisitor::class, [ + 'columns' => $columns, + 'option' => $option, + ]); $traverser->addVisitor($visitor); $stms = $traverser->traverse($stms); $code = $this->printer->prettyPrintFile($stms); @@ -203,7 +208,7 @@ class ModelCommand extends Command protected function getOption(string $name, string $key, string $pool = 'default', $default = null) { $result = $this->input->getOption($name); - $nonInput = $name === 'force-casts' ? false : null; + $nonInput = in_array($name, ['force-casts', 'no-fillable']) ? false : null; if ($result === $nonInput) { $result = $this->config->get("databases.{$pool}.{$key}", $default); } diff --git a/src/database/src/Commands/ModelOption.php b/src/database/src/Commands/ModelOption.php index 1a1b35625..05f90bd49 100644 --- a/src/database/src/Commands/ModelOption.php +++ b/src/database/src/Commands/ModelOption.php @@ -44,6 +44,11 @@ class ModelOption */ protected $uses; + /** + * @var bool + */ + protected $noFillable; + public function getPool(): string { return $this->pool; @@ -116,4 +121,22 @@ class ModelOption $this->uses = $uses; return $this; } + + /** + * @return bool + */ + public function isNoFillable(): bool + { + return $this->noFillable; + } + + /** + * @param bool $noFillable + * @return ModelOption + */ + public function setNoFillable(bool $noFillable): ModelOption + { + $this->noFillable = $noFillable; + return $this; + } } From 5036db21191d13e457b38a66e606ff2d21755ff8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Thu, 29 Aug 2019 10:27:20 +0800 Subject: [PATCH 2/5] Update CHANGELOG.md --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4662e0569..d908b7879 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # v1.0.14 - TBD +## Added + +- [#482](https://github.com/hyperf-cloud/hyperf/pull/482) Don't auto rewrite fillable argument when using no-fillable option. + ## Fixed - [#479](https://github.com/hyperf-cloud/hyperf/pull/479) Fixed typehint error when host of Elasticsearch client does not reached. From ef78ddb08f5ce4d3c382a715f660d3a77f10aceb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Thu, 29 Aug 2019 14:38:28 +0800 Subject: [PATCH 3/5] Added refresh-fillable option. --- CHANGELOG.md | 4 ++-- src/database/src/Commands/Ast/ModelUpdateVisitor.php | 2 +- src/database/src/Commands/ModelCommand.php | 6 +++--- src/database/src/Commands/ModelOption.php | 12 ++++++------ 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d908b7879..0e17e83e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,8 @@ # v1.0.14 - TBD -## Added +## Changed -- [#482](https://github.com/hyperf-cloud/hyperf/pull/482) Don't auto rewrite fillable argument when using no-fillable option. +- [#482](https://github.com/hyperf-cloud/hyperf/pull/482) Don't auto rewrite fillable argument unless using refresh-fillable option. ## Fixed diff --git a/src/database/src/Commands/Ast/ModelUpdateVisitor.php b/src/database/src/Commands/Ast/ModelUpdateVisitor.php index 048c246b6..b0f9f07cb 100644 --- a/src/database/src/Commands/Ast/ModelUpdateVisitor.php +++ b/src/database/src/Commands/Ast/ModelUpdateVisitor.php @@ -39,7 +39,7 @@ class ModelUpdateVisitor extends NodeVisitorAbstract { switch ($node) { case $node instanceof Node\Stmt\PropertyProperty: - if ($node->name == 'fillable' && ! $this->option->isNoFillable()) { + if ($node->name == 'fillable' && $this->option->isRefreshFillable()) { $node = $this->rewriteFillable($node); } elseif ($node->name == 'casts') { $node = $this->rewriteCasts($node); diff --git a/src/database/src/Commands/ModelCommand.php b/src/database/src/Commands/ModelCommand.php index 1a628988a..cee45436a 100644 --- a/src/database/src/Commands/ModelCommand.php +++ b/src/database/src/Commands/ModelCommand.php @@ -94,7 +94,7 @@ class ModelCommand extends Command ->setInheritance($this->getOption('inheritance', 'commands.db:model.inheritance', $pool, 'Model')) ->setUses($this->getOption('uses', 'commands.db:model.uses', $pool, 'Hyperf\DbConnection\Model\Model')) ->setForceCasts($this->getOption('force-casts', 'commands.db:model.force_casts', $pool, false)) - ->setNoFillable($this->getOption('no-fillable', 'commands.db:model.no_fillable', $pool, false)); + ->setRefreshFillable($this->getOption('refresh-fillable', 'commands.db:model.refresh_fillable', $pool, false)); if ($table) { $this->createModel($table, $option); @@ -113,7 +113,7 @@ class ModelCommand extends Command $this->addOption('prefix', 'P', InputOption::VALUE_OPTIONAL, 'What prefix that you want the Model set.'); $this->addOption('inheritance', 'i', InputOption::VALUE_OPTIONAL, 'The inheritance that you want the Model extends.'); $this->addOption('uses', 'U', InputOption::VALUE_OPTIONAL, 'The default class uses of the Model.'); - $this->addOption('no-fillable', null, InputOption::VALUE_NONE, 'Whether generate fillable argement for model.'); + $this->addOption('refresh-fillable', null, InputOption::VALUE_NONE, 'Whether generate fillable argement for model.'); } protected function getSchemaBuilder(string $poolName): MySqlBuilder @@ -208,7 +208,7 @@ class ModelCommand extends Command protected function getOption(string $name, string $key, string $pool = 'default', $default = null) { $result = $this->input->getOption($name); - $nonInput = in_array($name, ['force-casts', 'no-fillable']) ? false : null; + $nonInput = in_array($name, ['force-casts', 'refresh-fillable']) ? false : null; if ($result === $nonInput) { $result = $this->config->get("databases.{$pool}.{$key}", $default); } diff --git a/src/database/src/Commands/ModelOption.php b/src/database/src/Commands/ModelOption.php index 05f90bd49..89d0881d3 100644 --- a/src/database/src/Commands/ModelOption.php +++ b/src/database/src/Commands/ModelOption.php @@ -47,7 +47,7 @@ class ModelOption /** * @var bool */ - protected $noFillable; + protected $refreshFillable; public function getPool(): string { @@ -125,18 +125,18 @@ class ModelOption /** * @return bool */ - public function isNoFillable(): bool + public function isRefreshFillable(): bool { - return $this->noFillable; + return $this->refreshFillable; } /** - * @param bool $noFillable + * @param bool $refreshFillable * @return ModelOption */ - public function setNoFillable(bool $noFillable): ModelOption + public function setRefreshFillable(bool $refreshFillable): ModelOption { - $this->noFillable = $noFillable; + $this->refreshFillable = $refreshFillable; return $this; } } From 254c4b3730b0b259d9cf7ed61fc4ada0d6e6b6af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=9C=9D=E6=99=96?= Date: Thu, 29 Aug 2019 15:15:24 +0800 Subject: [PATCH 4/5] Update ModelOption.php --- src/database/src/Commands/ModelOption.php | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/src/database/src/Commands/ModelOption.php b/src/database/src/Commands/ModelOption.php index 89d0881d3..9c2eba451 100644 --- a/src/database/src/Commands/ModelOption.php +++ b/src/database/src/Commands/ModelOption.php @@ -104,36 +104,22 @@ class ModelOption return $this; } - /** - * @return string - */ public function getUses(): string { return $this->uses; } - /** - * @param string $uses - * @return ModelOption - */ public function setUses(string $uses): ModelOption { $this->uses = $uses; return $this; } - /** - * @return bool - */ public function isRefreshFillable(): bool { return $this->refreshFillable; } - /** - * @param bool $refreshFillable - * @return ModelOption - */ public function setRefreshFillable(bool $refreshFillable): ModelOption { $this->refreshFillable = $refreshFillable; From b386df56300cad14ad16d52ab45e2f79c288f305 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=9C=9D=E6=99=96?= Date: Thu, 29 Aug 2019 15:19:17 +0800 Subject: [PATCH 5/5] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e17e83e9..073898333 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## Changed -- [#482](https://github.com/hyperf-cloud/hyperf/pull/482) Don't auto rewrite fillable argument unless using refresh-fillable option. +- [#482](https://github.com/hyperf-cloud/hyperf/pull/482) Re-generate the `fillable` argument of Model when use `refresh-fillable` option, at the same time, the command will keep the `fillable` argument as default behaviours. ## Fixed