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 command class that create a Entity class |
||
| 10 | * from user input with specific namespace |
||
| 11 | * or from a table with all attributes |
||
| 12 | * name of entity will be translated as singular |
||
| 13 | * |
||
| 14 | * @package CodeIgniter\Commands |
||
| 15 | * @extend BaseCommand |
||
| 16 | */ |
||
| 17 | class MakeEntity extends BaseCommand |
||
| 18 | { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * The group command is heading under all |
||
| 22 | * commands will be listed |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $group = 'CI4-Recharge'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The Command's name |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $name = 'make:entity'; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The Command's short description |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $description = 'Creates a Entity file skeleton or from database Table.'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The Command's usage |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | protected $usage = 'make:entity [entity_name] [Options]'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The Command's Arguments |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | protected $arguments = [ |
||
| 50 | 'entity_name' => 'The Entity file name', |
||
| 51 | ]; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The Command's Options |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | protected $options = [ |
||
| 58 | '-n' => 'Set entity Namespace', |
||
| 59 | '-t' => 'Set entity Database table', |
||
| 60 | |||
| 61 | ]; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Creates a new entity file with the current timestamp. |
||
| 65 | * @param array $params |
||
| 66 | * @return void |
||
| 67 | */ |
||
| 68 | public function run(array $params = []) |
||
| 69 | { |
||
| 70 | helper(['inflector', 'filesystem']); |
||
| 71 | $file = new FileHandler(); |
||
| 72 | |||
| 73 | $name = array_shift($params); |
||
| 74 | $ns = $params['-n'] ?? CLI::getOption('n'); |
||
| 75 | $table = $params['-t'] ?? CLI::getOption('t'); |
||
| 76 | |||
| 77 | if (empty($name)) { |
||
| 78 | $name = CLI::prompt(lang('Recharge.nameEntity'), null, 'required|string'); |
||
| 79 | } |
||
| 80 | |||
| 81 | if (empty($name)) { |
||
| 82 | CLI::error(lang('Recharge.badName')); |
||
| 83 | return; |
||
| 84 | } |
||
| 85 | |||
| 86 | //namespace locator |
||
| 87 | $nsinfo = $file->getNamespaceInfo($ns, 'App'); |
||
| 88 | |||
| 89 | //class & file name |
||
| 90 | $name = singular(pascalize($name)); |
||
| 91 | $ns = $nsinfo['ns']; |
||
| 92 | $targetDir = $nsinfo['path'] . '/Entities/'; |
||
| 93 | $filepath = $targetDir . $name . '.php'; |
||
| 94 | |||
| 95 | if ($file->verifyDirectory($filepath)) { |
||
| 96 | |||
| 97 | //do we have to add table info |
||
| 98 | if (!empty($table)) { |
||
| 99 | $db = new DBHandler(); |
||
| 100 | |||
| 101 | if ($db->checkTableExist($table)) { |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 102 | $properties = $db->getEntityProperties($table); |
||
| 103 | extract($properties); |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | $data = ['{namespace}' => $ns, '{name}' => $name, '{created_at}' => date("d F, Y h:i:s A"), |
||
| 108 | '{attributes}' => $attributes ?? NULL, '{dates}' => $dates ?? NULL, '{casts}' => $casts ?? NULL]; |
||
| 109 | |||
| 110 | //check a directory exist |
||
| 111 | if ($file->checkFileExist($filepath) == true) { |
||
|
0 ignored issues
–
show
|
|||
| 112 | $template = $file->renderTemplate('entity', $data); |
||
| 113 | |||
| 114 | |||
| 115 | if (!write_file($filepath, $template)) { |
||
| 116 | CLI::error(lang('Recharge.writeError', [$filepath])); |
||
| 117 | return; |
||
| 118 | } |
||
| 119 | |||
| 120 | CLI::write('Created file: ' . CLI::color($filepath, 'green')); |
||
| 121 | } |
||
| 122 | } |
||
| 123 | } |
||
| 124 | } |
||
| 125 |