From 22ad849900923f698177ed9e1b636d54199eaa99 Mon Sep 17 00:00:00 2001 From: uctoo Date: Sun, 6 Jun 2021 15:57:50 +0800 Subject: [PATCH] add create:seed command --- extend/catcher/Utils.php | 28 ++++++++ .../command/Tools/CreateSeedCommand.php | 71 +++++++++++++++++++ extend/catcher/command/stubs/seed.stub | 8 ++- 3 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 extend/catcher/command/Tools/CreateSeedCommand.php diff --git a/extend/catcher/Utils.php b/extend/catcher/Utils.php index ee415b8..30bdd66 100644 --- a/extend/catcher/Utils.php +++ b/extend/catcher/Utils.php @@ -293,4 +293,32 @@ class Utils } } } + + /** + * 下划线转驼峰 + * 思路: + * step1.原字符串转小写,原字符串中的分隔符用空格替换,在字符串开头加上分隔符 + * step2.将字符串中每个单词的首字母转换为大写,再去空格,去字符串首部附加的分隔符. + * @param $uncamelized_words + * @param string $separator + * @return string + */ + public static function camelize($uncamelized_words,$separator='_') + { + $uncamelized_words = $separator. str_replace($separator, " ", strtolower($uncamelized_words)); + return ltrim(str_replace(" ", "", ucwords($uncamelized_words)), $separator ); + } + + + /** + * 驼峰命名转下划线命名 + * 思路: + * @param $camelCaps + * @param string $separator + * @return string + */ + public static function uncamelize($camelCaps,$separator='_') + { + return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . $separator . "$2", $camelCaps)); + } } diff --git a/extend/catcher/command/Tools/CreateSeedCommand.php b/extend/catcher/command/Tools/CreateSeedCommand.php new file mode 100644 index 0000000..0d706ba --- /dev/null +++ b/extend/catcher/command/Tools/CreateSeedCommand.php @@ -0,0 +1,71 @@ +setName('create:seed') + ->addArgument('table', Argument::REQUIRED, 'export tables') + ->addOption('module', '-m', Option::VALUE_REQUIRED, 'module name') + ->setDescription('Just for catchAdmin export data'); + } + + protected function execute(Input $input, Output $output) + { + $table = $input->getArgument('table'); + $module = $input->getOption('module'); + + if ($module) { + $data = Db::name($table)->where('deleted_at', 0) + // ->where('module', $module) + ->select() + ->toArray(); + + + } else { + $data = Db::name($table)->where('deleted_at', 0) + ->select() + ->toArray(); + } + + if ($module) { + $data = var_export($data, true) . ';'; + $this->exportSeed($data,$table, $module); + } else { + file_put_contents(root_path() . DIRECTORY_SEPARATOR . $table . '.php', "info('succeed!'); + } + + protected function exportSeed($data,$table, $module) + { + $stub = file_get_contents(dirname(__DIR__) . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'seed.stub'); + + $model = ucfirst(Utils::camelize($table)); + $class = ucfirst(Utils::camelize($table)) . 'Seed'; + + $stub = str_replace('{CLASS}', $class, $stub); + $stub = str_replace('{MODULE}', $module, $stub); + $stub = str_replace('{MODEL}', $model, $stub); + + file_put_contents(CatchAdmin::moduleSeedsDirectory($module) . $class .'.php', str_replace('{DATA}', $data, $stub)); + } +} + diff --git a/extend/catcher/command/stubs/seed.stub b/extend/catcher/command/stubs/seed.stub index 035c561..1ae1c8e 100644 --- a/extend/catcher/command/stubs/seed.stub +++ b/extend/catcher/command/stubs/seed.stub @@ -6,12 +6,12 @@ // +---------------------------------------------------------------------- // | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt ) // +---------------------------------------------------------------------- -// | Author: JaguarJack [ njphper@gmail.com ] +// | Author: UCToo [ contact@uctoo.com ] // +---------------------------------------------------------------------- use think\migration\Seeder; -class SeederClass extends Seeder +class {CLASS} extends Seeder { /** * Run Method. @@ -23,6 +23,10 @@ class SeederClass extends Seeder */ public function run() { + $data = {DATA} + foreach ($data as $item) { + \catchAdmin\{MODULE}\model\{MODEL}::create($item); + } } } \ No newline at end of file