diff --git a/system/Commands/Database/CreateSeeder.php b/system/Commands/Database/CreateSeeder.php new file mode 100644 index 000000000000..3c7d620c56a6 --- /dev/null +++ b/system/Commands/Database/CreateSeeder.php @@ -0,0 +1,169 @@ + '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'), null, 'required'); + } + + $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)) . DIRECTORY_SEPARATOR; + break; + } + } + } + else + { + $ns = defined('APP_NAMESPACE') ? APP_NAMESPACE : '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 = << 'Name the seeder file', + 'writeError' => 'Error trying to create {0} file, check if the directory is writable.', +];