|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Coolhax\CoolhaxCrudGenerator\Commands; |
| 4 | + |
| 5 | +use Illuminate\Support\Str; |
| 6 | + |
| 7 | +/** |
| 8 | + * Class CoolhaxCrudGenerator. |
| 9 | + * |
| 10 | + * @author Awais <[email protected]> |
| 11 | + */ |
| 12 | +class CoolhaxCrudGenerator extends GeneratorCommand |
| 13 | +{ |
| 14 | + /** |
| 15 | + * The name and signature of the console command. |
| 16 | + * |
| 17 | + * @var string |
| 18 | + */ |
| 19 | + protected $signature = 'make:crud |
| 20 | + {name : Table name} |
| 21 | + {--route= : Custom route name}'; |
| 22 | + |
| 23 | + /** |
| 24 | + * The console command description. |
| 25 | + * |
| 26 | + * @var string |
| 27 | + */ |
| 28 | + protected $description = 'Create bootstrap CRUD operations'; |
| 29 | + |
| 30 | + /** |
| 31 | + * Execute the console command. |
| 32 | + * |
| 33 | + * @return bool|null |
| 34 | + * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
| 35 | + * |
| 36 | + */ |
| 37 | + public function handle() |
| 38 | + { |
| 39 | + $this->info('Running Crud Generator ...'); |
| 40 | + |
| 41 | + $this->table = $this->getNameInput(); |
| 42 | + |
| 43 | + // If table not exist in DB return |
| 44 | + if (!$this->tableExists()) { |
| 45 | + $this->error("`{$this->table}` table not exist"); |
| 46 | + |
| 47 | + return false; |
| 48 | + } |
| 49 | + |
| 50 | + // Build the class name from table name |
| 51 | + $this->name = $this->_buildClassName(); |
| 52 | + |
| 53 | + // Generate the crud |
| 54 | + $this->buildOptions() |
| 55 | + ->buildController() |
| 56 | + ->buildModel() |
| 57 | + ->buildViews(); |
| 58 | + |
| 59 | + $this->info('Created Successfully.'); |
| 60 | + |
| 61 | + return true; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Build the Controller Class and save in app/Http/Controllers. |
| 66 | + * |
| 67 | + * @return $this |
| 68 | + * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
| 69 | + * |
| 70 | + */ |
| 71 | + protected function buildController() |
| 72 | + { |
| 73 | + $controllerPath = $this->_getControllerPath($this->name); |
| 74 | + |
| 75 | + if ($this->files->exists($controllerPath) && $this->ask('Already exist Controller. Do you want overwrite (y/n)?', 'y') == 'n') { |
| 76 | + return $this; |
| 77 | + } |
| 78 | + |
| 79 | + $this->info('Creating Controller ...'); |
| 80 | + |
| 81 | + $replace = $this->buildReplacements(); |
| 82 | + |
| 83 | + $controllerTemplate = str_replace( |
| 84 | + array_keys($replace), array_values($replace), $this->getStub('Controller') |
| 85 | + ); |
| 86 | + |
| 87 | + $this->write($controllerPath, $controllerTemplate); |
| 88 | + |
| 89 | + return $this; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @return $this |
| 94 | + * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
| 95 | + * |
| 96 | + */ |
| 97 | + protected function buildModel() |
| 98 | + { |
| 99 | + $modelPath = $this->_getModelPath($this->name); |
| 100 | + |
| 101 | + if ($this->files->exists($modelPath) && $this->ask('Already exist Model. Do you want overwrite (y/n)?', 'y') == 'n') { |
| 102 | + return $this; |
| 103 | + } |
| 104 | + |
| 105 | + $this->info('Creating Model ...'); |
| 106 | + |
| 107 | + // Make the models attributes and replacement |
| 108 | + $replace = array_merge($this->buildReplacements(), $this->modelReplacements()); |
| 109 | + |
| 110 | + $modelTemplate = str_replace( |
| 111 | + array_keys($replace), array_values($replace), $this->getStub('Model') |
| 112 | + ); |
| 113 | + |
| 114 | + $this->write($modelPath, $modelTemplate); |
| 115 | + |
| 116 | + return $this; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * @return $this |
| 121 | + * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
| 122 | + * |
| 123 | + * @throws \Exception |
| 124 | + */ |
| 125 | + protected function buildViews() |
| 126 | + { |
| 127 | + $this->info('Creating Views ...'); |
| 128 | + |
| 129 | + $tableHead = "\n"; |
| 130 | + $tableBody = "\n"; |
| 131 | + $viewRows = "\n"; |
| 132 | + $form = "\n"; |
| 133 | + |
| 134 | + foreach ($this->getFilteredColumns() as $column) { |
| 135 | + $title = Str::title(str_replace('_', ' ', $column)); |
| 136 | + |
| 137 | + $tableHead .= $this->getHead($title); |
| 138 | + $tableBody .= $this->getBody($column); |
| 139 | + $viewRows .= $this->getField($title, $column, 'view-field'); |
| 140 | + $form .= $this->getField($title, $column, 'form-field'); |
| 141 | + } |
| 142 | + |
| 143 | + $replace = array_merge($this->buildReplacements(), [ |
| 144 | + '{{tableHeader}}' => $tableHead, |
| 145 | + '{{tableBody}}' => $tableBody, |
| 146 | + '{{viewRows}}' => $viewRows, |
| 147 | + '{{form}}' => $form, |
| 148 | + ]); |
| 149 | + |
| 150 | + $this->buildLayout(); |
| 151 | + |
| 152 | + foreach (['index', 'create', 'edit', 'form', 'show'] as $view) { |
| 153 | + $viewTemplate = str_replace( |
| 154 | + array_keys($replace), array_values($replace), $this->getStub("views/{$view}") |
| 155 | + ); |
| 156 | + |
| 157 | + $this->write($this->_getViewPath($view), $viewTemplate); |
| 158 | + } |
| 159 | + |
| 160 | + return $this; |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Make the class name from table name. |
| 165 | + * |
| 166 | + * @return string |
| 167 | + */ |
| 168 | + private function _buildClassName() |
| 169 | + { |
| 170 | + return Str::studly(Str::singular($this->table)); |
| 171 | + } |
| 172 | +} |
0 commit comments