|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Rappasoft\LaravelLivewireTables\Commands; |
| 4 | + |
| 5 | +use Illuminate\Console\Command; |
| 6 | +use Illuminate\Support\Facades\File; |
| 7 | +use Illuminate\Support\Str; |
| 8 | +use Livewire\Commands\ComponentParser; |
| 9 | +use Livewire\Commands\MakeCommand as LivewireMakeCommand; |
| 10 | + |
| 11 | +/** |
| 12 | + * Class MakeCommand |
| 13 | + * |
| 14 | + * @package Rappasoft\LaravelLivewireTables\Commands |
| 15 | + */ |
| 16 | +class MakeCommand extends Command |
| 17 | +{ |
| 18 | + |
| 19 | + /** |
| 20 | + * @var |
| 21 | + */ |
| 22 | + protected $parser; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var |
| 26 | + */ |
| 27 | + protected $model; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var |
| 31 | + */ |
| 32 | + protected $viewPath; |
| 33 | + |
| 34 | + /** |
| 35 | + * The name and signature of the console command. |
| 36 | + * |
| 37 | + * @var string |
| 38 | + */ |
| 39 | + protected $signature = 'make:datatable |
| 40 | + {name : The name of your Livewire class} |
| 41 | + {model? : The name of the model you want to use in this table} |
| 42 | + {--view : We will generate a row view for you} |
| 43 | + {--force}'; |
| 44 | + |
| 45 | + /** |
| 46 | + * The console command description. |
| 47 | + * |
| 48 | + * @var string |
| 49 | + */ |
| 50 | + protected $description = 'Generate a Laravel Livewire datatable class and view.'; |
| 51 | + |
| 52 | + public function handle(): void |
| 53 | + { |
| 54 | + $this->parser = new ComponentParser( |
| 55 | + config('livewire.class_namespace'), |
| 56 | + config('livewire.view_path'), |
| 57 | + $this->argument('name') |
| 58 | + ); |
| 59 | + |
| 60 | + $livewireMakeCommand = new LivewireMakeCommand(); |
| 61 | + |
| 62 | + if ($livewireMakeCommand->isReservedClassName($name = $this->parser->className())) { |
| 63 | + $this->line("<fg=red;options=bold>Class is reserved:</> {$name}"); |
| 64 | + |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + $this->model = Str::studly($this->argument('model')); |
| 69 | + $force = $this->option('force'); |
| 70 | + |
| 71 | + $this->viewPath = $this->createView($force); |
| 72 | + $this->createClass($force); |
| 73 | + |
| 74 | + $this->info('Livewire Datatable Created: ' . $this->parser->className()); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @param bool $force |
| 79 | + * |
| 80 | + * @return false |
| 81 | + */ |
| 82 | + protected function createClass(bool $force = false) |
| 83 | + { |
| 84 | + $classPath = $this->parser->classPath(); |
| 85 | + |
| 86 | + if (! $force && File::exists($classPath)) { |
| 87 | + $this->line("<fg=red;options=bold>Class already exists:</> {$this->parser->relativeClassPath()}"); |
| 88 | + |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + $this->ensureDirectoryExists($classPath); |
| 93 | + |
| 94 | + File::put($classPath, $this->classContents()); |
| 95 | + |
| 96 | + return $classPath; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @param bool $force |
| 101 | + * |
| 102 | + * @return false|string|null |
| 103 | + */ |
| 104 | + protected function createView(bool $force = false) |
| 105 | + { |
| 106 | + if (! $this->option('view')) { |
| 107 | + return null; |
| 108 | + } |
| 109 | + |
| 110 | + $viewPath = base_path('resources/views/livewire-tables/rows/' . Str::snake($this->parser->className()->__toString()) . '.blade.php'); |
| 111 | + |
| 112 | + if (! $force && File::exists($viewPath)) { |
| 113 | + $this->line("<fg=red;options=bold>View already exists:</> {$viewPath}"); |
| 114 | + |
| 115 | + return false; |
| 116 | + } |
| 117 | + |
| 118 | + $this->ensureDirectoryExists($viewPath); |
| 119 | + |
| 120 | + File::put($viewPath, $this->viewContents()); |
| 121 | + |
| 122 | + return $viewPath; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * @param $path |
| 127 | + */ |
| 128 | + protected function ensureDirectoryExists($path): void |
| 129 | + { |
| 130 | + if (! File::isDirectory(dirname($path))) { |
| 131 | + File::makeDirectory(dirname($path), 0777, true, true); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * @return string |
| 137 | + */ |
| 138 | + public function classContents(): string |
| 139 | + { |
| 140 | + if ($this->model) { |
| 141 | + $template = file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'table-with-model.stub'); |
| 142 | + |
| 143 | + $contents = str_replace( |
| 144 | + ['[namespace]', '[class]', '[model]', '[model_import]'], |
| 145 | + [$this->parser->classNamespace(), $this->parser->className(), $this->model, $this->getModelImport()], |
| 146 | + $template |
| 147 | + ); |
| 148 | + } else { |
| 149 | + $template = file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'table.stub'); |
| 150 | + |
| 151 | + $contents = str_replace( |
| 152 | + ['[namespace]', '[class]'], |
| 153 | + [$this->parser->classNamespace(), $this->parser->className()], |
| 154 | + $template |
| 155 | + ); |
| 156 | + } |
| 157 | + |
| 158 | + if ($this->viewPath) { |
| 159 | + $contents = Str::replaceLast( |
| 160 | + "}\n", |
| 161 | + " |
| 162 | + public function rowView(): string |
| 163 | + { |
| 164 | + return '" . $this->getViewPathForRowView() . "'; |
| 165 | + } |
| 166 | +}\n", |
| 167 | + $contents |
| 168 | + ); |
| 169 | + } |
| 170 | + |
| 171 | + return $contents; |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * @return string |
| 176 | + */ |
| 177 | + private function getViewPathForRowView(): string |
| 178 | + { |
| 179 | + return Str::replace('/', '.', Str::before(Str::after($this->viewPath, 'resources/views/'), '.blade.php')); |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * @return false|string |
| 184 | + */ |
| 185 | + public function viewContents() |
| 186 | + { |
| 187 | + return file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'view.stub'); |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * @return string |
| 192 | + */ |
| 193 | + public function getModelImport(): string |
| 194 | + { |
| 195 | + if (File::exists(app_path('Models/' . $this->model . '.php'))) { |
| 196 | + return 'App\Models\\' . $this->model; |
| 197 | + } |
| 198 | + |
| 199 | + if (File::exists(app_path($this->model . '.php'))) { |
| 200 | + return 'App\\' . $this->model; |
| 201 | + } |
| 202 | + |
| 203 | + $this->error('Could not find path to model.'); |
| 204 | + |
| 205 | + return 'App\Models\\' . $this->model; |
| 206 | + } |
| 207 | +} |
0 commit comments