|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Nanvaie\DatabaseRepository\Commands; |
| 4 | + |
| 5 | +use Nanvaie\DatabaseRepository\CustomMySqlQueries; |
| 6 | +use Illuminate\Console\Command; |
| 7 | + |
| 8 | +class MakeEnum extends Command |
| 9 | +{ |
| 10 | + /** |
| 11 | + * The name and signature of the console command. |
| 12 | + * |
| 13 | + * @var string |
| 14 | + */ |
| 15 | + protected $signature = 'repository:make-enum {table_name} |
| 16 | + {--d|delete : Delete resource} |
| 17 | + {--f|force : Override/Delete enum} |
| 18 | + {--g|add-to-git : Add created file to git repository}'; |
| 19 | + |
| 20 | + /** |
| 21 | + * The console command description. |
| 22 | + * |
| 23 | + * @var string |
| 24 | + */ |
| 25 | + protected $description = 'Create a new enum(s).'; |
| 26 | + |
| 27 | + use CustomMySqlQueries; |
| 28 | + |
| 29 | + /** |
| 30 | + * @param string $attributeStub |
| 31 | + * @param string $attributeName |
| 32 | + * @param string $attributeType |
| 33 | + * @return string |
| 34 | + */ |
| 35 | + private function writeAttribute(string $attributeStub, string $attributeName, string $attributeString): string |
| 36 | + { |
| 37 | + return str_replace(['{{ AttributeName }}', '{{ AttributeString }}'], |
| 38 | + [$attributeName, $attributeString], |
| 39 | + $attributeStub); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Execute the console command. |
| 44 | + * |
| 45 | + * @return int |
| 46 | + */ |
| 47 | + public function handle(): int |
| 48 | + { |
| 49 | + $tableName = $this->argument('table_name'); |
| 50 | + $enumNamespace = config('repository.path.namespace.enums'); |
| 51 | + $relativeEntitiesPath = config('repository.path.relative.enums'); |
| 52 | + $entityStubsPath = __DIR__ . '/../../' . config('repository.path.stub.enums'); |
| 53 | + |
| 54 | + |
| 55 | + $columns = $this->getAllColumnsInTable($tableName); |
| 56 | + |
| 57 | + if ($columns->isEmpty()) { |
| 58 | + $this->alert("Couldn't retrieve columns from table \"$tableName\"! Perhaps table's name is misspelled."); |
| 59 | + die; |
| 60 | + } |
| 61 | + |
| 62 | + $enums = []; |
| 63 | + foreach ($columns as $_column) { |
| 64 | + if ($_column->DATA_TYPE == 'enum') { |
| 65 | + $enumClassName = studly_case(substr_replace($_column->TABLE_NAME, '', -1) . '_' . $_column->COLUMN_NAME); |
| 66 | + $enums[$enumClassName] = explode(',', str_replace(['enum(', '\'', ')'], ['', '', ''], $_column->COLUMN_TYPE)); |
| 67 | + |
| 68 | + $filenameWithPath = $relativeEntitiesPath . $enumClassName.'.php'; |
| 69 | + |
| 70 | + if (file_exists($filenameWithPath) && $this->option('delete')) { |
| 71 | + unlink($filenameWithPath); |
| 72 | + $this->info("Enum \"$enumClassName\" has been deleted."); |
| 73 | + return 0; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // Create Attributes |
| 79 | + |
| 80 | + $baseContentStub = file_get_contents($entityStubsPath.'class.stub'); |
| 81 | + $attributeStub = file_get_contents($entityStubsPath.'attribute.stub'); |
| 82 | + |
| 83 | + foreach ($enums as $enumName => $enum) { |
| 84 | + $filenameWithPath = $relativeEntitiesPath . $enumName.'.php'; |
| 85 | + |
| 86 | + |
| 87 | + if ( ! file_exists($relativeEntitiesPath) && ! mkdir($relativeEntitiesPath, 0775, true) && ! is_dir($relativeEntitiesPath)) { |
| 88 | + $this->alert("Directory \"$relativeEntitiesPath\" was not created"); |
| 89 | + return 0; |
| 90 | + } |
| 91 | + |
| 92 | + if (class_exists($relativeEntitiesPath.'\\'.$enumName) && ! $this->option('force')) { |
| 93 | + $this->alert("Enum \"$enumName\" is already exist!"); |
| 94 | + return 0; |
| 95 | + } |
| 96 | + |
| 97 | + $attributes = ''; |
| 98 | + foreach ($enum as $_enum) { |
| 99 | + $attributes .= $this->writeAttribute( |
| 100 | + $attributeStub, |
| 101 | + strtoupper($_enum), |
| 102 | + $_enum |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | + $baseContent = str_replace(['{{ EnumNamespace }}', '{{ EnumName }}', '{{ Attributes }}',], |
| 108 | + [$enumNamespace, $enumName, $attributes,], |
| 109 | + $baseContentStub); |
| 110 | + |
| 111 | + file_put_contents($filenameWithPath, $baseContent); |
| 112 | + |
| 113 | + if ($this->option('add-to-git')) { |
| 114 | + shell_exec('git add '.$filenameWithPath); |
| 115 | + } |
| 116 | + |
| 117 | + $this->info("Enum \"$enumName\" has been created."); |
| 118 | + } |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | + return 0; |
| 123 | + } |
| 124 | +} |
0 commit comments