From 13c016e5320415b9b8396624eff5a609fa1c4922 Mon Sep 17 00:00:00 2001 From: Abilio Posada Date: Mon, 6 Jul 2020 10:02:10 -0600 Subject: [PATCH 01/12] Add seed language Add seed language file --- system/Language/en/Seed.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 system/Language/en/Seed.php diff --git a/system/Language/en/Seed.php b/system/Language/en/Seed.php new file mode 100644 index 000000000000..a32f20a3e869 --- /dev/null +++ b/system/Language/en/Seed.php @@ -0,0 +1,22 @@ + 'Name the seeder file', + 'badName' => 'You must provide a seeder file name.', + 'writeError' => 'Error trying to create {0} file, check if the directory is writable.', +]; From 910033043ba4f4f28ce32378c6ed0791405778ad Mon Sep 17 00:00:00 2001 From: Abilio Posada Date: Mon, 6 Jul 2020 10:03:17 -0600 Subject: [PATCH 02/12] Add create seeder Add create seeder file --- system/Commands/Database/CreateSeeder.php | 174 ++++++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100644 system/Commands/Database/CreateSeeder.php diff --git a/system/Commands/Database/CreateSeeder.php b/system/Commands/Database/CreateSeeder.php new file mode 100644 index 000000000000..88c24ebb450b --- /dev/null +++ b/system/Commands/Database/CreateSeeder.php @@ -0,0 +1,174 @@ + 'The seeder file name', + ]; + + /** + * the Command's Options + * + * @var array + */ + protected $options = [ + '-n' => 'Set seeder namespace', + ]; + + /** + * Creates a new migration file with the current timestamp. + * + * @param array $params + */ + public function run(array $params = []) + { + helper('inflector'); + + $name = array_shift($params); + + if (empty($name)) + { + $name = CLI::prompt(lang('Seed.nameFile')); + } + + if (empty($name)) + { + CLI::error(lang('Seed.badName')); + return; + } + + $ns = $params['-n'] ?? CLI::getOption('n'); + $homepath = APPPATH; + + if (! empty($ns)) + { + // Get all namespaces + $namespaces = Services::autoloader()->getNamespace(); + + foreach ($namespaces as $namespace => $path) + { + if ($namespace === $ns) + { + $homepath = realpath(reset($path)); + break; + } + } + } + else + { + $ns = 'App'; + } + + // full path + $path = $homepath . '/Database/Seeds/' . $name . '.php'; + + // Class name should be pascal case now (camel case with upper first letter) + $name = pascalize($name); + + $template = << Date: Mon, 6 Jul 2020 12:50:10 -0600 Subject: [PATCH 03/12] Change command name Change the command name --- system/Commands/Database/CreateSeeder.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/system/Commands/Database/CreateSeeder.php b/system/Commands/Database/CreateSeeder.php index 88c24ebb450b..29e282046fc2 100644 --- a/system/Commands/Database/CreateSeeder.php +++ b/system/Commands/Database/CreateSeeder.php @@ -62,7 +62,7 @@ class CreateSeeder extends BaseCommand * * @var string */ - protected $name = 'db:seed:create'; + protected $name = 'make:seeder'; /** * the Command's short description @@ -76,7 +76,7 @@ class CreateSeeder extends BaseCommand * * @var string */ - protected $usage = 'db:seed:create [seeder_name] [options]'; + protected $usage = 'make:seeder [seeder_name] [options]'; /** * the Command's Arguments From 9bfb96f9eb8f7455d86b6dc041be9a16ac07cc05 Mon Sep 17 00:00:00 2001 From: Abilio Posada Date: Thu, 9 Jul 2020 08:14:28 -0600 Subject: [PATCH 04/12] Change group name command --- system/Commands/Database/CreateSeeder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Commands/Database/CreateSeeder.php b/system/Commands/Database/CreateSeeder.php index 29e282046fc2..86f9f0b91503 100644 --- a/system/Commands/Database/CreateSeeder.php +++ b/system/Commands/Database/CreateSeeder.php @@ -55,7 +55,7 @@ class CreateSeeder extends BaseCommand * * @var string */ - protected $group = 'Database'; + protected $group = 'Generators'; /** * The Command's name From 81203fcec21625972d2d54e222bb00d94d4a6655 Mon Sep 17 00:00:00 2001 From: Abilio Posada Date: Thu, 9 Jul 2020 08:15:42 -0600 Subject: [PATCH 05/12] Change name to work with PSR-4 autoloading --- system/Commands/Database/CreateSeeder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Commands/Database/CreateSeeder.php b/system/Commands/Database/CreateSeeder.php index 86f9f0b91503..b4b87b62d8ca 100644 --- a/system/Commands/Database/CreateSeeder.php +++ b/system/Commands/Database/CreateSeeder.php @@ -147,7 +147,7 @@ public function run(array $params = []) $name = pascalize($name); $template = << Date: Thu, 9 Jul 2020 08:30:48 -0600 Subject: [PATCH 06/12] Add DIRECTORY_SEPARATOR at the end of homepath --- system/Commands/Database/CreateSeeder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Commands/Database/CreateSeeder.php b/system/Commands/Database/CreateSeeder.php index b4b87b62d8ca..58f84c9648a6 100644 --- a/system/Commands/Database/CreateSeeder.php +++ b/system/Commands/Database/CreateSeeder.php @@ -130,7 +130,7 @@ public function run(array $params = []) { if ($namespace === $ns) { - $homepath = realpath(reset($path)); + $homepath = realpath(reset($path)) . DIRECTORY_SEPARATOR; break; } } From 7cf8b8c894450f2d258b2aad41f9f34677323941 Mon Sep 17 00:00:00 2001 From: Abilio Posada Date: Thu, 9 Jul 2020 08:31:08 -0600 Subject: [PATCH 07/12] Remove beginning forward slash of the path --- system/Commands/Database/CreateSeeder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Commands/Database/CreateSeeder.php b/system/Commands/Database/CreateSeeder.php index 58f84c9648a6..048ba985cd23 100644 --- a/system/Commands/Database/CreateSeeder.php +++ b/system/Commands/Database/CreateSeeder.php @@ -141,7 +141,7 @@ public function run(array $params = []) } // full path - $path = $homepath . '/Database/Seeds/' . $name . '.php'; + $path = $homepath . 'Database/Seeds/' . $name . '.php'; // Class name should be pascal case now (camel case with upper first letter) $name = pascalize($name); From 4dbaaa1222cf818b400ba943b9e9aec18077287e Mon Sep 17 00:00:00 2001 From: Abilio Posada Date: Thu, 9 Jul 2020 08:42:51 -0600 Subject: [PATCH 08/12] Add required validation on prompt --- system/Commands/Database/CreateSeeder.php | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/system/Commands/Database/CreateSeeder.php b/system/Commands/Database/CreateSeeder.php index 048ba985cd23..2dd21f79a073 100644 --- a/system/Commands/Database/CreateSeeder.php +++ b/system/Commands/Database/CreateSeeder.php @@ -109,13 +109,7 @@ public function run(array $params = []) if (empty($name)) { - $name = CLI::prompt(lang('Seed.nameFile')); - } - - if (empty($name)) - { - CLI::error(lang('Seed.badName')); - return; + $name = CLI::prompt(lang('Seed.nameFile'), null, 'required'); } $ns = $params['-n'] ?? CLI::getOption('n'); From 249c5e17f6413cd3599e4f993e9e3c60e5a20873 Mon Sep 17 00:00:00 2001 From: Abilio Posada Date: Thu, 9 Jul 2020 08:43:29 -0600 Subject: [PATCH 09/12] Remove unused language string --- system/Language/en/Seed.php | 1 - 1 file changed, 1 deletion(-) diff --git a/system/Language/en/Seed.php b/system/Language/en/Seed.php index a32f20a3e869..98429b4711ae 100644 --- a/system/Language/en/Seed.php +++ b/system/Language/en/Seed.php @@ -17,6 +17,5 @@ return [ // Seed command 'nameFile' => 'Name the seeder file', - 'badName' => 'You must provide a seeder file name.', 'writeError' => 'Error trying to create {0} file, check if the directory is writable.', ]; From 182eabdd2de4a933bf55864fab1c85f130a44e4f Mon Sep 17 00:00:00 2001 From: Abilio Posada Date: Thu, 9 Jul 2020 14:20:06 -0600 Subject: [PATCH 10/12] Rename namespace to work with PSR-4 --- system/Commands/Database/CreateSeeder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Commands/Database/CreateSeeder.php b/system/Commands/Database/CreateSeeder.php index 2dd21f79a073..36620cb9960f 100644 --- a/system/Commands/Database/CreateSeeder.php +++ b/system/Commands/Database/CreateSeeder.php @@ -143,7 +143,7 @@ public function run(array $params = []) $template = << Date: Thu, 9 Jul 2020 14:21:55 -0600 Subject: [PATCH 11/12] Change default namespace --- system/Commands/Database/CreateSeeder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Commands/Database/CreateSeeder.php b/system/Commands/Database/CreateSeeder.php index 36620cb9960f..53db640134b9 100644 --- a/system/Commands/Database/CreateSeeder.php +++ b/system/Commands/Database/CreateSeeder.php @@ -131,7 +131,7 @@ public function run(array $params = []) } else { - $ns = 'App'; + $ns = defined('APP_NAMESPACE') ? APP_NAMESPACE : 'App'; } // full path From 459e9a44405141ebf5bd4104d5014e0a17f7627d Mon Sep 17 00:00:00 2001 From: Abilio Posada Date: Thu, 9 Jul 2020 16:34:52 -0600 Subject: [PATCH 12/12] Replace namespace before show it --- system/Commands/Database/CreateSeeder.php | 1 + 1 file changed, 1 insertion(+) diff --git a/system/Commands/Database/CreateSeeder.php b/system/Commands/Database/CreateSeeder.php index 53db640134b9..3c7d620c56a6 100644 --- a/system/Commands/Database/CreateSeeder.php +++ b/system/Commands/Database/CreateSeeder.php @@ -163,6 +163,7 @@ public function run() return; } + $ns = rtrim(str_replace('\\', DIRECTORY_SEPARATOR, $ns), '\\') . DIRECTORY_SEPARATOR; CLI::write('Created file: ' . CLI::color(str_replace($homepath, $ns, $path), 'green')); } }