|
3 | 3 | namespace Designbycode\EloquentDatatable\Commands; |
4 | 4 |
|
5 | 5 | use Illuminate\Console\Command; |
| 6 | +use Illuminate\Support\Facades\File; |
| 7 | +use Illuminate\Support\Str; |
6 | 8 |
|
7 | 9 | class EloquentDatatableCommand extends Command |
8 | 10 | { |
9 | | - public $signature = 'eloquent-datatable'; |
| 11 | + protected $signature = 'datatable:create {name : The name of the controller} {--model= : The model associated with the controller} {--middleware= : The middleware to be applied (comma separated)}'; |
10 | 12 |
|
11 | | - public $description = 'My command'; |
| 13 | + protected $description = 'Create a DataTable controller'; |
12 | 14 |
|
13 | | - public function handle(): int |
| 15 | + public function __construct() |
14 | 16 | { |
15 | | - $this->comment('All done'); |
| 17 | + parent::__construct(); |
| 18 | + } |
| 19 | + |
| 20 | + public function handle(): void |
| 21 | + { |
| 22 | + $controllerName = $this->argument('name'); |
| 23 | + $modelName = $this->option('model'); |
| 24 | + $middleware = $this->option('middleware'); |
| 25 | + |
| 26 | + // Read the stub file |
| 27 | + $stub = File::get(__DIR__.'/../../stubs/datatable.stub'); |
| 28 | + |
| 29 | + if (! $modelName) { |
| 30 | + $modelName = $this->getSingularName($controllerName); |
| 31 | + if (! $this->confirm("Do you want to use '$modelName' as the model name?", true)) { |
| 32 | + $modelName = $this->ask('Enter the model name:'); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + if ($this->confirm("Do you want to use '$modelName' model fillable fields for updating columns?", true)) { |
| 37 | + // Get the path to the model file |
| 38 | + $modelPath = $this->getModelPath($modelName); |
| 39 | + |
| 40 | + // Read the model file |
| 41 | + $modelContent = File::get($modelPath); |
| 42 | + |
| 43 | + // Extract the $fillable fields from the model content |
| 44 | + $fillable = $this->extractFillableFields($modelContent); |
| 45 | + |
| 46 | + $stub = str_replace('// :fillable', implode(',', $fillable), $stub); |
| 47 | + } else { |
| 48 | + $stub = str_replace('// :fillable', "''", $stub); |
| 49 | + } |
| 50 | + |
| 51 | + // Replace placeholders |
| 52 | + $stub = str_replace('StubController', $controllerName, $stub); |
| 53 | + $stub = str_replace('StubModel', $modelName, $stub); |
| 54 | + |
| 55 | + // Define namespace, path, class name, and file name |
| 56 | + $namespace = 'App\\Http\\Controllers\\Datatables'; |
| 57 | + $path = app_path('Http\\Controllers\\Datatables'); |
| 58 | + $className = $controllerName; |
| 59 | + $fileName = $className.'.php'; |
| 60 | + |
| 61 | + // Ensure the directory exists |
| 62 | + File::ensureDirectoryExists($path); |
| 63 | + |
| 64 | + if ($middleware) { |
| 65 | + $middlewareArray = explode(',', $middleware); |
| 66 | + $middlewareCode = "\$this->middleware(['".implode("','", $middlewareArray)."']);"; |
| 67 | + $stub = str_replace('// :middleware', $middlewareCode, $stub); |
| 68 | + } |
| 69 | + |
| 70 | + // Check if model exists |
| 71 | + if (! $this->modelExists($modelName)) { |
| 72 | + $this->error("Model '$modelName' does not exist in App\Models directory."); |
| 73 | + |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + // Check if the file already exists |
| 78 | + if (File::exists($path.'\/'.$fileName)) { |
| 79 | + $this->error('Controller file already exists: '.$path.'\/'.$fileName); |
| 80 | + |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + // Write the content to the file |
| 85 | + File::put($path.'/'.$fileName, $stub); |
| 86 | + |
| 87 | + // Inform user about successful creation |
| 88 | + $this->info('DataTable controller created successfully: '.$path.'/'.$fileName); |
| 89 | + } |
| 90 | + |
| 91 | + private function modelExists($modelName): bool |
| 92 | + { |
| 93 | + $modelFilePath = app_path('Models/'.$modelName.'.php'); |
| 94 | + |
| 95 | + return File::exists($modelFilePath); |
| 96 | + } |
| 97 | + |
| 98 | + // Function to get the path to the model file |
| 99 | + private function getModelPath($modelName): string |
| 100 | + { |
| 101 | + return app_path('Models/'.$modelName.'.php'); |
| 102 | + } |
| 103 | + |
| 104 | + // Function to extract $fillable fields from model content |
| 105 | + private function extractFillableFields($modelContent): array |
| 106 | + { |
| 107 | + $matches = []; |
| 108 | + preg_match('/\$fillable\s*=\s*\[(.*?)\];/s', $modelContent, $matches); |
| 109 | + if (isset($matches[1])) { |
| 110 | + return array_map('trim', explode(',', $matches[1])); |
| 111 | + } |
| 112 | + |
| 113 | + return []; |
| 114 | + } |
| 115 | + |
| 116 | + private function getSingularName($controller): string |
| 117 | + { |
| 118 | + return Str::singular($this->getPascalCaseFirstSegment($controller)); |
| 119 | + } |
| 120 | + |
| 121 | + private function getPascalCaseFirstSegment(string $inputString): string |
| 122 | + { |
| 123 | + // Split the input string by uppercase letters to get the first segment |
| 124 | + preg_match('/^[A-Z][a-z]*/', $inputString, $matches); |
16 | 125 |
|
17 | | - return self::SUCCESS; |
| 126 | + // Return the matched segment |
| 127 | + return $matches[0]; |
18 | 128 | } |
19 | 129 | } |
0 commit comments