hafijul233 /
ci-recharge
| 1 | <?php namespace Hafiz\Commands; |
||||
| 2 | |||||
| 3 | use CodeIgniter\CLI\BaseCommand; |
||||
| 4 | use CodeIgniter\CLI\CLI; |
||||
| 5 | use Hafiz\Libraries\DBHandler; |
||||
| 6 | use Hafiz\Libraries\FileHandler; |
||||
| 7 | |||||
| 8 | /** |
||||
| 9 | * Make Commadn for Seeder File Generation Class |
||||
| 10 | * Seeder can be skeleton aor loaded from a database table |
||||
| 11 | * |
||||
| 12 | * @package CodeIgniter\Commands |
||||
| 13 | * @extend BaseCommand |
||||
| 14 | */ |
||||
| 15 | class MakeSeeder extends BaseCommand |
||||
| 16 | { |
||||
| 17 | |||||
| 18 | /** |
||||
| 19 | * The group command is heading under all |
||||
| 20 | * commands will be listed |
||||
| 21 | * @var string |
||||
| 22 | */ |
||||
| 23 | protected $group = 'CI4-Recharge'; |
||||
| 24 | |||||
| 25 | /** |
||||
| 26 | * The Command's name |
||||
| 27 | * @var string |
||||
| 28 | */ |
||||
| 29 | protected $name = 'make:seed'; |
||||
| 30 | |||||
| 31 | /** |
||||
| 32 | * The Command's short description |
||||
| 33 | * @var string |
||||
| 34 | */ |
||||
| 35 | protected $description = 'Creates a Seeder file.'; |
||||
| 36 | |||||
| 37 | /** |
||||
| 38 | * The Command's usage |
||||
| 39 | * @var string |
||||
| 40 | */ |
||||
| 41 | protected $usage = 'make:seed [seed_name] [Options]'; |
||||
| 42 | |||||
| 43 | /** |
||||
| 44 | * The Command's Arguments |
||||
| 45 | * @var array |
||||
| 46 | */ |
||||
| 47 | protected $arguments = [ |
||||
| 48 | 'seed_name' => 'The Seeder file name', |
||||
| 49 | ]; |
||||
| 50 | |||||
| 51 | /** |
||||
| 52 | * The Command's Options |
||||
| 53 | * @var array |
||||
| 54 | */ |
||||
| 55 | protected $options = [ |
||||
| 56 | '-n' => 'Set seed namespace', |
||||
| 57 | '-t' => 'Set seed Database table', |
||||
| 58 | |||||
| 59 | ]; |
||||
| 60 | |||||
| 61 | /** |
||||
| 62 | * Creates a new entity file with the current timestamp. |
||||
| 63 | * @param array $params |
||||
| 64 | * @return void |
||||
| 65 | */ |
||||
| 66 | public function run(array $params = []) |
||||
| 67 | { |
||||
| 68 | helper(['inflector', 'filesystem']); |
||||
| 69 | $file = new FileHandler(); |
||||
| 70 | |||||
| 71 | $name = array_shift($params); |
||||
| 72 | $ns = $params['-n'] ?? CLI::getOption('n'); |
||||
| 73 | $table = $params['-t'] ?? CLI::getOption('t'); |
||||
| 74 | |||||
| 75 | if (empty($name)) { |
||||
| 76 | $name = CLI::prompt(lang('Recharge.nameSeed')); |
||||
| 77 | } |
||||
| 78 | |||||
| 79 | if (empty($name)) { |
||||
| 80 | CLI::error(lang('Recharge.badName')); |
||||
| 81 | return; |
||||
| 82 | } |
||||
| 83 | |||||
| 84 | //namespace locator |
||||
| 85 | $nsinfo = $file->getNamespaceInfo($ns, 'App'); |
||||
| 86 | |||||
| 87 | //class & file name |
||||
| 88 | $name = singular(pascalize($name)) . ((stripos($name, 'seeder') == false) ? 'Seeder' : ''); |
||||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||||
| 89 | $ns = $nsinfo['ns']; |
||||
| 90 | $targetDir = $nsinfo['path'] . '/Database/Seeds/'; |
||||
| 91 | $filepath = $targetDir . $name . '.php'; |
||||
| 92 | |||||
| 93 | if ($file->verifyDirectory($filepath)) { |
||||
| 94 | //do we have to add table info |
||||
| 95 | if (!empty($table)) { |
||||
| 96 | $db = new DBHandler(); |
||||
| 97 | if ($db->checkTableExist($table)) |
||||
|
0 ignored issues
–
show
It seems like
$table can also be of type boolean; however, parameter $table of Hafiz\Libraries\DBHandler::checkTableExist() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 98 | $properties = $db->generateRowArray($table); |
||||
| 99 | } |
||||
| 100 | |||||
| 101 | $data = ['{namespace}' => $ns, '{name}' => $name, '{created_at}' => date("d F, Y h:i:s A"), |
||||
| 102 | '{seeder}' => $properties ?? NULL, '{table}' => $table ?? NULL]; |
||||
| 103 | |||||
| 104 | //check a directory exist |
||||
| 105 | if ($file->checkFileExist($filepath) == true) { |
||||
|
0 ignored issues
–
show
|
|||||
| 106 | $template = $file->renderTemplate('seed', $data); |
||||
| 107 | |||||
| 108 | |||||
| 109 | if (!write_file($filepath, $template)) { |
||||
| 110 | CLI::error(lang('Recharge.writeError', [$filepath])); |
||||
| 111 | return; |
||||
| 112 | } |
||||
| 113 | |||||
| 114 | CLI::write('Created file: ' . CLI::color($filepath, 'green')); |
||||
| 115 | } |
||||
| 116 | } |
||||
| 117 | } |
||||
| 118 | } |
||||
| 119 |